In this article I will explain how you can export your GridView control to Excel files.
The following code executes on a button click event.
protected void btnExcel_Click(object sender, EventArgs e)
The following code executes on a button click event.
protected void btnExcel_Click(object sender, EventArgs e)
{
//ExportToExcel();
gvTDefaulters.AllowPaging = false;
gvTDefaulters.AllowSorting = false;
gvTDefaulters.EditIndex = -1;
this.BindData(); // Populate Gridview with data
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
HtmlForm frm = new HtmlForm();
this.gvTDefaulters.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(this.gvTDefaulters);
frm.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.End();
}
Now if you try to export your GridView to excel it will work fine.
Now if you try to export your GridView to excel it will work fine.
1 comment:
It's a very nice blog.
Thanks for sharing
Post a Comment