Saturday, April 24, 2010

ASP.NET | GridView Tips

Extract GridView data to Xls file:

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

No comments:

Post a Comment