xcode Objective-c 从 excel 加载数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9974501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
objective-c loading data from excel
提问by loucidity
I'm writing an application for the iPad that will tell us whether or not someone is on court at a tennis tournament. Basically, just an application with a list of names each with an on/off button next to them. Once the on button is pressed, their name turns red, thus they are on court.
我正在为 iPad 编写一个应用程序,它会告诉我们是否有人在参加网球锦标赛。基本上,只是一个带有名称列表的应用程序,每个名称旁边都有一个开/关按钮。一旦按下开启按钮,他们的名字就会变成红色,这样他们就上场了。
Is there any way, to make this easier for myself, I would be able to connect it to my computer and load in a list of names once the application is complete? That would save me from having to individually enter/modify names manually.
有什么方法可以让我自己更轻松地将其连接到我的计算机并在应用程序完成后加载名称列表?这将使我不必手动单独输入/修改名称。
Thanks in advance, Louis.
提前致谢,路易斯。
回答by Dr.Kameleon
Though your question is pretty... vague, one suggestion :
虽然你的问题很...模糊,但一个建议:
importing Spreadsheet-like data could easily be done by import the .csv
version (Comma-separated value) of a Spreadsheet.
通过导入电子表格的.csv
版本(逗号分隔值)可以轻松地导入类似电子表格的数据。
A comma-separated values (CSV)file stores tabular data (numbers and text) in plain-text form. Plain text means that the file is a sequence of characters, with no data that has to be interpreted instead, as binary numbers. A CSV file consists of any number of records, separated by line breaks of some kind; each record consists of fields, separated by some other character or string, most commonly a literal TAB or comma. Usually, all records have an identical sequence of fields.
甲逗号分隔值(CSV)文件存储表格数据(数字和文本)在纯文本格式。纯文本意味着文件是一个字符序列,没有数据必须被解释为二进制数。CSV 文件由任意数量的记录组成,由某种换行符分隔;每个记录由字段组成,由一些其他字符或字符串分隔,最常见的是文字 TAB 或逗号。通常,所有记录都具有相同的字段序列。
Example :
例子 :
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
Then, you could simply :
然后,您可以简单地:
(1)Load your csv file as a simple text file
(1)将您的 csv 文件加载为简单的文本文件
NSString* contents = [NSString stringWithContentsOfFile:filename
encoding:NSUTF8StringEncoding
error:nil];
(2)Get the lines
(2)获取行
NSArray* lines = [contents componentsSeparatedByString:@"\n"];
(3)Parse each line's fields
(3)解析每一行的字段
for (NSString* line in lines)
{
NSArray* fields = [line componentsSeparatedByString:@","];
}
回答by Peyo
This is a CSV problem overall, not specific to this code. Indeed if CSV fields contains commas, this is broken. Happens a lot if your numbers standards uses commas for floating point numbers instead of dots.
这是一个 CSV 问题,并非特定于此代码。事实上,如果 CSV 字段包含逗号,这将被破坏。如果您的数字标准使用逗号而不是点来表示浮点数,则会发生很多情况。
I tend to always used CSVs where the field separator is TAB (aka "\t") instead of comma. Much more unlikely to be broken. This is usually configurable in CSV-producing tools such as Excel.
我倾向于总是使用字段分隔符是 TAB(又名“\t”)而不是逗号的 CSV。更不可能被打破。这通常可以在 CSV 生成工具(如 Excel)中进行配置。