Using EPPlus with a MemoryStream

后端 未结 8 1113
离开以前
离开以前 2020-12-05 06:05

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 occ

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 06:55

    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 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.

提交回复
热议问题