Friday, September 18, 2009

Export ASP.NET Grid to Excel

Below is the code for exporting the ASP.NET grid into Excel document


            HtmlForm form = new HtmlForm();
            string attachment = "attachment; filename=Employee.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter stw = new StringWriter();
            HtmlTextWriter htextw = new HtmlTextWriter(stw);
            form.Controls.Add(GridView1);
            this.Controls.Add(form);
            form.RenderControl(htextw);
            Response.Write(stw.ToString());
            Response.End();

1 comment:

  1. Hi Rajavel,
    I have a grid which has two levels of drill down. On clicking on the row the details view opens which inside contains one more detail view. Will this approach will work in this scenario.

    I am using Teleric Grid for this.

    Please suggest.

    Sanket

    ReplyDelete