Have you tried this?
protected void ExportToExcel(DataTable dataTable, bool exportMode, string fileName)
{
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gv = new GridView();
gv.ShowHeader = false;
gv.DataSource = dataTable;
gv.DataBind();
for (int I = 0; I < dataTable.Rows.Count; I++)
{
for (int iCol = 0; iCol < dataTable.Columns.Count; iCol++)
{
if (dataTable.Rows[I][iCol].ToString().Length < 256)
{
gv.Rows[I].Cells[Convert.ToInt32(iCol)].Attributes.Add("class", "text");
}
}
}
dataTable = null;
if (gv.Rows.Count == 0)
{
gv.ShowHeaderWhenEmpty = true;
gv.EmptyDataText = "No Data !"
}
else
{
Response.Clear();
Response.Charset = "utf-8";
Response.ContentType = "application/vnd.ms-excel";
if (exportMode)
{
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName.Replace(" ", "_") + "\"");
}
gv.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
htmlWrite.Dispose();
stringWrite.Dispose();
gv.Dispose();
}
Hope that helps