Wednesday, July 8, 2009

Export Gridview to xls

Now you can Export content from GridView to Excel file

1. Create one Button "Export"

<asp:Button id="Export" Text="Export" onClick="Export_Click"> </asp:Button>

2. Add this on Export Event

protected void Export_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment;filename=ExportData.xls");
Response.Charset = String.Empty;
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter SWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmWr = new HtmlTextWriter(SWrite);
GridView1.RenderControl(htmWr);
Response.Write(SWrite.ToString());
Response.End();
}


that's it

No comments:

Post a Comment