如何动态添加数据到html文件

时间:2010-10-21 15:07:37

标签: html objective-c dynamic

我有一个表格形式,用于生成html文件的报告,我想通过使用obj-c动态填充数据来保存这些文件。

任何想法怎么做..

提前致谢

1 个答案:

答案 0 :(得分:1)

我做了很多这个。我最好的朋友是NSString&有限公司

特别注意方法stringWithFormat。

例如,这是我为html表设置颜色。

-(NSString *) colorcomponents:(int) i size:(int) s
{
    CGColorRef color = [[coltable objectAtIndex:i] CGColor];
    NSString * tablecompformat = @"<td style=\"background: rgb(%d, %d, %d)\" width=%d height=%d> <b>  </b>  </td>";
    int numComponents = CGColorGetNumberOfComponents(color);

    if (numComponents == 4)
    {
        const CGFloat *components = CGColorGetComponents(color);
        CGFloat red = components[0]*256;
        CGFloat green = components[1]*256;
        CGFloat blue = components[2]*256;

        return [NSString     stringWithFormat:tablecompformat,(int)red,(int)green,(int)blue,s,s];
    }
    return nil;
}

对于您构建的每个部分,您可以使用NSString方法appendString添加到主html字符串。完成字符串构建后,您可以将其保存为文件或eamil或其他任何内容......

这是主要例程。它应该让你很好地了解如何动态构建和html字符串:

-(NSString *) emailstr
{
    NSMutableString * ress=[[NSMutableString alloc] initWithCapacity:100];

    NSString * formatstring=@"<tr><td > <b> %@</b>  </td>  <td>: %@ </td></tr>\n";
    //NSString * formatstring2=@"<tr><td> <b> %@</b>  </td>  <td>: %@ </td> <td>: %@ </td> </tr>\n";
    NSString * hline=@"<tr><td colspan=\"3\">  <hr>  </td>  </tr>\n";

    [ress appendString:@"<html><body>\n"];
    [ress appendString:[NSString stringWithFormat:@"<h3> Game: %@ </h3> \n", [gm name]  ]];

    [ress appendString:@"<table border=\"0\">"];// beginning of main table
    [ress appendString:@"<tr><td > <b> Initial</b>  </td> <td>"];

    [ress appendString:@"<table border=\"0\">"];
    for (int i=0;i<[gm xcount];i++)
    {
        [ress appendString:@"<tr> "];
        for(int j=0;j<[gm ycount];j++)
        {
            int k=[gm getinitpos:i y:j];
            [ress appendString:[self colorcomponents:k size:10]];
        }
        [ress appendString:@"</tr> "];
    }
    [ress appendString:@"</table><br>\n "];
    [ress appendString:@"</td></tr>\n"];

    [ress appendString:@"<tr><td> <b> End</b>  </td> <td>"];
    [ress appendString:@"<table border=\"0\">"];
    for (int i=0;i<[gm xcount];i++)
    {
        [ress appendString:@"<tr> "];
        for(int j=0;j<[gm ycount];j++)
        {
            int k=[gm getpos:i y:j];
            [ress appendString:[self colorcomponents:k size:10]];

        }
        [ress appendString:@"</tr> "];
    }
    [ress appendString:@"</table><br>"];
    [ress appendString:@"</td></tr>\n"];


    [ress appendString:[NSString stringWithFormat:formatstring,@"Rules",[sql getrulesname:[gm rules_id]]]];
    if ([gm step]>0) 
    {
        //[ress appendString:@"<table border=\"0\">"];
        [ress appendString:hline]; 

        for (int i=1; i<=[gm step];i++)
        {
            int x=[sql getlogx:[gm session_id] step:i];
            int y=[sql getlogy:[gm session_id] step:i];

            NSString * pnum = [NSString stringWithFormat:@"Move %d",i];
            NSString * posstring=[NSString stringWithFormat:@"(%d, %d)",x+1,y+1 ];
            [ress appendString:[NSString stringWithFormat:formatstring,pnum,posstring]];
        }
        [ress appendString:hline];
        //[ress appendString:@"</table><br>"];
    }

    [ress appendString:@"</table><br>"]; //end of main table

    [ress appendString:@"</body></html>"];

    return ress;
}