Home > Code Samples > Export DataTable to Excel in C#

Export DataTable to Excel



C#
DataTable products = new DataTable();
...
Response.Clear();
Response.ClearHeaders();
using (CsvWriter writer = new CsvWriter(Response.OutputStream, ',', Encoding.Default))
{
	writer.WriteAll(products, true);
}
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=products.csv");
Response.End();