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