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.
尽可能简单:我有一个非常简单的Excel电子表格,只有超过1k的记录。我想将它用作iPhone应用程序的静态数据源。
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?
1)直接读取XLS作为数据源:是否有一个Obj-C lib?
2) Converting the XLS to a format that Obj-C has a lib for... CSV? XML? some native CoreData format?
2)将XLS转换为Obj-C具有... CSV的格式? XML?一些原生的CoreData格式?
3) Importing it into something MySQLish and just getting an XML feed from a server.
3)将其导入MySQLish并从服务器获取XML feed。
I would need some help figuring these approaches out. I've never worked with Excel.
我需要一些帮助来确定这些方法。我从未使用过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).
1会很好,2可能是我现在正在做的最好的解决方案,3我几乎知道怎么做但我不确定MySQL是否有XLS导入(我相信MSSQL会这样做)。
2 个解决方案
#1
1
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.
我有一个类似的问题。我最终做的是编写一个简单的应用程序,它接受一个CSV文件并将其转换为Plist。然后我在应用程序中根据需要使用了Plist。此代码使用cCSVParse。它将使用第一行中的标题作为键名,为每个连续的行创建一个字典数组。输出是一个包含所有数据的整洁的plist文件。使用[NSArray arrayWithContentsOfFile:]将数据弹出到应用程序的内存中。
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.
我还为此构建了一个非常简单的UI。也许我会清理整个项目并将其发布在Google Code上。
#2
0
You might look into rendering the Excel file with a UIWebView
instance, and seeing if you can access the DOM within the web view.
您可以考虑使用UIWebView实例渲染Excel文件,并查看是否可以在Web视图中访问DOM。
If you can access the DOM, you could perhaps use libxml2
or a similar library to write a parser that retrieves data from it.
如果可以访问DOM,则可以使用libxml2或类似的库来编写从中检索数据的解析器。
#1
1
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.
我有一个类似的问题。我最终做的是编写一个简单的应用程序,它接受一个CSV文件并将其转换为Plist。然后我在应用程序中根据需要使用了Plist。此代码使用cCSVParse。它将使用第一行中的标题作为键名,为每个连续的行创建一个字典数组。输出是一个包含所有数据的整洁的plist文件。使用[NSArray arrayWithContentsOfFile:]将数据弹出到应用程序的内存中。
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.
我还为此构建了一个非常简单的UI。也许我会清理整个项目并将其发布在Google Code上。
#2
0
You might look into rendering the Excel file with a UIWebView
instance, and seeing if you can access the DOM within the web view.
您可以考虑使用UIWebView实例渲染Excel文件,并查看是否可以在Web视图中访问DOM。
If you can access the DOM, you could perhaps use libxml2
or a similar library to write a parser that retrieves data from it.
如果可以访问DOM,则可以使用libxml2或类似的库来编写从中检索数据的解析器。