views:

459

answers:

2

As simple as possible: I have a very simple Excel spreadsheet with just over 1k records. I want to use this as a static datasource for an iPhone application.

What's the best plan of attack?

Possibilities in my mind:

1) Reading the XLS directly as a data source: is there an Obj-C lib for this?

2) Converting the XLS to a format that Obj-C has a lib for... CSV? XML? some native CoreData format?

3) Importing it into something MySQLish and just getting an XML feed from a server.

I would need some help figuring these approaches out. I've never worked with Excel.

1 would be nice, 2 would be probably the best solution for what I am doing right now, and 3 I pretty much know how to do but I am not actually sure if MySQL has an XLS import (I believe MSSQL does).

A: 

You might look into rendering the Excel file with a UIWebView instance, and seeing if you can access the DOM within the web view.

If you can access the DOM, you could perhaps use libxml2 or a similar library to write a parser that retrieves data from it.

Alex Reynolds
+1  A: 

I had a similar problem at one point. What I ended up doing was writing a simple application that took a CSV file and converted it into a Plist. Then I used the Plist as needed in the app. This code uses cCSVParse. It will use the headers in the first row as the key names to create an array of dictionaries for each successive row. The output is a tidy plist file with all of your data. Use [NSArray arrayWithContentsOfFile:] to pop the data into memory in your app.

    CSVParser *parser = [CSVParser new];
[parser openFileWithPath:pathAsString];
NSMutableArray *csvContent = [parser parseFile];
[parser closeFile];

if (pathAsString != nil)
{

 NSArray *keyArray = [csvContent objectAtIndex:0];

 NSMutableArray *plistOutputArray = [NSMutableArray array];

 NSInteger i = 0;

 for (NSArray *array in csvContent)
 {



  NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

  NSInteger keyNumber = 0;

  for (NSString *string in array)
  {

   [dictionary setObject:string forKey:[keyArray objectAtIndex:keyNumber]];

   keyNumber++;

  }

  if (i > 0)
  {
   [plistOutputArray addObject:dictionary];
  }

  i++;

 }

 NSMutableString *mutableString = [NSMutableString stringWithString:pathAsString];
 [mutableString replaceOccurrencesOfString:@".csv" withString:@".plist" options:nil range:NSMakeRange([mutableString length]-4, 4)];

 NSURL *url = [NSURL fileURLWithPath:mutableString];


 [plistOutputArray writeToURL:url atomically:YES];

I also built a pretty simple UI for this. Maybe I'll clean up the whole project and post it on Google Code.

Danilo Campos