dan dan - 1 year ago 708
C# Question

Using EPPlus with a MemoryStream

I am using EPPlus to generate an XLSX file in C#. As soon as I instantiate the ExcelPackage with a memory stream - I get the error:

"A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))"

Code is:

MemoryStream stream = new MemoryStream();

using (ExcelPackage package = new ExcelPackage(stream)) {

Has anyone else seen this?

Answer Source

None of the other answers quite got me there (the Excel worksheet was always empty), but this worked for me:

using (var package = new ExcelPackage())
    var worksheet = package.Workbook.Worksheets.Add("Worksheet Name");


    var stream = new MemoryStream(package.GetAsByteArray());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download