Here is my code to generate xls file from gridview
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Contacts.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
Grv_Data.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < Grv_Data.HeaderRow.Cells.Count; i++)
{
Grv_Data.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
Grv_Data.HeaderRow.Cells[i].Style.Add("height", "20");
}
int j = 1;
foreach (GridViewRow gvrow in Grv_Data.Rows)
{
gvrow.BackColor = System.Drawing.Color.White;
if (j <= Grv_Data.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
Grv_Data.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
Hope this helps a lot...
Enjoy Coding...