开发者

how to dynamically add the data to html file

开发者 https://www.devze.com 2023-01-21 07:22 出处:网络
i hav a tabular forms used to generate reports which are html files and i want to save these files by filling with data dynamically using obj-c..

i hav a tabular forms used to generate reports which are html files and i want to save these files by filling with data dynamically using obj-c..

any ideas how to do ..

开发者_如何学运维Thanks in advance


I do a lot of this. My best friend here is NSString & Co.

Take a special look at the method stringWithFormat.

For example, here is me setting colors for an html table.

-(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;
}

For each of the pieces you build you can add to your main html string using the NSString method appendString. After you have finished building your string you cah save it as a file or eamil it or whatever...

Here is the main routine. It should give you a good idea of how to dynamically build and html string:

-(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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消