2012-05-30 59 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

快速如何? –