i want to export data from multiple datatables to csv file.
i already know how to write one datatable to csv.
any help please. i have this function
public void CreateCSVFile(DataTable dt, string strFilePath)
{
try
{
StreamWriter sw = new StreamWriter(strFilePath, false);
int columnCount = dt.Columns.Count;
for (int i = 0; i < columnCount ; i++)
{
sw.Write(dt.Columns[i]);
if (i < columnCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < columnCount ; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < columnCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
catch (Exception ex)
{
throw ex;
}
}