Export Excel I believe a lot of people have used it, but I'm having a problem. "Aborting thread"
The source code is as follows:
public static void Exportexcel (String fileName, GridView gvmain)
{
Current Conversation
System.Web.HttpContext curcontext = System.Web.HttpContext.Current;
IO for exporting and returning Excel files
System.IO.StringWriter strwriter = null;
System.Web.UI.HtmlTextWriter htmlwriter = null;
if (Gvmain.datasource! = null)
{
Setting encoding and Attachment formats
CurContext.Response.Clear ();
CurContext.Response.Buffer = true;
System.Web.HttpUtility.UrlEncode (filename, System.Text.Encoding.UTF8) function is the way Chinese file name garbled
CurContext.Response.AddHeader ("Content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode ( FileName, System.Text.Encoding.UTF8) + ". xls");
CurContext.Response.ContentType = "application/vnd.ms-excel";
Solve the content of the output is garbled problem
CurContext.Response.Write ("<meta http-equiv=content-type content=text/html;charset=utf-8>");
//export Excel files
strwriter = new System.IO.StringWriter ();
HTMLWriter = new System.Web.UI.HtmlTextWriter (strwriter);
Download to Client
Gvmain.rendercontrol (HTMLWriter);
CurContext.Response.Write (Strwriter.tostring ());
Htmlwriter.close ();
Strwriter.close ();
CurContext.Response.End ();
}
}
The data can also be exported, but the exception is thrown.
Symptoms
If you use the Response.End, Response.Redirect, or Server.Transfer methods, a ThreadAbortException exception occurs. You can use the Try-catch statement to catch this exception.
Reason
<!--Inject Script Filtered--
The Response.End method terminates the execution of the page and switches this execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.
This problem occurs in the Response.Redirect and Server.Transfer methods, because both methods call Response.End internally.
Solution Solutions
To resolve this issue, use one of the following methods: For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to skip The code execution for the Application_EndRequest event.
For Response.Redirect, use the overloaded Response.Redirect (String URL, bool endresponse), which passes false to the Endresponse parameter to cancel the Response.End Internal invocation of the. For example:
Response.Redirect ("Nextpage.aspx", false);
If you use this workaround, the code that follows Response.Redirect is executed.
For Server.Transfer, use the Server.Execute method instead.
from:http://blog.csdn.net/zhensoft163/article/details/5995146
C # export Excel "aborting thread" error