主要介紹DataTable、日誌Log、檔案流FileStream、StreamWriter類,將資料儲存為txt和excel格式。
1.建立TestDataTable類
CreateTable()方法建立一個資料table,SaveTableToExcel(string fileName)方法將資料table存為檔案名稱為fileName的excel格式檔案,CreateDirectory(string fileName)方法去檢查檔案是否存在,若不存在,則自動建立一個。
class TestDataTable: DataTable { public void CreateTable() { this.Columns.Add("Time(s)", System.Type.GetType("System.String")); this.Columns.Add("Ch", System.Type.GetType("System.String")); this.Columns.Add("BER", System.Type.GetType("System.String")); const int length1 = 4; const int length2 = 10; int[][] data = new int[length1][]; //第一條資料 data[0] = new int[length2] { 13, 25, 21, 33, 28, 39, 43, 36, 42, 36 }; //第二條資料 data[1] = new int[length2] { 20, 13, 10, 5, 15, 7, 10, 14, 19, 20 }; //第三條資料 data[2] = new int[length2] { 78, 92, 65, 83, 90, 59, 63, 72, 88, 98 }; //第四條資料 data[3] = new int[length2] { 45, 49, 39, 47, 52, 76, 67, 51, 57, 67 }; for (int i = 0; i < length2; i++) { for (int j = 0; j < length1; j++) { DataRow dr = this.NewRow(); dr[0] = i + 1; dr[1] = j; dr[2] = data[j][i]; this.Rows.Add(dr); } } } public void SaveTableToExcel(string fileName) { CreateDirectory(fileName); StringBuilder title = new StringBuilder(); FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(new BufferedStream(fileStream), System.Text.Encoding.Default); for (int i = 0; i < this.Columns.Count; i++) { title.Append(this.Columns[i].ColumnName + "\t"); //欄位:自動跳到下一儲存格 } title.Append("\n"); writer.Write(title); foreach (DataRow row in this.Rows) { StringBuilder content = new StringBuilder(); for (int i = 0; i < this.Columns.Count; i++) { content.Append(row[i] + "\t");//內容:自動跳到下一儲存格 } content.Append("\n"); writer.Write(content); } writer.Close(); fileStream.Close(); } public void CreateDirectory(string fileName) { DirectoryInfo directoryInfo = Directory.GetParent(fileName); if (!directoryInfo.Exists) { directoryInfo.Create(); } } }
2.建立日誌Log類
將檔案名稱FileName定義為類的屬性,在建構函式時賦值。SaveLogToTxt(string info)方法將資料info存入檔案名稱為fileName的txt檔案中。同樣CreateDirectory()方法去檢查檔案是否存在
class Log { private string fileName; public string FileName { set { this.fileName = value; } get { return this.fileName; } } public Log(string fileName) { this.fileName = fileName; CreateDirectory(); } public void SaveLogToTxt(string info) { StreamWriter writer = null; FileStream fileStream = null; try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(this.fileName); if (!fileInfo.Exists) { fileStream = fileInfo.Create(); writer = new StreamWriter(fileStream); } else { fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write); writer = new StreamWriter(fileStream); } writer.WriteLine(info); } finally { if (writer != null) { writer.Close(); writer.Dispose(); fileStream.Close(); fileStream.Dispose(); } } } public void CreateDirectory() { DirectoryInfo directoryInfo = Directory.GetParent(this.fileName); if (!directoryInfo.Exists) { directoryInfo.Create(); } } }
3.調用類方法存資料
執行個體化類、建立資料table,將table儲存為txt格式和excel格式,以時間命名。
TestDataTable dt = new TestDataTable();dt.CreateTable();dt.Rows.Clear(); //清除資料table裡所有行DateTime startTime = DateTime.Now;Log log = new Log(@"Log\" + startTime.ToString("yyyyMMddHHmmss") + ".txt");//以時間為檔案命名,存在exe所在檔案夾下的Log檔案夾裡for (int i = 0; i < 4; i++){ DataRow dr = dt.NewRow(); dr[0] = (int)second; dr[1] = i; dr[2] = BERByChannel[i].ToString("E2"); dt.Rows.Add(dr);//添加新行到資料table string info = string.Format("{0}: Time(s): {1}, Ch: {2}, BER: {3}", DateTime.Now, dr[0], dr[1], dr[2]); log.SaveLogToTxt(info);//存為txt格式 }//存為excel格式string fileName = @"TestRecord\" + startTime.ToString("yyyyMMddHHmmss") + ".xls";dt.SaveTableToExcel(fileName);//以時間為檔案命名,存在exe所在檔案夾下的TestRecord檔案夾裡
以上就是 從0自學C#03--檔案流儲存資料為txt/excel格式的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!