'Value does not fall within the expected range on async call?

I am reading data from SQL and writing that data on an excel sheet as the data is too large so I am using async call for writing the data in excel. The library that I am using for excel is EPPLUS. The data reading is completed with no errors but when reached on this line

Response.AddHeader("content-disposition", "attachment: filename=" + "Report.xls");

I am getting the error Value does not fall within the expected range.

Here is my code

public async void  exportFileMaking()
            {
                ExcelPackage Ep = new ExcelPackage();
                await Task.Run(() =>
                {
                    var customername = cau.GetSelectedCustomerName(CustomerId);
                    ExcelWorksheet Sheet0 = Ep.Workbook.Worksheets.Add("Tenants");
                    Sheet0.Cells["A1"].Value = "Customer ID";
                    Sheet0.Cells["B1"].Value = "Customer Name";
    
                    Sheet0.View.FreezePanes(2, 1);
                    
                 
    Sheet0.Row(1).Style.Fill.PatternType=OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    
    Sheet0.Row(1).Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
    
                    int row0 = 2;
                    Sheet0.Cells[string.Format("A{0}", row0)].Value = CustomerId;
                    Sheet0.Cells[string.Format("B{0}", row0)].Value = customername;
                    Sheet0.Cells["A:AZ"].AutoFitColumns();
                    Sheet0.Column(1).Hidden = true;
                });
Response.Clear();
Response.ContentType = "application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment: filename=" + "Report.xls");
Response.BinaryWrite(Ep.GetAsByteArray());
Response.End();
    }


Solution 1:[1]

Value does not fall within the expected range.

This is because the header value is malformed; see here for the expected format.

There are two problems with the value your code is passing:

  1. There should be a semicolon, not a full colon, after the attachment parameter.
  2. The filename must be in quotes.

Fix:

Response.AddHeader("content-disposition", "attachment; filename=\"Report.xls\"");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephen Cleary