I want to convert my data table into this csv format.Please help me if anyone have idea how to do thatHow to convert Data table into this csv format
I want to convert my data table into this csv format.Please help me if anyone have idea how to do that
Nisha RegilRank #1,956 · 748 reputationPosted Sep 1, 2020, 5:20 AM EDT
Leon DRank #851 · 2.6K reputationPosted Sep 1, 2020, 4:11 PM EDT
Hamid KhanRank #424 · 5.9K reputationPosted Sep 2, 2020, 1:04 AM EDT
public static class DataTableExtensionsClass { public static void ConvertToCsvFile(this DataTable dataTable, string filePath) { StringBuilder fileContent = new StringBuilder(); foreach (var col in dataTable.Columns) { fileContent.Append(col.ToString() + ","); } fileContent.Replace(",", System.Environment.NewLine, fileContent.Length - 1, 1); foreach (DataRow dr in dataTable.Rows) { foreach (var column in dr.ItemArray) { fileContent.Append("\"" + column.ToString() + "\","); } fileContent.Replace(",", System.Environment.NewLine, fileContent.Length - 1, 1); } System.IO.File.WriteAllText(filePath, fileContent.ToString()); } }