Showing posts with label Excel Export. Show all posts
Showing posts with label Excel Export. Show all posts

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();