Well, try this. Here is the full source to export datatable to excel
public void ExportToExcel(DataTable data, string fileName)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
for (int i = 0; i < data.Columns.Count; i++)
{
if (i > 0)
{
context.Response.Write(",");
}
context.Response.Write(data.Columns[i].ColumnName);
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in data.Rows)
{
for (int i = 0; i < data.Columns.Count; i++)
{
if (i > 0)
{
context.Response.Write(",");
}
context.Response.Write(row.ItemArray[i].ToString());
}
context.Response.Write(Environment.NewLine);
}
context.Response.End();
}
private DataTable getData()
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel(getData(), "[customefilename]");
}
Hope this helps.
cheers