在iOS上创建Excel XLS文件 [英] Creating Excel XLS files on iOS

查看:222
本文介绍了在iOS上创建Excel XLS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建Excel格式的报告,准备通过电子邮件发送.到目前为止,我发现最好和最简单的方法是按如下方式创建xml文档并将其另存为xls.

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>

然后我可以使用

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

但是,在我发送电子邮件并尝试打开xls文件之后,该XML会显示在电子表格中.有人可以引导我朝正确的方向创建这个xls文件吗?

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?

推荐答案

我尝试了与OP相同的操作,并放弃了.我最终使用libxls打开和读取xls文件,并使用csv将文件写出. (libxls不能编写xls,只能读取它).

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

我还没有尝试过,但是xlslib声称要编写excel文件.它在2014年1月6日进行了更新,表示它可以在iOS中使用:

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/

此外,请阅读为何编写excel文件如此困难的原因,因为它是真实而有趣的: http://www.joelonsoftware.com/items/2008/02/19.html

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

这篇关于在iOS上创建Excel XLS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆