Search code examples
c#excelout-of-memoryapache-poinpoi

'OutOfMemoryException' reading 20mb XLSX file


I'm using NPOI to deal with Excel files. Here's how I'm reading files:

using (FileStream stream = File.OpenRead(excelFilePath))
{
    IWorkbook wb = WorkbookFactory.Create(stream);
    ...
}

However, for any XLSX file larger than a few megabytes, it causes memory usage to shot up to about 1GB and eventually throw an OOM exception.

Doing some research, I've found out that, strangely, loading a workbook from a File rather than a Stream results in less memory consumption by POI. The closest C# equivalent to the provided Java examples I've come up with to use Files is the following:

OPCPackage pkg = OPCPackage.Open(new FileInfo(excelFilePath));
XSSFWorkbook wb = new XSSFWorkbook(pkg);

But it seems to use the same underlying implementation since the memory usage is still the same and causes OutOfMemory exceptions.

Does NPOI have anything built-in for handling large XLSX files?

Suggestions on alternative libraries that can handle both XLS and XLSX files are also welcome.


Solution

  • It seems XLSX support is rather new in NPOI and it simply can't handle large files yet.

    After trying a few libraries, EPPlus was able to process the large XLSX file without a hitch, so I finally settled on having two libraries for reading in Excel files, NPOI for XLS and EPPlus for XLSX.