I am trying to write data from datatable to cs file in c#. the problem is that i want to write datatable in new worksheet in existing csv file
I have already the function to write data from datatable to new csv file. any help please?
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;
}
}