Saturday, July 25, 2009

Export data in excel from gridview

Here is the comple code to generate data in excel from gridview....


protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("file.xls", "excelsheet"));

System.IO.StringWriter stringwriter = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(stringwriter);
GridView1.RenderControl(hw);
Response.Write(stringwriter.ToString());
Response.End();

}

No comments:

Post a Comment