C # Save the data as CSV files and Excel files,

Source: Internet
Author: User

C # Save the data as CSV files and Excel files,

1 public void WriteData () 2 {3 try 4 {5 if (System. IO. directory. exists (DataFileRootPath) = false) 6 {7 System. IO. directory. createDirectory (DataFileRootPath); 8} 9 StringBuilder DataColumn = new StringBuilder (); 10 StringBuilder DataLine = new StringBuilder (); 11 12 string strT = DateTime. now. year. toString () + "-" + DateTime. now. month. toString () + "-" + DateTime. now. day. toString () + "_" + DateTime. now. hour. toString () + "-" + DateTime. now. minute. toString () + "-" + DateTime. now. second. toString () + "-" + DateTime. now. millisecond. toString (); 13 14 // column title 15 DataColumn. append ("DateTime,"); 16 // row data 17 DataLine. append (strT + ","); 18 19 20 21 22 string FileName = DateTime. now. year. toString () + "-" + DateTime. now. month. toString () + "-" + DateTime. now. day. toString (); 23 string FilePath = DataFileRootPath + "\" + FileName + ". CSV "; 24 25 if (System. IO. file. exists (FilePath) = false) 26 {27 System. IO. streamWriter stream = new System. IO. streamWriter (FilePath, false, Encoding. UTF8); 28 stream. writeLine (DataColumn); 29 stream. writeLine (DataLine); 30 stream. flush (); 31 stream. close (); 32 stream. dispose (); 33} 34 else35 {36 System. IO. streamWriter stream = new System. IO. streamWriter (FilePath, true, Encoding. UTF8); 37 stream. writeLine (DataLine); 38 stream. flush (); 39 stream. close (); 40 stream. dispose (); 41} 42} 43 catch (Exception ex) 44 {45 46} 47}
View Code

 

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.