问题
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?
回答1:
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.
Could you make your app have the option of both Excel XML (for Excel users) and CSV (for Numbers users)?
来源:https://stackoverflow.com/questions/10735805/export-data-to-xls-file-by-using-xml-to-create-the-file-on-an-ios-5-app