CSV file interpretation code

Source: Internet
Author: User

CSV document Rules
1: Data is separated by commas;
2: The content can contain commas (,) used to separate data. Such data is enclosed by double quotation marks;
3: if the data contains double quotation marks, two double quotation marks are required;

The Code does not use functions such as remove and substring to process the source string, but uses indexes to locate the string value.

The following code stores the data interpreted by the CSV file in the able for use. The calculation activity diagram is attached.

 

/// <Summary> /// CSV file description /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void btncallwfp_click (Object sender, eventargs e) {filestream = new filestream (@ "D: \ temp \ a.csv", filemode. open, fileaccess. read, fileshare. none); // identifies whether the read data is the first row. The first row lists bool columnflag = true; // defines the new data row string strline = NULL; datatable table = new datatable (); Using (streamreader Mysr = new streamreader (filestream, system. Text. encoding. getencoding ("gb2312") {While (! Mysr. endofstream) {strline = mysr. Readline (); If (! String. isnullorempty (strline) {// lihx may April 7, 2013 is actually a problem here, because when "," is included in the CSV file, the comma is not interpreted as a separator, for example: "" 2-post office remittance, "" // read the first line, as the column name if (columnflag) {columnflag = false; int linestartindex = 0; while (linestartindex <strline. length) {table. columns. add (getspvalue (ref linestartindex, ref strline) ;}} else {// initialize a new row and put int itemindex = 0 in the table; int linestartindex = 0; object [] items = new object [table. CO Lumns. count]; while (linestartindex <strline. length & itemindex <items. length) {items [itemindex ++] = getspvalue (ref linestartindex, ref strline);} table. rows. add (items) ;}}}} Private Static string getspvalue (ref int linestartindex, ref string strline) {int spindex = linestartindex; if (strline [linestartindex] = '\ "') {do {spindex = strline. indexof ("\" ", spindex + 1); If (spindex =- 1) {spindex = strline. length-1 ;}}while (++ spindex <strline. length & strline [spindex] = '\ "');} spindex = strline. indexof (",", spindex); If (spindex =-1) {spindex = strline. length;} If (linestartindex>-1 & linestartindex <strline. length) {string tmpvalue = strline. substring (linestartindex, spindex-linestartindex); If (tmpvalue! = NULL & tmpvalue. length> 1 & tmpvalue. startswith ("\" ") & tmpvalue. endswith ("\" ") {tmpvalue = tmpvalue. substring (1, tmpvalue. length-2); If (tmpvalue. length> 0) {tmpvalue = tmpvalue. replace ("\" \ "", "\" ") ;}// the following string truncation algorithms are not recommended considering the performance. // Strline = strline. substring (spindex + 1); linestartindex = spindex + 1; return tmpvalue;} linestartindex = spindex + 1; return NULL ;}

 

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.