2012-05-30 56 views
9

我试图以Excel格式创建报告,准备通过电子邮件发送。到目前为止,我发现最好和最简单的方法是按如下所示创建一个xml文档并将其保存为xls。在iOS上创建Excel 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文件?

+1

嗯,对我来说Excel 2010的作品 – CarlJ

+0

感谢您的评论!是的,它在Excel 2010上工作正常。但它显示文件格式与文件扩展名不匹配的错误。它不会在Google文档和网页中打开。所以我认为这个问题可能来自我正在保存的xml文档。 – ChrisBorg

+0

有没有人可能知道一个很好的方式来形成一个XML文档,它可以被Google Spreadsheets和Numbers支持呢? – ChrisBorg

回答

-3

尝试创建一个CSV文件格式,这将是

0

试图在文件的顶部添加下面的特殊标签的最佳方法:

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
[...] 

我只是尝试这样做标记与XML并且它在我的Windows7机器上工作 - 我将文档保存为'test.excel.xml' - 并且<?mso...>标记足以让Windows将格式识别为Excel。

有一个例子在这里也:http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats

+0

使用文件扩展名.xml,可以在Windows7上使用Excel 2010。在Mac OSX上使用Excel 2011时,必须使用文件扩展名.xls作为相同的文件内容。不幸的是,我不知道可以在两个平台上工作的变体。 – pommy

1

我想同样的事情作为OP,也放弃了。我最终使用libxls打开并读取xls文件,并使用csv写出文件。 (libxls不能写入xls,只能读取它)。

http://libxls.sourceforge.net

我还没有尝试过,但xlslib声称写Excel文件。它有2014年1月6日的最新说法它可以在iOS的工作:

http://sourceforge.net/projects/xlslib/files/

此外,阅读它为什么这么难写Excel文件,因为它的真实和有趣: http://www.joelonsoftware.com/items/2008/02/19.html

0

这是创建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]; 
     }); 
}); 
+0

快速如何? –