简体   繁体   English

在iOS上创建Excel XLS文件

[英]Creating Excel XLS files on iOS

I am trying to create a report in Excel format, ready to be sent by email. 我正在尝试以Excel格式创建报告,准备通过电子邮件发送。 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文档,如下所示并保存为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. 但是,在我发送电子邮件并尝试打开xls文件后,xml将显示在电子表格中。 Could anyone please lead me to the right direction to create this xls file? 有谁能请我指向正确的方向来创建这个xls文件?

I tried the same thing as the OP, and gave up. 我尝试了和OP一样的东西,然后放弃了。 I eventually used libxls to open and read xls files, and wrote the files out using csv. 我最终使用libxls打开并读取xls文件,并使用csv写出文件。 (libxls can't write xls, just read it). (libxls不能写xls,只是读它)。

http://libxls.sourceforge.net http://libxls.sourceforge.net

I haven't tried it, but xlslib claims to write excel files. 我没有尝试过,但xlslib声称要编写excel文件。 It has an update in Jan 6 2014 saying it can work in iOS: 它在2014年1月6日有一个更新,说它可以在iOS上运行:

http://sourceforge.net/projects/xlslib/files/ 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 另外,阅读为什么编写excel文件如此困难,因为它真实而有趣: http//www.joelonsoftware.com/items/2008/02/19.html

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. 我刚刚用你的XML尝试了这个标签,它在我的Windows7机器上工作 - 我将你的文件保存为'test.excel.xml' - 而<?mso...>标签足以让Windows将格式识别为Excel。

There is an example here as well: http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats 这里也有一个例子: http//en.wikipedia.org/wiki/Microsoft_Office_XML_formats

This is the code for create XLS file 这是创建XLS文件的代码

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

尝试创建CSV文件格式,这将是最好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM