Search This Blog

Monday, July 26, 2010

Export To Excel

public void GenerateExcelReport()
{
HttpContext.Current.Response.Clear();
this.GridView1.AllowPaging = false;
this.GridView1.AllowSorting = false;
this.GridView1.DataSource = GetCustomMadeDataTable();
this.GridView1.DataBind();
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("
Training History Report
");

HttpContext.Current.Response.Write("");
System.IO.
StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=TrainingHistoryReport.xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.xls";
// Create a form to contain the grid
System.Web.UI.HtmlControls.
HtmlForm frm = new System.Web.UI.HtmlControls.HtmlForm();
this.GridView1.Parent.Controls.Add(frm);
frm.Attributes[
"runat"] = "server";
frm.Controls.Add(
this.GridView1);
frm.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.End();

}

No comments:

Post a Comment