I have an excel file with some data (with multiple columns and multiple rows). I want to import this data into my core-data based database. I’m struggling to import the data, because the text in the cells contain line breaks.
I’ve tried the following:
1.) Export excel to Tab-delimited text file
2.) Write an import route in iOS that reads the tab-delimited text file using the following code:
NSCharacterSet *tabCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"\t"];
NSArray *rows = [dataString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSArray *columns = [row componentsSeparatedByCharactersInSet:tabCharacterSet];
Problem: I have 1000 rows with 15 columns each. The parsing routine returns more than 1000 rows and less than 15 columns. The line breaks with-in the cell are not being handled properly by the parsing routine.
I get the same results if I use
[NSCharacterSet characterSetWithCharactersInString:@"\r\n"];
or
[NSCharacterSet characterSetWithCharactersInString:@"\r"];
instead of
[NSCharacterSet newlineCharacterSet]
but it completely fails if I use
[NSCharacterSet characterSetWithCharactersInString:@"\n"];
How can I parse the excel-data properly?
Perhaps I can use Regex expressions to get line components? Any idea/pointers?
UPDATE (Sample XLSX and Export File):


The solution that worked for me involves using
NSScannerclass.Reference: http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data