Creating Excel XLS files on iOS

孤者浪人 提交于 2019-12-04 10:45:43

问题


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.

<?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?


回答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).

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:

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




回答2:


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.

There is an example here as well: http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats




回答3:


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:


Try To create a CSV file format,it will be best method



来源:https://stackoverflow.com/questions/10818207/creating-excel-xls-files-on-ios

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