C# extension method to convert a DataTable to a CSV string.
public static string DataTableToCSV(this DataTable datatable, char seperator)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < datatable.Columns.Count; i++)
{
sb.Append(datatable.Columns[i]);
if (i < datatable.Columns.Count - 1)
{
sb.Append(seperator);
}
}
sb.AppendLine();
foreach (DataRow dr in datatable.Rows)
{
for (int i = 0; i < datatable.Columns.Count; i++)
{
if (dr[i] is string[])
{
var list = dr[i] as string[];
var val = string.Join(";", list);
if (val.Contains(","))
{
val = "\"" + val + "\"";
}
sb.Append(val);
}
else
{
var value = dr[i].ToString();
if (value.Contains("\""))
{
value = value.Replace("\"", "'");
}
if (value.Contains(","))
{
value = "\"" + value + "\"";
}
sb.Append(value);
}
if (i < datatable.Columns.Count - 1)
{
sb.Append(seperator);
}
}
sb.AppendLine();
}
return sb.ToString();
}