I am trying to export Gridview Data to Excel. I searched a lot and got the code to export to excel. But excel not showing. When using Response.End() a threadexception comes so i used HttpContext.Current.ApplicationInstance.CompleteRequest() instead of Response.End()
Please help me find out a Solution.
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gridSalary.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
// Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
The below is a working example
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=file-name.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
Note : There is a function VerifyRenderingInServerForm
which is overridden. And i guess you missed it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.