Vignesh Kumar

Vignesh Kumar

  • NA
  • 1k
  • 403.7k

Export multiple gridview to excel

Jan 20 2014 1:21 AM
Hi,

I am using two grids in a page and I want to export those two grids on export to excel. I use the below code to export a single gridview and it works fine. Can anyone suggest me on how to achieve export for two grid views.

protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename=    {0}", "Reports.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    //Change the Header Row back to white color
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
    //Applying stlye to gridview header cells
    for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
    {
        GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#aaaaaa");
    }
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}



Answers (3)