ThreadAbortException in ASP.NET application

When using GemBox.Spreadsheet in ASP.NET application, a common task that one can require is downloading of spreadsheet files. To do this, GemBox.Spreadsheet provides overload Save methods that can be used for direct streaming of spreadsheet files to a client's browser.

If we place a breakpoint inside a catch block and run this "btnDowload_Click" method, we'll notice that the ThreadAbortException is thrown at execution, even though we have successfully downloaded an "Output.xlsx" file.

The reason for this is that those Save overload methods that are used to stream a file to a client's browser will call a Response.End at the end of their execution. The following code is somewhat similar to what that Save method does:

// Unlike with other formats, with XLSX we cannot write directly to Response.OutputStream.// This is because MS Packaging API does not allow this, so we use a temporary MemoryStream.var ms = new System.IO.MemoryStream();workbook.Save(ms, options);ms.WriteTo(response.OutputStream);

' Unlike with other formats, with XLSX we cannot write directly to Response.OutputStream.' This is because MS Packaging API does not allow this, so we use a temporary MemoryStream.Dim ms = New System.IO.MemoryStream()workbook.Save(ms, options)ms.WriteTo(response.OutputStream)

response.End()

The last executed method is a Response.End that is used to immediately terminate the response after a file download. By nature, it throws a ThreadAbortException; you can read about this in the Resonse.End method's remarks.

So what we can do, is either ignore the catching of a ThreadAbortException type of exception when calling a GemBox.Spreadsheet's Save method, or use an alternative approach to stream the file. For example: