Export GridView to Excel in ASP.NET



cheat sheet to Export GridView to Excel in ASP.NET

protected void btnExport_Click(object sender, EventArgs e)

{ 

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);
//render gridview control to excel 
        gvEmployee.RenderControl(htextw);

        Response.Write(stw.ToString());

        Response.End();

}

Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.