DataTable to CSV, datatablecsv

Source: Internet
Author: User

DataTable to CSV, datatablecsv

Direct Method:

1 /// <summary> 2 /// convert DataTable to CSV file 3 /// </summary> 4 /// <param name = "dt"> DataTable </ param> 5 // <param name = "filePath"> file path </param> 6 public static void SaveCSV (DataTable dt, string filePath) 7 {8 FileStream fs = new FileStream (filePath + dt. tableName + ". csv ", System. IO. fileMode. create, System. IO. fileAccess. write); 9 StreamWriter sw = new StreamWriter (fs, System. text. encoding. default); 10 string data = ""; 11 12 // write the column name 13 for (int I = 0; I <dt. columns. count; I ++) 14 {15 data + = dt. columns [I]. columnName. toString (); 16 if (I <dt. columns. count-1) 17 {18 data + = ","; 19} 20} 21 sw. writeLine (data); 22 23 // write all rows of data 24 for (int I = 0; I <dt. rows. count; I ++) 25 {26 data = ""; 27 for (int j = 0; j <dt. columns. count; j ++) 28 {29 data + = dt. rows [I] [j]. toString (); 30 if (j <dt. columns. count-1) 31 {32 data + = ","; 33} 34} 35 sw. writeLine (data); 36} 37 sw. close (); 38 fs. close (); 39}

 

 

 

  

Related Article

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.