通过使用xml在iOS 5应用程序上创建文件将数据导出到* .xls文件 [英] Export data to *.xls file by using xml to create the file on an iOS 5 App
问题描述
我正在使用以下代码在我的iOS应用上创建一个* .xls文件.一切顺利.我可以用Microsoft Excel打开它,但是,如果我想用Apple Numbers打开它,则不起作用.
I'm using the following code, to create an *.xls file on my iOS App. Everything is going right. I can open it with Microsoft Excel but, if I would like to open it with Apple Numbers, it doesn't work.
- (void)exportToExcel
{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSError *error;
NSString *header = @"<?xml version=\"1.0\"?>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\nxmlns:o=\"urn:schemas-microsoft-com:office:office\"\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\nxmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\nxmlns:html=\"http://www.w3.org/TR/REC-html40\">\n<Worksheet ss:Name=\"Sheet1\">\n<Table ss:ExpandedColumnCount=\"";
NSString *columncount = @"\" ss:ExpandedRowCount=\"";
NSString *rowcount = @"\" x:FullColumns=\"1\"x:FullRows=\"1\">\n";
NSString *rowStart = @"<Row>\n";
NSString *rowEnde = @"\n</Row>\n";
NSString *stringStart = @"<Cell><Data ss:Type=\"String\">";
NSString *stringEnde = @"</Data></Cell>";
NSString *numberStart = @"<Cell><Data ss:Type=\"Number\">";
NSString *numberEnde = @"</Data></Cell>";
NSString *footer = @"</Table>\n</Worksheet>\n</Workbook>";
NSString *xlsstring = @"";
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Form" inManagedObjectContext:context];
[request setEntity:entity];
NSArray *arr = [context executeFetchRequest:request error:&error];
int numberOfRows = 1;
int numberOfCols = 2;
for (Form *form in arr) {
numberOfRows = numberOfRows + 1;
}
xlsstring = [NSString stringWithFormat:@"%@%i%@%i%@", header, numberOfCols, columncount, numberOfRows, rowcount];
xlsstring = [xlsstring stringByAppendingFormat:@"%@%@Patient number%@%@Name%@%@", rowStart, stringStart, stringEnde, stringStart, stringEnde, rowEnde];
for (Form *form in arr) {
xlsstring = [xlsstring stringByAppendingFormat:@"%@%@%@%@%@%@%@%@", rowStart, numberStart, form.pnumber, numberEnde, stringStart, form.name, stringEnde, rowEnde];
}
xlsstring = [xlsstring stringByAppendingFormat:@"%@", footer];
[xlsstring writeToFile:@"/Users/***/Desktop/form.xls" atomically:YES encoding:NSISOLatin1StringEncoding error:nil];
}
有人知道为什么会这样吗,甚至是针对此错误的解决方案吗?
Does anyone have any idea why this might be so, or even a solution for this error?
推荐答案
数字不会导入Excel XML格式. (请参见此处.)它将导入Excel本机格式,但是写起来并不是一件容易的事.
Numbers won't import the Excel XML format. (See here.) It'll import the Excel native format, but writing that isn't going to be a trivial job.
您能否让您的应用同时具有Excel XML(对于Excel用户)和CSV(对于Numbers用户)的选项?
Could you make your app have the option of both Excel XML (for Excel users) and CSV (for Numbers users)?
这篇关于通过使用xml在iOS 5应用程序上创建文件将数据导出到* .xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!