主要介紹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格式的內容,更多相關內容請關注PHP中文網(www.php.cn)!