I am trying to create a report in Excel format, ready to be sent by email. So far I have found that the best and simplest way is to create an xml document as follows and save it as xls.
我正在尝试以Excel格式创建报告,准备通过电子邮件发送。到目前为止,我发现最好和最简单的方法是创建一个xml文档,如下所示,并将其保存为xls。
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1">
<Row>
<Cell><Data ss:Type="String">Name</Data></Cell>
<Cell><Data ss:Type="String">Example</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">Value</Data></Cell>
<Cell><Data ss:Type="Number">123</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Then I could save this document using
然后我可以使用保存此文档
NSArray *documentDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [NSString stringWithFormat:@"%@/Report.xls", [documentDirectoryPath objectAtIndex:0]];
[xmlString writeToFile:docDir atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[serializedData writeToFile:docDir atomically:YES];
However, after I send the email and try to open the xls file, the xml is displayed in the spreadsheet instead. Could anyone please lead me to the right direction to create this xls file?
但是,在我发送电子邮件并尝试打开xls文件后,xml将显示在电子表格中。有谁能请我找到正确的方向来创建这个xls文件?
4 个解决方案
#1
1
I tried the same thing as the OP, and gave up. I eventually used libxls to open and read xls files, and wrote the files out using csv. (libxls can't write xls, just read it).
我尝试了与OP相同的东西,然后放弃了。我最终使用libxls打开并读取xls文件,并使用csv写出文件。 (libxls不能写xls,只是读它)。
http://libxls.sourceforge.net
I haven't tried it, but xlslib claims to write excel files. It has an update in Jan 6 2014 saying it can work in iOS:
我没有尝试过,但xlslib声称要编写excel文件。它在2014年1月6日有一个更新,说它可以在iOS上工作:
http://sourceforge.net/projects/xlslib/files/
http://sourceforge.net/projects/xlslib/files/
Also, read why it's so hard to write excel files, because it's true and funny: http://www.joelonsoftware.com/items/2008/02/19.html
另外,阅读为什么编写excel文件如此困难,因为它真实而有趣:http://www.joelonsoftware.com/items/2008/02/19.html
#2
0
Try adding in the following special tag at the top of the document:
尝试在文档顶部添加以下特殊标记:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
[...]
I just tried this tag with your XML and it worked on my Windows7 machine - I saved your document as 'test.excel.xml' - and the <?mso...>
tag was enough for Windows to recognize the format as Excel.
我刚刚用你的XML尝试过这个标签,它在我的Windows7机器上工作 - 我将你的文件保存为'test.excel.xml' - <?mso ...>标签足以让Windows将格式识别为Excel。
There is an example here as well: http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
这里也有一个例子:http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
#3
0
This is the code for create XLS file
NSMutableString *stringToWrite = [[NSMutableString alloc] init];
[stringToWrite appendString:[NSString stringWithFormat:@"First Name /t Last Name /t Full Name /tPhone Number /tEmail /t Job/t organizationName /tNote\n\n"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for(int i = 0 ;i<[Contact count];i++) {
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"firstName"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"lastName"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact valueForKey:@"userName"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"phoneNumber"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"emailAddress"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"jobTitle"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"organizationName"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[Contact objectAtIndex:i] valueForKey:@"note"] ]];
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.xls"];
[stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
});
});
#4
-4
Try To create a CSV file format,it will be best method
尝试创建CSV文件格式,这将是最好的方法
#1
1
I tried the same thing as the OP, and gave up. I eventually used libxls to open and read xls files, and wrote the files out using csv. (libxls can't write xls, just read it).
我尝试了与OP相同的东西,然后放弃了。我最终使用libxls打开并读取xls文件,并使用csv写出文件。 (libxls不能写xls,只是读它)。
http://libxls.sourceforge.net
I haven't tried it, but xlslib claims to write excel files. It has an update in Jan 6 2014 saying it can work in iOS:
我没有尝试过,但xlslib声称要编写excel文件。它在2014年1月6日有一个更新,说它可以在iOS上工作:
http://sourceforge.net/projects/xlslib/files/
http://sourceforge.net/projects/xlslib/files/
Also, read why it's so hard to write excel files, because it's true and funny: http://www.joelonsoftware.com/items/2008/02/19.html
另外,阅读为什么编写excel文件如此困难,因为它真实而有趣:http://www.joelonsoftware.com/items/2008/02/19.html
#2
0
Try adding in the following special tag at the top of the document:
尝试在文档顶部添加以下特殊标记:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
[...]
I just tried this tag with your XML and it worked on my Windows7 machine - I saved your document as 'test.excel.xml' - and the <?mso...>
tag was enough for Windows to recognize the format as Excel.
我刚刚用你的XML尝试过这个标签,它在我的Windows7机器上工作 - 我将你的文件保存为'test.excel.xml' - <?mso ...>标签足以让Windows将格式识别为Excel。
There is an example here as well: http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
这里也有一个例子:http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
#3
0
This is the code for create XLS file
NSMutableString *stringToWrite = [[NSMutableString alloc] init];
[stringToWrite appendString:[NSString stringWithFormat:@"First Name /t Last Name /t Full Name /tPhone Number /tEmail /t Job/t organizationName /tNote\n\n"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for(int i = 0 ;i<[Contact count];i++) {
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"firstName"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"lastName"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact valueForKey:@"userName"] objectAtIndex:i]]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"phoneNumber"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"emailAddress"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"jobTitle"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"organizationName"] ]];
[stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[Contact objectAtIndex:i] valueForKey:@"note"] ]];
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.xls"];
[stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
});
});
#4
-4
Try To create a CSV file format,it will be best method
尝试创建CSV文件格式,这将是最好的方法