1. Export to plain text CSV
If all you are trying to do is extract data from Excel to use it elsewhere, unlike capturing formulas and formatting Excel, you probably should not try to read the .xls file. XLS is a complex format. This is good for Excel, not for general data sharing.
Likewise, you probably don't need to use AppleScript or anything else to integrate with Excel, if all you want to do is save the data in clear text. Excel already knows how to save data in clear text. Just use the Excel Save As command. (This is what he called on the Mac. I don't know about the PC.)
The question is which plaintext format to use. One obvious choice for this is the plain text comma-delimited (CSV) file because it is a simple de facto standard (as opposed to a complex official standard such as XML). This will facilitate consumption in Swift or in any other language.
2. Export in UTF-8 encoding, if possible, other than UTF-16
So how do you do this? Plaintext is remarkably simple, but one subtlety that you need to keep track of is the text encoding. Text encoding is a way of representing characters in a plaintext file. Unfortunately, you cannot reliably specify the encoding of the file, just by looking at the file, so you need to select the encoding when saving it and do not forget to use this encoding when reading it. If you ruin this, accented characters, typographic quotes, dashes, and other non-ASCII characters will be distorted. So what text encoding should you use? Short answer: you should always use UTF-8, if possible .
But if you are working with an old version of Excel, you will not be able to use UTF-8. In this case, you should use UTF-16. In particular, UTF-16, I believe, is the only export option in Excel 2011 for Mac that gives a predicted result that will not depend on unexpected methods from unclear locale settings or Microsoft-specific encodings.
So, if you are using Excel 2011 for Mac, for example, select "Unicode Text UTF-16" from the Excel Save As command.
This will force Excel to save the file so that each line is a line of text and each column is separated by a tab character. (So technically these are tab-delimited value files, not a comma-delimited value file.)
3. Import with Swift
Now you have a plaintext file, which, as you know, was saved in UTF-8 (or UTF-16) encoding. So now you can read it and analyze it in Swift.
If your Excel data is complex, you may need a full-featured CSV parser. the best option is probably CHCSVParser .
Using CHCSV, you can parse the file with the following code:
NSURL * const inputFileURL = [NSURL fileURLWithPath:@"/path/to/exported/file.txt"]; unichar tabCharacter = '\t'; NSArray *rows = [NSArray arrayWithContentsOfCSVFile:inputFilePath options:CHCSVParserOptionsSanitizesFields delimiter:tabCharacter];
(Of course, you can also call it from Swift.)
On the other hand, if you data is relatively simple (for example, it does not have escaped characters), you may not need to use an external library at all. You can write Swift code that parses tab-delimited values only by reading in a file as a line, splitting a newline into lines, and then splitting into tabs .
This function will take a String representing TSV data and return an array of dictionaries:
func JSONObjectFromTSV(tsvInputString:String, columnNames optionalColumnNames:[String]? = nil) -> Array<NSDictionary> { let lines = tsvInputString.componentsSeparatedByString("\n") guard lines.isEmpty == false else { return [] } let columnNames = optionalColumnNames ?? lines[0].componentsSeparatedByString("\t") var lineIndex = (optionalColumnNames != nil) ? 0 : 1 let columnCount = columnNames.count var result = Array<NSDictionary>() for line in lines[lineIndex ..< lines.count] { let fieldValues = line.componentsSeparatedByString("\t") if fieldValues.count != columnCount {
Therefore you only need to read the file in a line and pass it to this function. This snippet is derived from this value for the tsv-to-json converter . And if you need to know more about which text encodings are produced by Microsoft products and which Cocoa can be automatically detected, then this text encoding repo contains research on export samples, which led to the conclusion that UTF-16 is the way for old products Microsoft on Mac.
(I understand that I'm getting attached to my own repositories here. Excuse me?)