0
votes

I am using EPPlus and C# in an ASP.Net MVC application to read an excel document out of a storage system. It is returned as a byte array (byte[]) to the method. I convert the byte array to a stream and then to an ExcelPackage and modify the cells on the existing worksheets.

When I convert the ExcelPackage back to a stream, to be served to the calling browser as a FileStreamResult. When I execute the line MemoryStream ms = new MemoryStream(excel.GetAsByteArray()); I get the above referenced exception.

    public ActionResult Export(string id)
    {
        // Get the Excel document from storage

        MemoryStream ohDoc = new MemoryStream();
        ohDoc.Write(doc.data, 0, doc.data.Length);
        ohDoc.Position = 0;

        ExcelPackage excel = new ExcelPackage(ohDoc);
        var workBook = excel.Workbook;
        var bidInfo = excel.Workbook.Worksheets["BID INFO"];
        var recap = excel.Workbook.Worksheets["RECAP"];

        // Modify the worksheets ...

        MemoryStream ms = new MemoryStream(excel.GetAsByteArray());
        ms.Position = 0;

        FileStreamResult fileStreamResult = new FileStreamResult(ms, doc.MIMEType);
        fileStreamResult.FileDownloadName = doc.FileName;

        return fileStreamResult;
    }
}

I have spent hours reading through Google and Stack Overflow and have not been able to find an answer.

1
This Exception is thrown when you try to access something with a key that isn't there. I would start with the worksheets. Are you sure those sheets exist?Kyle
Yes, the worksheets do exist. That was the first thing I checked. :)user6373040
SheetName with spaces may not support, remove spaces from sheet names @RusselMadereKailash Chandra Polai
@KailashChandraPolai I unfortunately do not control the spreadsheet. If I did, it would be a web application instead.user6373040
I also experienced the issue when using copy function with the file generated using Google sheet.Vidyesh

1 Answers

1
votes

When I downgraded the EPPlus library from 4.1.0 to 3.1.3, it started to work properly.