ThreadAbortException when using ExcelFile.Save

Using the latest version of GemBox installed from NuGet.

Here is the error:

Error

{“Thread was being aborted.”}
Data: {System.Collections.ListDictionaryInternal}
ExceptionState: Unable to evaluate expression because the code is optimized or a native frame is
on top of the call stack.
HResult: -2146233040
HelpLink: Nothing
InnerException: Nothing
Message: “Thread was being aborted.”
Source: “mscorlib”
StackTrace:
" at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)" & vbCrLf &
" at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)" & vbCrLf & "
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)" & vbCrLf &
" at " & ChrW(3) & " ." & ChrW(2) & "[TSaveOptions](Object " & ChrW(2) & ", String " & ChrW(3) & ", TSaveOptions " & ChrW(5) & ", String " & vbBack & “, Boolean " & ChrW(6) & “, Action`2 " & ChrW(14) & “)” & vbCrLf &
" at GemBox.Spreadsheet.ExcelFile.Save(Object httpResponse, String fileName, SaveOptions options)” & vbCrLf &
" at Admin_Registrations_RegistrationList.btnExportList_Click(Object sender, EventArgs e) in C:\DevOps\IDMI\UHEMS\EventRegistration\Main\Webroot\Admin\Registrations\RegistrationList.aspx.vb:line 184”
TargetSite: {System.Object InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)}

The code that I’m using is the following:

Protected Sub btnExportList_Click(sender As Object, e As EventArgs) Handles btnExportList.Click
    Dim objExcel As ExcelFile
    Dim dset As DataSet

    objExcel = ExcelFile.Load(MapPath(ConfigurationManager.AppSettings("AdministrationPath") & "/Assets/Registration_Export.xlsx"))

    Dim xlSheet As ExcelWorksheet = objExcel.Worksheets(0)

    Dim idxRow As Integer = 1
    Dim csCenter As CellStyle = New CellStyle
    Dim csCurrency As CellStyle = New CellStyle
    Dim csCategory As CellStyle = New CellStyle

    csCenter.HorizontalAlignment = HorizontalAlign.Center
    csCurrency.HorizontalAlignment = HorizontalAlign.Right

    dset = RegistrationTools.GetList(ddlEvents.SelectedValue)
    Dim objEvent As New NewEvent(ddlEvents.SelectedValue)

    For Each row As DataRow In dset.Tables(0).Rows
        xlSheet.Cells(idxRow, 0).Value = ddlLocations.SelectedItem.Text
        xlSheet.Cells(idxRow, 1).Value = objEvent.EventStartDate.ToShortDateString
        xlSheet.Cells(idxRow, 2).Value = objEvent.EventName
        xlSheet.Cells(idxRow, 3).Value = row("RegistrationDate").ToString
        xlSheet.Cells(idxRow, 4).Value = row("FirstName").ToString & " " & row("LastName").ToString
        xlSheet.Cells(idxRow, 5).Value = ""
        xlSheet.Cells(idxRow, 6).Value = ""
        xlSheet.Cells(idxRow, 7).Value = ""
        xlSheet.Cells(idxRow, 8).Value = ""
        xlSheet.Cells(idxRow, 9).Value = ""
        xlSheet.Cells(idxRow, 10).Value = ""
        xlSheet.Cells(idxRow, 11).Value = ""
        xlSheet.Cells(idxRow, 12).Value = ""
        xlSheet.Cells(idxRow, 13).Value = ""
        xlSheet.Cells(idxRow, 14).Value = ""
        xlSheet.Cells(idxRow, 15).Value = ""
        xlSheet.Cells(idxRow, 16).Value = ""
        xlSheet.Cells(idxRow, 17).Value = ""

        If Not Convert.IsDBNull(row("EMailAddress")) Then xlSheet.Cells(idxRow, 5).Value = row("EMailAddress").ToString
        If Not Convert.IsDBNull(row("AddressLine1")) Then xlSheet.Cells(idxRow, 6).Value = row("AddressLine1").ToString
        If Not Convert.IsDBNull(row("AddressLine2")) Then xlSheet.Cells(idxRow, 7).Value = row("AddressLine2").ToString
        If Not Convert.IsDBNull(row("AddressCity")) Then xlSheet.Cells(idxRow, 8).Value = row("AddressCity").ToString
        If Not Convert.IsDBNull(row("AddressState")) Then xlSheet.Cells(idxRow, 9).Value = row("AddressState").ToString
        If Not Convert.IsDBNull(row("AddressZipCode")) Then xlSheet.Cells(idxRow, 10).Value = row("AddressZipCode").ToString
        If Not Convert.IsDBNull(row("PhoneHome")) Then xlSheet.Cells(idxRow, 11).Value = row("PhoneHome").ToString
        If Not Convert.IsDBNull(row("PhoneWork")) Then xlSheet.Cells(idxRow, 12).Value = row("PhoneWork").ToString
        If Not Convert.IsDBNull(row("PlaceOfEmployment")) Then xlSheet.Cells(idxRow, 13).Value = row("PlaceOfEmployment").ToString
        If Not Convert.IsDBNull(row("Department")) Then xlSheet.Cells(idxRow, 14).Value = row("Department").ToString
        If Not Convert.IsDBNull(row("CertificationExpiryBCLS")) Then xlSheet.Cells(idxRow, 15).Value = row("CertificationExpiryBCLS").ToString
        If Not Convert.IsDBNull(row("CertificationExpiryACLS")) Then xlSheet.Cells(idxRow, 16).Value = row("CertificationExpiryACLS").ToString
        If Not Convert.IsDBNull(row("AccountNumber")) Then xlSheet.Cells(idxRow, 17).Value = row("AccountNumber").ToString

        idxRow += 1
    Next

    Response.Clear()
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Try
        objExcel.Save(HttpContext.Current.Response, "Registration_Export.xlsx", XlsSaveOptions.XlsxDefault)
        'objExcel.Save("Registration_Export.xlsx")  -- i tried this and didn't work either
    Catch ex As Exception
        Dim int As Integer = 1 -- this is just so I could catch the error
    End Try
End Sub

I’ve looked at several examples on the website and can’t find a reason why this is occurring… Could definitely use some help on this one!

Hi Christopher,

Please check this ASP.NET Web Forms example, at the bottom you can find details about this.

In short, that is normal behavior from ASP.NET, that’s how HttpResponse.End method terminates the current request (see the method’s remarks).

So, you can either remove try-catch from ExcelFile.Save method or change it to this:

Try
    Dim excelName = "Registration_Export.xlsx"
    Dim excelOptions = SaveOptions.XlsxDefault
    
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.ContentType = excelOptions.ContentType
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" & excelName)
    
    objExcel.Save(HttpContext.Current.Response.OutputStream, excelOptions)
    
    HttpContext.Current.Response.Flush()
    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()
Catch ex As Exception
    '...
End Try

Does this solve your issue?

Regards,
Mario

Hey Mario -

Thanks for the additional information… Unfortunately, this did not solve the error, but researching other things did get me there…

Basically, all I had to do to fix this was change remove the Response.Charset command, and replace it with Response.Buffer = True (right above the Try block).

When I did that I was then getting the excel file to download.

Thank you for your help in this matter.

Hi Christopher,

If you’re still using that ExcelFile.Save method (the one that saves directly to HttpResponse) then the ThreadAbortException is still thrown.

I’m not sure how those two mentioned properties changes that, they really shouldn’t.

Nevertheless, I’m glad you have things working now as you want.
Let me know if you need anything else.

Regards,
Mario