Dear Member 10868354,
I am answering thing assuming you having one DataTable, that you want to convert into CSV File.
In
DataTable we having COLUMN HEADER(COLUMN NAME) and Data Rows.
public static string CSVGenerate(DataTable table)
{
var CSVresult = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
CSVresult.Append(table.Columns[i].ColumnName);
CSVresult.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
CSVresult.Append(row[i].ToString());
CSVresult.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
}
return CSVresult.ToString();
}
table
is my datatable variable passed to
CSVGenerate method.
Please, Try & Check this.If, you having fruther any query feel free to contact.
If its your answer don't forget to mark as answer.
Thanking You,
Manoj Kalla