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?
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");
worksheet.Cells["A1"].LoadFromCollection(data);
var stream = new MemoryStream(package.GetAsByteArray());
}
I know the question was answered months before, but this is how I do it for future reference to anyone trying:
In VB.NET:
Dim stream As New MemoryStream
Using package As New ExcelPackage(stream)
'Here goes the ExcelPackage code etc
package.Save()
End Using
In C#:
MemoryStream stream = new MemoryStream();
using (ExcelPackage package = new ExcelPackage(stream))
{
//Here goes the ExcelPackage code etc
package.Save()
}
The C# code should be correct, as far as I know. And the ExcelPackage has built-in support for streams.