Home > Article > Backend Development > Self-study C#03 from 0--File stream saves data in txt/excel format
Mainly introduces the DataTable, log Log, file stream FileStream, and StreamWriter classes to save data in txt and excel formats.
1. Create the TestDataTable class
CreateTable() method creates a data table, SaveTableToExcel(string fileName) method saves the data table as an excel format file named fileName, CreateDirectory(string fileName) ) method to check whether the file exists. If it does not exist, it will automatically create one.
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. Create a log Log class
Define the file name FileName as an attribute of the class and assign it in the constructor. The SaveLogToTxt(string info) method saves the data info into a txt file named fileName. Same CreateDirectory() method to check whether the file exists
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. Call the class method to save data
Instantiate the class, create a data table, and save the table in txt format and excel format, with time name.
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文件夹里
The above is the content of self-study C#03 from 0--file stream saving data in txt/excel format. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!