使用xml将数据导出到* .xls文件以在iOS 5应用程序上创build文件

我使用下面的代码,在我的iOS应用上创build一个* .xls文件。 一切正常。 我可以用Microsoft Excel打开它,但是,如果我想用Apple Numbers打开它,则不起作用。

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

有没有人有任何想法,为什么这可能是这样,甚至解决这个错误?

数字不会导入Excel XML格式。 (见这里) 。它会导入Excel本地格式,但写这不会是一个微不足道的工作。

你可以让你的应用程序可以同时selectExcel XML(用于Excel用户)和CSV(用于Numbers用户)?

Interesting Posts