10

I'm new to iOS and am trying to read the contents of a spreadsheet into an iOS array. The spreadsheet that I'm using is a simple 3 x 2 array of numbers for the first column and text for the second. I've tried reading it in with & without column headers, in .xls, .xlsx, . cdv, .txt (unicode and delimited) but without success. The file is called "funds", and the code I'm using is:

NSData       *databuffer;
NSFileHandle * file;
NSString     *docDir  = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)[0];
NSString     *fileDir = [docDir stringByAppendingPathComponent:@"funds.xls"];

file = [NSFileHandle fileHandleForReadingAtPath:fileDir];
if (file == nil) NSLog (@"Failed to open file");

[file seekTOOffset: 10];
databuffer = [file readDataOfLength: 5];
[file closeFile];

It finds the directory of the file but gives up at [NSFileHandle fileHandleForReadingAtPath:fileDir].

What should I be doing? I need help with confirming what file type I should use as the source, and how to read it into an array.

When this has been solved, I'm likely to need to read in large amount of data, several columns, 4k rows, a mixture of text & non-integer numbers.

Is there a format or method I should be using when the volume of data is getting to that size?

user2903240
  • 105
  • 1
  • 1
  • 5
  • where is the file present? In your bundle? – footyapps27 Jan 24 '14 at 16:28
  • Sorry for taking so long to reply, work has overtaken me. The file is stored in the bundle. altyus' reply seems like the best long term solution but I'm struggling to get my head around github! – user2903240 Feb 13 '14 at 15:20

4 Answers4

7

Be sure to export the data from Excel in CSV format.

It's easy to use NSFileManager to access the file:

NSString *pathName = ...;  /// fill in the file's pathname
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:pathName]) {
    // read the file contents, see below
}

For small files, you could just say:

NSString *lines = [NSString stringWithContentsOfFile:filePath];

and then proceed to split the string into lines, and process each line.

For large files, better have a look at some more sophisticated techniques, like in Objective-C: Reading a file line by line.

As for parsing the CSV lines, use the NSString method componentsSeparatedByString to tokenize each line, something like:

NSArray *fields = [line componentsSeparatedByString:@","];

Actually, that's also what you would use to split the file contents you read into individual lines. Just pay attention to the line feeds (CR LF or LF). You will find something in how to split a string with newlines.

Community
  • 1
  • 1
Jere Käpyaho
  • 1,305
  • 1
  • 10
  • 29
  • 1
    This will not parse lines with commas inside correctly. .csv file may have commas inside values, e.g One, "String, with, comma", Three – Alexey Podlasov Aug 04 '17 at 11:27
  • Well it won't handle quoted values either. It was just a naive example. Let's hope that by now people have found RFC 4180 and are using a decent CSV parser library. – Jere Käpyaho Aug 05 '17 at 08:50
  • Also not much help if you've got elements that prevent CSV export, such as multiple sheets in a workbook. – Ash Feb 07 '19 at 14:13
3

There is a very popular CocoaPod / Github project that both will write and parse CSV Files. https://github.com/davedelong/CHCSVParser

altyus
  • 606
  • 4
  • 13
  • Since the OP is new with iOS, it may be difficult to embrace CocoaPods, but this would be great for further down the line. I also found this quite useful, so thanks for pointing it out. – Jere Käpyaho Jan 24 '14 at 16:30
  • That is why I made sure to mention that there is a Github project as well. The Github link attached has instructions on adding the project directly by cloning the Github repo. But just incase the poster is interested in using Cocoa Pods (and (s)he should be), here's a link on using cocoa pods in your project (it's super easy). http://cocoapods.org/ and click 'get started'. – altyus Jan 24 '14 at 16:39
  • 1
    I agree, it's easier to get started with CocoaPods than one would think. Even a simple format like CSV has some potential complications, so it's good to have a solid component. Also, it uses NSInputStream, so it should be OK for large files, too. – Jere Käpyaho Jan 24 '14 at 16:45
0

You can find here: Where can I find a CSV to NSArray parser for Objective-C? quite similar problem, btw. converting CSV to JSON should help you parsing data, since JSON could be easily converted to the NSDictionary or NSArray.

Community
  • 1
  • 1
Artur Kucaj
  • 1,071
  • 1
  • 11
  • 16
-1

I use Numbers create csv and I have problem when I add "," to table. I use some trick to fix it. Sorry I try to add sample code here but it too long.

https://github.com/LovePick/CSVParser-Swift