STG_E_WRITEFAULT while reading Excel file with Epplus

3

In an ASP.NET MVC Core (.netcore2) application, I'm using the following method to read an Excel file.

[HttpPost]
public IActionResult Import (IFormFile importFile)
{
    using (var excel = new ExcelPackage(importFile.OpenReadStream())
    {
        var sheet = excel.Workbook.Worksheets.FirstOrDefault();
        if (sheet == null) return BadRequest("No worksheet.");

        var events = /* import logic */ 
        return Json(events);
    }
}

However, epplus is throwing this exception:

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

I've used methods like this in the past to import Excel files, and I don't know what's going wrong.

I've checked:

  • Versions of references on the project
  • Excel file validity (I'm positive it's a valid xlsx file, not an old xls or other format)
c#
asp.net-core
epplus
asked on Stack Overflow Dec 17, 2017 by jdphenix • edited Dec 17, 2017 by jdphenix

1 Answer

1

Epplus currently has an issue when targeting .NetStandard with text encoding. To resolve this, add this reference to your project.

<ItemGroup>
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
</ItemGroup>

Then, call this once in your application's startup -

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

More detail here on why this is happening.

answered on Stack Overflow Dec 17, 2017 by jdphenix • edited Dec 17, 2017 by jdphenix

User contributions licensed under CC BY-SA 3.0