Convert Excel document (xls) to a plist

John Muchow picture John Muchow · Oct 9, 2009 · Viewed 26.5k times · Source

I have a pretty straightforward Excel spreadsheet, and I need to use the data in an iPhone app. The xls document has 6 columns, and > 200 rows.

I would like to create a plist from the xls document. How can I convert one to the other, programmatically?

Answer

Danilo Campos picture Danilo Campos · Dec 5, 2009

I'm late to the party but I built a desktop utility that will convert CSV to a plist. You can download the binary or use this code, which requires cCSVParse. It uses whatever is in row 0 to create key names, then generates dictionaries for each successive row.

    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];