我是iphone应用程序开发的新手。在我的应用程序中,我使用两个不同的NSMutable
数组。两者都有字典,每个索引都有4个字段,如名称,地址等。现在我的问题是我想以HTML表格格式导出该数据并制作电子邮件正文。所以,请任何人帮助我。
谢谢
答案 0 :(得分:1)
例如,项目是:
NSMutableArray *arrItem = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"sam",@"name",@"london",@"address",nil], nil];
现在create html string
就像这样:
NSString *strHTML = @"<!DOCTYPE html>"
@"<html><body>"
@"<table border=\"1\" style=\"width:300px\">"; //change width according to your requirement
for (NSDictionary *dict in arrItem) {
NSString *strName = [dict objectForKey:@"name"];
NSString *strAddress = [dict objectForKey:@"address"];
strHTML = [strHTML stringByAppendingString:@"<tr>"];
//add table column value
strHTML = [strHTML stringByAppendingString:[NSString stringWithFormat:@"<td>%@</td>",strName]];
strHTML = [strHTML stringByAppendingString:[NSString stringWithFormat:@"<td>%@</td>",strAddress]];
strHTML = [strHTML stringByAppendingString:@"</tr>"];
}
strHTML = [strHTML stringByAppendingString:@"</table>"
@"</body>"
@"</html>"];
在hmtl string
中使用MailcompserViewController
:
[controller setMessageBody:strHTML isHTML:YES];
编辑:格式化
答案 1 :(得分:0)
MailcompserViewController *controller = [[MailcompserViewController alloc] init];//Allocating mailComposer
NSString *eMailBody=@""; //Value for row
for (int i=0; i<[arrMailList count]; i++) { //Get all the row value
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
MailData *data = [arrMailList objectAtIndex:i]; //Get array value in NSObject class
NSDate *date = [dateFormat dateFromString:data.date];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"EEEE, dd MMMM yyyy"]; //Date NSDateFormatter
NSString *strDate = [dateFormat1 stringFromDate:date];
NSString *numbers = [data.setName stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];
eMailBody = [eMailBody stringByAppendingFormat:@"<tr><td width=\"200px\">%@</td><td >%@</td><td >%@</td><td >%@</td><td>%@</td><td >%@</td></tr>",strDate,data.Name,data.email,data.address,data.phoneNumber];//Create row
}
NSString *html = [NSString stringWithFormat:@"<html><style>body{font-family:Helvetica;font-size:14px;}th{fontcolor:#ffffff;font-size:16px;font-weight:BOLD;}</style><body><table cellpadding=5px border=1px bordercolor=#000000><tr bgcolor=#cccccc><th width=200>Date</th><th>Workout Name</th><th>Excercise Name</th><th>Set</th><th>Weight</th><th>Reps</th>%@</table></body></html>",eMailBody]; //Add row in table view
[controller setMessageBody:html isHTML:YES];
[self presentViewController:controller animated:YES completion:nil];
答案 2 :(得分:0)
var str =“\ n”
str=str.stringByAppendingString("<tr><th>")
str=str.stringByAppendingString("Task")
str=str.stringByAppendingString("</th>")
str=str.stringByAppendingString("<th>")
str=str.stringByAppendingString("SpareParts")
str=str.stringByAppendingString("</th>")
str=str.stringByAppendingString("<th>")
str=str.stringByAppendingString("Labour")
str=str.stringByAppendingString("</th></tr>\n")
var i:Int=0
for i=0;i<carImageList.count;i++
{
str=str.stringByAppendingString("<tr><td>")
str=str.stringByAppendingString(carImageList[i] as! String)
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString(carImageList[i] as! String)
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString(carImageList[i] as! String)
str=str.stringByAppendingString("</td></tr>\n")
}
//SubTotal
str=str.stringByAppendingString("<tr><td>")
str=str.stringByAppendingString("Sub Total")
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString("100")
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString("100")
str=str.stringByAppendingString("</td></tr>\n")
//Total
str=str.stringByAppendingString("<tr><td>")
str=str.stringByAppendingString("Total")
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td colSpan='2'>")
str=str.stringByAppendingString("300")
str=str.stringByAppendingString("</td></tr>\n")
str=str.stringByAppendingString("</table></body></html>\n")
NSLog("TEST %@",str)
mailComposerVC.setMessageBody(str, isHTML: true)