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

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

问题描述

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

  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(@无法打开文件);

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

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



我该怎么办?我需要帮助确认什么文件类型我应该用作源,以及如何读取它到数组。



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



当数据量达到该大小时,应该使用哪种格式或方法?

解决方案

请务必在 CSV格式



很容易使用 NSFileManager 访问文件:

  NSString * pathName = ...; ///填写文件的路径名
NSFileManager * fm = [NSFileManager defaultManager];
if([fm fileExistsAtPath:pathName]){
//读取文件内容,见下面
}

对于小文件,您可以说:

  NSString * lines = [NSString stringWithContentsOfFile:filePath]; 

,然后继续将字符串拆分成行,并处理每一行。



对于大文件,更好地看看一些更复杂的技术,如 Objective-C:按行读取文件



对于解析CSV行,使用 NSString 方法 componentsSeparatedByString 来对每行进行标记,例如:

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

实际上,这也是用来将读取的文件内容拆分成单行的。只需注意换行(CR LF或LF)。您可以在中找到如何使用换行符拆分字符串。 / p>

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

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.

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?

解决方案

Be sure to export the data from Excel in CSV format.

It's easy to use NSFileManager to access the file:

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.

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

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

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

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