Export data to *.xls file by using xml to create the file on an iOS 5 App

限于喜欢 提交于 2019-12-21 19:53:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!