DataTable is used in .net projects to cache data, and DataTable represents a table of data in memory. CSV files were first used in simple databases. Because of their simple format and strong openness, they were initially used as markers for their own picture albums by scanning the picture family. A CSV file is a plain text file, each line represents many properties of an image.
In the .net project, C# is used to convert DataTable into CSV files. Now, a more general method is provided, with the specific code as follows:
/// <summary> /// Convert DataTable to CSV file/// </summary> /// <param name="dt">DataTable</param> /// <param name="filePath">File Path</param>public static void SaveCsv(DataTable dt, string filePath) { FileStream fs = null; StreamWriter sw = null; try { fs = new FileStream(filePath + + ".csv", , ); sw = new StreamWriter(fs, ); var data = ; // Write out the column namefor (var i = 0; i < ; i++) { data += [i].ColumnName; if (i < - 1) { data += ","; } } (data); //Write out each line of datafor (var i = 0; i < ; i++) { data =; for (var j = 0; j < ; j++) { data += [i][j].ToString(); if (j < - 1) { data += ","; } } (data); } } catch (IOException ex) { throw new IOException(, ex); } finally { if (sw != null) (); if (fs != null) (); } }
The above is the method of converting DataTable into CSV files in C# introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!