Vivek Jagga
Vivek Jagga

Reputation: 502

Export To excel in Asp.Net

I have button on my page for" Export To Excel". The ButtonClick function is to export the datgrid (dgrISGrid) to excel Code is attached below: But while executing its throwing error as"thread is aborted".Whats the solution?

    protected void imgbtnExport_Click(object sender, ImageClickEventArgs e)
    {
      try
      {

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=InformationSystems.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);
        dgrISGrid.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
      }
      catch (Exception ex)
      {

        ExceptionHandler ObjExceptionHandler = new ExceptionHandler();
        lblError.Text = ObjExceptionHandler.GetExceptionDetails(ex);
      }
    }

Upvotes: 1

Views: 386

Answers (1)

Saar
Saar

Reputation: 8484

Response.End() causes this error.

Try using "Response.Flush()" just before this statement.

Upvotes: 1

Related Questions