Using EPPlus with a MemoryStream

C#ExcelEpplus

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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());
}

Solution 2 - C#

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.

Solution 3 - C#

If you want to continue using a stream (e.g. Response.OutputStream) you can create an ExcelPackage with an empty constructor and use the SaveAs(Stream OutputStream) method.

Solution 4 - C#

It looks like you're hitting a bug in the error handler of the ExcelPackage constructor. If you try and give it an empty stream, System.IO.Packaging.Package.Open raises an exception indication that a package cannot be empty.

This code works, even if the file doesn't exist:

var file = new FileInfo("test.xlsx");
using (ExcelPackage package = new ExcelPackage(file))
{
}

Given that the documentation for the constructor overload indicates that the stream is allowed to be empty, I'd recommend raising this issue in the EPPlus issue tracker.

Solution 5 - C#

You can create an ExcelPackage with an empty constructor. It will handle its own internal buffer.

http://epplus.codeplex.com/wikipage?title=WebapplicationExample

Solution 6 - C#

We had a similar issue when converting code that used the 4.1.1 version of EPPlus to the 4.5.1 version.

Originally, we were using the following pattern:

using (var ms = new MemoryStream())
{
    new ExcelBuilder().BuildResultFile(result, ms);
    ms.Position = 0;    // <-- Cannot access a closed Stream error thrown here
    // Send Excel file to Azure storage
}

And our ExcelBuilder class, BuildResultFile function:

public void BuildResultFile(List<ResultSet> resultSets, Stream stream)
{
    using (var package = new ExcelPackage(stream))
    {
        // Create Excel file from resultSets
        package.Save();
    }
}

In order to make this work with 4.5.1 we had to remove using block from the BuildResultFile function.

I can't seem to find any documentation on in GitHub w/r/t why this changed or if I am even implementing this correctly.

Solution 7 - C#

I faced with the same issue when tried to open existing excel file and spent couple days with it. In my case I received mentioned exception "A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))" due to encryption.

I was able to read .xlsx file by passing password. In my case empty string "" was enough.

in your case please try to initialize package using constructor with password:

public ExcelPackage(Stream newStream, string Password)

package = new ExcelPackage(stream, "");

Have a look into ExcelPackage source code http://epplus.codeplex.com/SourceControl/latest#EPPlus/ExcelPackage.cs

There is a method

private void Load(Stream input, Stream output, string Password) 

which is used to load excel file.

private void Load(Stream input, Stream output, string Password) 

...

if (Password != null)

{
  Stream encrStream = new MemoryStream();
  CopyStream(input, ref encrStream);
  EncryptedPackageHandler eph = new EncryptedPackageHandler();
  Encryption.Password = Password;
  ms = eph.DecryptPackage((MemoryStream)encrStream, Encryption);
}
else
{
  ms = new MemoryStream();
  CopyStream(input, ref ms);
 }

...

Code will try to decrypt excel stream even if password is empty, BUT NOT NULL.

However if you try to initialize package for file that is not encrypted you will have exception:

'The stream is not an valid/supported encrypted document.'

Solution 8 - C#

I was dealing with the same error, but none of the other answers provided any help.

In the end, the problem was solved after adding this code before trying to open the file:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

It seems that the root cause was that EPPlus could not open the ZIP because of a missing code page. I got this to work thank to this StackOverflow answer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanView Question on Stackoverflow
Solution 1 - C#Noah HeldmanView Answer on Stackoverflow
Solution 2 - C#Reigo HeinView Answer on Stackoverflow
Solution 3 - C#DannyView Answer on Stackoverflow
Solution 4 - C#Mike GoatlyView Answer on Stackoverflow
Solution 5 - C#HamidView Answer on Stackoverflow
Solution 6 - C#Electric_WizardView Answer on Stackoverflow
Solution 7 - C#ArtemView Answer on Stackoverflow
Solution 8 - C#Kristof VerbiestView Answer on Stackoverflow