0

I have a big excel file and I want to be able to use this in my app and sort by name. I have 3 columns, name, height and weight.

Any advice would be so very greatly appreciated...I've been trying to do this for days and I don't know how to!

  • 2
    Possible dupe: http://stackoverflow.com/questions/4936281/iphone-app-that-reads-csv-file – Blender Nov 18 '11 at 17:19
  • No, i want to know how I can get my app to read the data and sort the data on a CSV file. not if I can open it on an iPhone! Thanks anyway. Any more suggestions? – user1054273 Nov 18 '11 at 17:23
  • Your app will be running on an iPhone, so you should be reading and writing your CSV on the iPhone. You must parse a CSV before you can sort it. Could you explain what you are looking for? – Blender Nov 18 '11 at 17:25
  • I am intending to do a sort for people and see what their height and weight are. I have a huge CSV file with the details on that I want to be able to let my app read and display, only within the app, not just have a CSV reader. I can do all the nice layout and everything, I just don't know how to call the data from the CSV file. – user1054273 Nov 18 '11 at 17:33
  • That's what I'm suggesting, a CSV reader ;) But CSV isn't a very nice format, IMO, so I'd suggest you convert it to XML (as XML is much more versatile and easy to parse). – Blender Nov 18 '11 at 17:37

2 Answers2

0

If you're not worried about parsing an actual CSV file in your iPhone app, but just about accessing the data, I would think about converting the CSV data into a .plist format. plist files (in text format) are very handy because they're human readable and you can suck the contents into an NSDictionary or similar data structure at runtime with very little effort.

This is addressed in this question:

Convert Excel document (xls) to a plist

I've personally used the 'make a formula inside your spreadsheet to generate .plist file contents' approach successfully several times before. See Christopher Fairbairn's answer at the above link for more details.

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
0

A 3 column csv is a doddle to parse, assuming you've got control over it and there are no silly quoted commas or anything.

You can do it with the following untested pseudo-sorta-typed-on-my-phone-code:

NSString *fileContents = [NSString stringWithContentsOfFile:@"myFile.csv" encoding:_whatever_ error:nil];
NSMutableArray *parsedData = [NSMutableArray array];
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];

for (NSString *line in lines)
{
    NSArray *fields = [line componentsSeparatedByString:@","];
    // add a dictionary or whatever to your array containing the relevant values from fields...
}
jrturton
  • 118,105
  • 32
  • 252
  • 268