user6373040
user6373040

Reputation:

EPPlus throwing "The given key was not present in the dictionary." when saving an document

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.

Upvotes: 0

Views: 2780

Answers (1)

user6373040
user6373040

Reputation:

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

Upvotes: 1

Related Questions