将数据从.xls / .csv文件读入iOS [英] Reading data from .xls/.csv files into iOS

查看:208
本文介绍了将数据从.xls / .csv文件读入iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS的新手,正在尝试将电子表格的内容读入iOS阵列。
我使用的电子表格是一个简单的3 x 2数组数组,第一列和第二列的文本。我已经尝试阅读它与&没有列标题,在.xls,.xlsx,。 cdv,.txt(unicode和delimited),但没有成功。该文件称为资金,我使用的代码是:

I'm new to iOS and am trying to read the contents of a spreadsheet into an iOS array. The spreadsheet that I'm using is a simple 3 x 2 array of numbers for the first column and text for the second. I've tried reading it in with & without column headers, in .xls, .xlsx, . cdv, .txt (unicode and delimited) but without success. 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];

它找到文件的目录,但放弃在 [NSFileHandle fileHandleForReadingAtPath: FILEDIR]

It finds the directory of the file but gives up at [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.

当这已经解决了,我很可能需要读取大量的数据,几列,4k行,文本&非整数。

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.

当数据量达到该大小时,是否有格式或方法?

Is there a format or method I should be using when the volume of data is getting to that size?

推荐答案

确保从 CSV格式的Excel导出数据

很容易使用 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.

对于大型文件,请查看一些更复杂的技术,如 Objective-C:逐行阅读文件

For large files, better have a look at some more sophisticated techniques, like in Objective-C: Reading a file line by line.

对于解析CSV行,使用 NSString 方法 componentsSeparatedByString 来标记每一行,如下所示:

As for parsing the CSV lines, use the NSString method componentsSeparatedByString to tokenize each line, something like:

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

实际上,这也是您将用于将您读取的文件内容拆分为单独行的方式。只要注意换行(CR LF或LF)。您将在如何使用换行符分割字符串找到某些内容。

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). You will find something in how to split a string with newlines.

这篇关于将数据从.xls / .csv文件读入iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆