I have a data table in C#.Net.Please help me to write Datatable Content to CSV File in Asp.Net(C# language)
Asked
Active
Viewed 152 times
-3
-
http://stackoverflow.com/questions/4959722/c-sharp-datatable-to-csv – Sunil Gudivada Apr 18 '14 at 11:00
-
4This question appears to be off-topic because it show absolutely no effort. – SchmitzIT May 06 '14 at 11:57
1 Answers
2
You can try this code:
var sb = new StringBuilder();
var columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
var fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText("YOURFILE.csv", sb.ToString());

user3548641
- 21
- 2