Convert DataTable to CSV file
DataTable is used in. Net projects to cache data, and DataTable represents a table of data in memory. CSV file was first used in a simple database. Because of its simple format and strong openness, it was initially used as a mark for one's own atlas by scanners. The CSV file is a plain text file, with each line representing many attributes of a picture.
Using C # to convert DataTable into CSV files in .net projects now provides a more general method, as follows:
/ / convert DataTable to CSV file / DataTable / File path public static void SaveCsv (DataTable dt, string filePath) {FileStream fs = null; StreamWriter sw = null Try {fs = new FileStream (filePath + dt.TableName + ".csv", FileMode.Create, FileAccess.Write); sw = new StreamWriter (fs, Encoding.Default); var data = string.Empty; / / write the column name for (var I = 0; I < dt.Columns.Count) Sw.WriteLine +) {data + = dt.Columns [I] .ColumnName; if (I < dt.Columns.Count-1) {data + = ",";}} ColumnName (data) / / write out each row of data for (var I = 0; I < dt.Rows.Count; iTunes +) {data = string.Empty; for (var j = 0; j < dt.Columns.Count; jacks +) {data + = dt.Rows [I] [j] .ToString () If (j < dt.Columns.Count-1) {data + = ",";}} sw.WriteLine (data) } catch (IOException ex) {throw new IOException (ex.Message, ex);} finally {if (sw! = null) sw.Close (); if (fs! = null) fs.Close ();}}