Search This Blog

2010/01/17

Downloading data as Excel Sheet:

C# Code Snipet:

GridView GridViewDump = new GridView(); GridViewDump.HeaderStyle.Font.Bold = true;

/*
Using Normal database connectivity code
Create dataset ds fill it using dataadapter
*/

GridViewDump.DataSource =ds;
GridViewDump.DataBind();

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Report.xls");

Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


GridViewDump.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

No comments:

Post a Comment