简体   繁体   English

将.xls / .csv文件中的数据读入iOS

[英]Reading data from .xls/.csv files into iOS

I'm new to iOS and am trying to read the contents of a spreadsheet into an iOS array. 我是iOS的新手,我正在尝试将电子表格的内容读入iOS数组。 The spreadsheet that I'm using is a simple 3 x 2 array of numbers for the first column and text for the second. 我正在使用的电子表格是第一列的简单3 x 2数字数组和第二列的文本数字。 I've tried reading it in with & without column headers, in .xls, .xlsx, . 我试过用.xls,.xlsx中的有和没有列标题的内容阅读它。 cdv, .txt (unicode and delimited) but without success. cdv,.txt(unicode和delimited)但没有成功。 The file is called "funds", and the code I'm using is: 该文件称为“资金”,我正在使用的代码是:

NSData       *databuffer;
NSFileHandle * file;
NSString     *docDir  = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)[0];
NSString     *fileDir = [docDir stringByAppendingPathComponent:@"funds.xls"];

file = [NSFileHandle fileHandleForReadingAtPath:fileDir];
if (file == nil) NSLog (@"Failed to open file");

[file seekTOOffset: 10];
databuffer = [file readDataOfLength: 5];
[file closeFile];

It finds the directory of the file but gives up at [NSFileHandle fileHandleForReadingAtPath:fileDir] . 它找到文件的目录,但放弃[NSFileHandle fileHandleForReadingAtPath:fileDir]

What should I be doing? 我该怎么办? I need help with confirming what file type I should use as the source, and how to read it into an array. 我需要帮助确认我应该使用哪种文件类型作为源,以及如何将其读入数组。

When this has been solved, I'm likely to need to read in large amount of data, several columns, 4k rows, a mixture of text & non-integer numbers. 当这个问题得到解决后,我可能需要读取大量数据,多列,4k行,文本和非整数的混合。

Is there a format or method I should be using when the volume of data is getting to that size? 当数据量达到那么大时,我应该使用哪种格式或方法吗?

Be sure to export the data from Excel in CSV format . 请务必以CSV格式从Excel导出数据。

It's easy to use NSFileManager to access the file: 使用NSFileManager访问文件很容易:

NSString *pathName = ...;  /// fill in the file's pathname
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:pathName]) {
    // read the file contents, see below
}

For small files, you could just say: 对于小文件,您可以说:

NSString *lines = [NSString stringWithContentsOfFile:filePath];

and then proceed to split the string into lines, and process each line. 然后继续将字符串拆分为行,并处理每一行。

For large files, better have a look at some more sophisticated techniques, like in Objective-C: Reading a file line by line . 对于大型文件,最好看看一些更复杂的技术,比如在Objective-C中:逐行读取文件

As for parsing the CSV lines, use the NSString method componentsSeparatedByString to tokenize each line, something like: 至于解析CSV行,使用NSString方法componentsSeparatedByString来标记每一行,如:

NSArray *fields = [line componentsSeparatedByString:@","];

Actually, that's also what you would use to split the file contents you read into individual lines. 实际上,这也是您用来将读取的文件内容拆分为单独行的方法。 Just pay attention to the line feeds (CR LF or LF). 只需注意换行(CR LF或LF)。 You will find something in how to split a string with newlines . 您将找到有关如何使用换行符拆分字符串的内容

There is a very popular CocoaPod / Github project that both will write and parse CSV Files. 有一个非常流行的CocoaPod / Github项目,它们都将编写和解析CSV文件。 https://github.com/davedelong/CHCSVParser https://github.com/davedelong/CHCSVParser

You can find here: Where can I find a CSV to NSArray parser for Objective-C? 您可以在这里找到:在哪里可以找到针对Objective-C的NSArray解析器的CSV? quite similar problem, btw. 非常相似的问题,顺便说一句。 converting CSV to JSON should help you parsing data, since JSON could be easily converted to the NSDictionary or NSArray. 将CSV转换为JSON应该可以帮助您解析数据,因为JSON可以很容易地转换为NSDictionary或NSArray。

I use Numbers create csv and I have problem when I add "," to table. 我使用Numbers创建csv,当我向表中添加“,”时我遇到了问题。 I use some trick to fix it. 我用一些技巧来解决它。 Sorry I try to add sample code here but it too long. 对不起,我尝试在这里添加示例代码,但时间太长了。

https://github.com/LovePick/CSVParser-Swift https://github.com/LovePick/CSVParser-Swift

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM