I'm writing a Cocoa application and I'm trying to export to the Excel XML format (ISO/IEC 29500-1) which is basically a zip file with a bunch of XML files in it named with a .xlsx
extension.
I've tried generating AppleScript and using NSAppleScript
to create the Excel file, which works, but is slow and the user has to have Excel installed on their machine.
I was able to create a simple .xlsx Excel file by writing the XML to files in Objective C and zipping them up and renaming the zip to .xlsx
. Excel could open the file, but Numbers threw an error trying to open the file I created. It seems Excel must have implemented all (or most of) the ISO/IEC spec, while Numbers may only open the Excel flavored version.
How to create working Excel files using Cocoa and Objective-c?
You can open XLSX files with Microsoft Excel in Windows and macOS. Excel is the best option for opening XLSX files because it fully supports the formatting of Excel spreadsheets, which includes images, graphs, and spacing of data fields.
XLSX seems to be XML by definition, but the encoding is not as nailed down. UTF-8 simply seems to be the default convention. This page also seems to indicate it is what is described in the XML header... If you change the file extension to txt or open in a text editor you should be able to see...
XLSX is a zipped, XML-based file format. Microsoft Excel 2007 and later uses XLSX as the default file format when creating a new spreadsheet. Support for loading and saving legacy XLS files is also included. XLS is the default format used with Office 97-2003.
For compatibility, XLS has higher compatibility than XLSX. XLS is readable by all Microsoft Excel versions while XLSX is only readable by Excel 2007 and later versions. besides, XLS is able to hold the spreadsheets either including Macros or not, while XLSX isn't capable to support Macros.
How about encapsulating HTML table in Excel file? I have checked that this works and I can open it with Excel.
NSURL * documentsDirectory = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject;
NSURL *file = [documentsDirectory URLByAppendingPathComponent:@"contacts.xls"];
NSString* string = @"<table><tr><td>FOO</td><td>BAR</td></tr></table>";
[string writeToFile:file.path atomically:YES encoding:NSUTF8StringEncoding error:nil];
I've worked with the OpenOffice API and it works great. I would suggest you work with that to create your Excel files. If you don't have time time/patience to do that, a workaround could be to have 2 links to export files, the XLSX version hack for the Excels of the world and then a separate exported CSV file for the Numbers application.
Wikipedia offers some links to libraries, which would probably give you a good start.
If the Office Open XML format isn't a requirement, and you don't need overly complex files exported, I also suggest checking saving the files as stylesheet formatted HTML which Excel can also read. A simple way to learn how to format the HTML you want as a spreadsheet is creating the file in Excel, and saving it as HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With