Creating file which is more 1.5 Mb throws and exception System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Cannot access Stream object because it was closed or disposed.'

Jul 23, 2013 at 5:39 AM
Hello everyone.

My aim is to form and save the xlsx file using epplus.dll. Everything is fine when file is less than 1.5 Mb. But trouble arrows when formed file, becomes more than 1.5 MB and it throws an exception at saving time.


Exception
 System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Cannot access Stream object because it was closed or disposed.'
Code snippet
 using (ExcelPackage package = new ExcelPackage())
         {
// there is code above where I write the data in package.
Response.Clear();
             Response.ContentType = "application/force-download";
             Response.AddHeader("content-disposition", "attachment; filename=ReportLicensesInExcel" + DateTime.Today.ToShortDateString() + ".xlsx");
             Response.Buffer = true;

             package.SaveAs(Response.OutputStream);

             Response.End();
    }
Command line with exception
package.SaveAs(Response.OutputStream);
Stack trace

[ObjectDisposedException: Cannot access a disposed object.
Object name: 'Cannot access Stream object because it was closed or disposed.'.]
OfficeOpenXml.ExcelPackage.Save() +1235
OfficeOpenXml.ExcelPackage.SaveAs(Stream OutputStream) +83
Forms_ExpImpLicense.PrintToExcel() in c:\Inetpub\wwwroot\easuweb\VET\Forms\ExpImpLicense.aspx.cs:1220
Forms_ExpImpLicense.ExportToExcel_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\easuweb\VET\Forms\ExpImpLicense.aspx.cs:1623
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

What should i do?
Editor
Jul 23, 2013 at 11:30 AM
Edited Jul 23, 2013 at 2:57 PM
I´m pretty sure when you call SaveAs method some objects are disposed, but i don't know why you have problem only with files greater than 1.5mb

try this:
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                   
                Response.BinaryWrite(pck.GetAsByteArray());
                Response.End();
Jul 23, 2013 at 12:46 PM
I was having an issue where the workbook had to be repaired every time I downloaded.
Response.End()
Thank you mrxrsd
Jul 23, 2013 at 2:51 PM
Try removing this line:
Response.Buffer = true;
Jul 24, 2014 at 7:16 PM
Was the ever resolved? I am experiencing this same issue with any file over 1.5 MB in an MVC 3 web app.
 public ActionResult GetPublisherReportFile()
 {
      ExcelPackage pck = new ExcelPackage();
            
      var ws = pck.Workbook.Worksheets.Add("Report");

      ... omitted for brevity

      string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      string downloadName = string.Format("{0}.xlsx", reportTitle);

      var fileStream = new MemoryStream();
      pck.SaveAs(fileStream);
      fileStream.Position = 0;

      return File(fileStream, contentType, downloadName);
}
I really want this to work or I will have to move on to other options. It's a slick library.

It runs fine in my development environment but in production it throws the following error when it goes to production:
Cannot access a disposed object. Object name: 'Cannot access Stream object because it was closed or disposed.'. 
Production is running Windows Server 2003 R2 x64 and IIS6

Have attempted every solution I could fine on the web but no luck.