Skip to content

Exception Thread was being aborted #3

@MatGei

Description

@MatGei

I am using latest version 1.0.5 of ClosedXML.Extensions.AspNet

I have a static class ExcelService containging this code:

public static void CreateExcelSheetForDataTableAndAddToResponse(DataTable tableToHandle, HttpResponse response, string fileName)
{
DataSet ds = new DataSet("REPORTS");
ds.Tables.Add(tableToHandle);

        var wb = new XLWorkbook();

        wb.Worksheets.Add(ds);

        if (wb.Worksheets.FirstOrDefault() != null && wb.Worksheets.FirstOrDefault().Columns() != null)
        {
            wb.Worksheets.FirstOrDefault().Columns().AdjustToContents();
        }
        fileName = fileName.EndsWith(".xlsx") ? fileName : fileName + ".xlsx";
        //wb.DeliverToHttpResponse(response, fileName);
        response.DeliverWorkbook(wb, fileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }

I call it like this from my code behind for a asp.net page:

ExcelService.CreateExcelSheetForDataTableAndAddToResponse(myDataTable, this.Response, fileName.ToFileName());

My string extension for safe file name generation:

    public static string ToFileName(this string s)
    {
        
        string regexSearch = new string(Path.GetInvalidFileNameChars());
        Regex rs = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
        s = rs.Replace(s, "");

        return s.Replace(" ", "_");  // replace space with underscore
    }

The response stream gets the file with both delivery methods but both delivery methods also throws a Thread was being aborted exception. Catching this exception explicitly and it all works. But why is it throwing that Thread exception?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions