Ios-parse to read CSV file, parse Excel file

Source: Internet
Author: User
Tags processing text

The project may encounter data exported from the database in CSV format, similar to the following:

You need to import the CSV data into your program or write it to a local database file.

* What is CSV?
CSV, which is a comma-separated value (comma-separated values). Sometimes also called character-delimited values, because delimited characters can also be not commas, which can be semicolons, and their files store tabular data (numbers and text) in plain text.
This file format is often used as a format for data interaction between different programs.
CSV format data is structured like a table, with different records occupying one row, and the fields in a row separated by "," (comma).
In Xcode, a file in CSV format is a small-footprint text document that features:

 (1 ) is not left blank in the behavior unit. (2 ) takes one row per record, with a comma delimiter.    The column is empty to express its existence.    (3 ) can contain or not contain column names, and if a column name is included, the first line of the file.    (4 ) row of data does not span rows, no blank lines.    (5 ) field contains a comma character, which must be enclosed in double quotation marks.    (The 6 ) field contains a newline character, which must be enclosed in double quotation marks. (7 ) contains spaces before and after the field, which must be enclosed in double quotation marks. (a b C ==>  "a b C" ) (8 ) The double quotation marks in the field, expressed in two double quotation marks. (I said:  "abc" .) ==> I said:  abc. ) (9 ) if there are double quotes in the field, the field must be enclosed in double quotation marks. (I said:  "abc" .) ==>  "I said:"   "abc"  . " 

After a simple understanding of the CSV file, the programmer is to parse the CSV into memory:
In iOS, you can parse by using the following classes to parse it into an Nsarray array:

+(Nsarray*) readcsvdata{Nsmutablearray*_infoarray;if(_infoarray) {return NULL; } _infoarray=[[NsmutablearrayAlloc]init];NSString*filepath=[[NSBundleMainbundle] pathforresource:@"Employee Table"oftype:@"CSV"]; FILE *fp=fopen ([filepath utf8string],"R");if(FP) {CharBuf[bufsiz]; Fgets (buf, Bufsiz, FP); while(!feof (FP)) {CharBuf[bufsiz]; Fgets (buf, Bufsiz, FP);//Processing text information to convert an array of files            NSString*s=[[NSStringAlloc]initwithutf8string: (Const Char*) BUF];NSString*ss=[s stringbyreplacingoccurrencesofstring:@"\ r"withstring:@""]; Ss=[ss stringbyreplacingoccurrencesofstring:@"\ n"withstring:@""];Nsarray*a=[ss componentsseparatedbystring:@","];        [_infoarray addobject:a]; }    }NSLog(@"%@", _infoarray);return_infoarray;}

The printout information is the data that the programmer needs!

Ios-parse to read CSV file, parse Excel file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.