从xls /读取数据。csv文件到iOS

时间:2023-01-15 11:36:25

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:

我是iOS的新手,我正在尝试将电子表格的内容读入iOS数组。我使用的电子表格是一个简单的3×2的数字数组,第一列是数字,第二列是文本。我试过在。xls .xlsx .中使用&不使用列标题阅读它。cdv .txt (unicode和分隔符),但没有成功。这个文件叫做“funds”,我使用的代码是:

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?

当数据量达到这个大小时,是否应该使用某种格式或方法?

5 个解决方案

#1


5  

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方法componentsseparation bystring来标记每一行,如下所示:

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.

实际上,这也是您将读取的文件内容拆分为单独的行的方法。只需关注线源(CR LF或LF)。您将发现如何使用换行符分割字符串。

#2


3  

There is a very popular CocoaPod / Github project that both will write and parse CSV Files. https://github.com/davedelong/CHCSVParser

有一个非常流行的CocoaPod / Github项目,它可以同时编写和解析CSV文件。https://github.com/davedelong/CHCSVParser

#3


0  

You can find here: Where can I find a CSV to NSArray parser for Objective-C? quite similar problem, btw. converting CSV to JSON should help you parsing data, since JSON could be easily converted to the NSDictionary or NSArray.

您可以在这里找到:在哪里可以找到针对Objective-C的CSV解析器?非常相似的问题,顺便说一句。将CSV转换为JSON应该可以帮助您解析数据,因为JSON可以很容易地转换为NSDictionary或NSArray。

#4


-1  

I use Numbers create csv and I have problem when I add "," to table. I use some trick to fix it. Sorry I try to add sample code here but it too long.

我使用数字创建csv,当我添加“,”到表时,我有问题。我用了一些技巧来修理它。抱歉,我尝试在这里添加示例代码,但是太长了。

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

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

#5


-1  

There is a library to handle Xlsx files in iOS. Its written in Objective-C but you can also use it with Swift using bridging header. XlxsReaderWriter on GitHub You can read a Xlsx file using

在iOS中有一个用于处理Xlsx文件的库。它是用Objective-C写的,但是你也可以使用Swift的桥接头。您可以使用GitHub上的xlsreaderwriter来读取Xlsx文件

let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(outPath.path)
let workSheet : BRAWorksheet=spreadsheet.workbook.worksheets?[0] as! BRAWorksheet;

I thought it might help someone, like me, searching for reading xlsx files.

我认为它可能会帮助像我这样的人,搜索xlsx文件。

#1


5  

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方法componentsseparation bystring来标记每一行,如下所示:

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.

实际上,这也是您将读取的文件内容拆分为单独的行的方法。只需关注线源(CR LF或LF)。您将发现如何使用换行符分割字符串。

#2


3  

There is a very popular CocoaPod / Github project that both will write and parse CSV Files. https://github.com/davedelong/CHCSVParser

有一个非常流行的CocoaPod / Github项目,它可以同时编写和解析CSV文件。https://github.com/davedelong/CHCSVParser

#3


0  

You can find here: Where can I find a CSV to NSArray parser for Objective-C? quite similar problem, btw. converting CSV to JSON should help you parsing data, since JSON could be easily converted to the NSDictionary or NSArray.

您可以在这里找到:在哪里可以找到针对Objective-C的CSV解析器?非常相似的问题,顺便说一句。将CSV转换为JSON应该可以帮助您解析数据,因为JSON可以很容易地转换为NSDictionary或NSArray。

#4


-1  

I use Numbers create csv and I have problem when I add "," to table. I use some trick to fix it. Sorry I try to add sample code here but it too long.

我使用数字创建csv,当我添加“,”到表时,我有问题。我用了一些技巧来修理它。抱歉,我尝试在这里添加示例代码,但是太长了。

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

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

#5


-1  

There is a library to handle Xlsx files in iOS. Its written in Objective-C but you can also use it with Swift using bridging header. XlxsReaderWriter on GitHub You can read a Xlsx file using

在iOS中有一个用于处理Xlsx文件的库。它是用Objective-C写的,但是你也可以使用Swift的桥接头。您可以使用GitHub上的xlsreaderwriter来读取Xlsx文件

let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(outPath.path)
let workSheet : BRAWorksheet=spreadsheet.workbook.worksheets?[0] as! BRAWorksheet;

I thought it might help someone, like me, searching for reading xlsx files.

我认为它可能会帮助像我这样的人,搜索xlsx文件。