Exporting a CSV file using Npoi import in C #

Source: Internet
Author: User

1. First (download DLL Npoi official website)

Add Reference:NPOI.dll,npoi. OOXML.dll,npoi. OpenXml4Net.dll,npoi. OpenXmlFormats.dll

2. Code--csvhelper

1      Public classCsvhelper2     {3         /// <summary>4         ///Write to CSV5         /// </summary>6         /// <param name= "FileName" >file name</param>7         /// <param name= "DT" >the DataTable to write to</param>8          Public Static voidWritecsv (stringfilename,datatable DT)9         {Ten FileStream FS; One StreamWriter SW; A             stringdata =NULL; -  -             //determines whether the file exists, does not write the column name again the             if(!file.exists (fileName)) -             { -FS =NewFileStream (FileName, FileMode.Create, FileAccess.Write); -SW =NewStreamWriter (FS, Encoding.UTF8); +  -                 //write out the column name +                  for(inti =0; i < dt. Columns.count; i++) A                 { atData + =dt. Columns[i]. Columnname.tostring (); -                     if(I < dt. Columns.count-1) -                     { -Data + =",";//in the middle, separated -                     } -                 } in SW. WriteLine (data); -             } to             Else +             { -FS =NewFileStream (FileName, Filemode.append, FileAccess.Write); theSW =NewStreamWriter (FS, Encoding.UTF8); *             } $             Panax Notoginseng             //write each row of data -              for(inti =0; i < dt. Rows.Count; i++) the             { +data =NULL; A                  for(intj =0; J < dt. Columns.count; J + +) the                 { +Data + =dt. ROWS[I][J]. ToString (); -                     if(J < dt. Columns.count-1) $                     { $Data + =",";//in the middle, separated -                     } -                 } the SW. WriteLine (data); -             } Wuyi SW. Close (); the FS. Close ();  -         } Wu  -  About  $         /// <summary> -         ///Read CSV file -         /// </summary> -         /// <param name= "FileName" >file path</param> A          Public StaticDataTable Readcsv (stringfileName) +         { theDataTable dt =NewDataTable (); -FileStream fs =NewFileStream (FileName, FileMode.Open, FileAccess.Read); $StreamReader sr =NewStreamReader (FS, Encoding.UTF8); the  the             //record a row of records per read the             stringStrLine =NULL; the             //record the contents of each field in each row of records -             string[] arrayline=NULL; in             //Separators the             string[] separators = {"," }; the             //judge, if for the first time, create a table header About             BOOLIsFirst =true; the  the             //Read the CSV file line by row the              while((StrLine = Sr.) ReadLine ())! =NULL) +             { -StrLine = Strline.trim ();//Remove Kinsoku spaces theArrayline = Strline.split (separators, stringsplitoptions.removeemptyentries);//separating strings, returning arraysBayi                 intDtcolumns = Arrayline.length;//Number of columns the  the                 if(IsFirst)//Create a table header -                 { -                      for(inti =0; i < dtcolumns; i++) the                     { theDt. Columns.Add (Arrayline[i]);//each column name the                     } the                 } -                 Else   //Table Content the                 { theDataRow datarow = dt. NewRow ();//New Row the                      for(intj =0; J < Dtcolumns; J + +)94                     { theDATAROW[J] =Arrayline[j]; the                     } theDt. Rows.Add (DataRow);//Add a row98                 } About             } - Sr. Close ();101 FS. Close ();102 103             returnDT;104}

Exporting a CSV file using Npoi import in C #

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.