'OutOfMemoryException' reading 20mb XLSX file

时间:2015-11-12 11:45:42

标签: c# excel out-of-memory apache-poi npoi

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.

2 个答案:

答案 0 :(得分:2)

似乎XLSX支持在NPOI中相当新,但它根本无法处理大型文件。

尝试了几个库之后,EPPlus能够毫不费力地处理大型XLSX文件,所以我最终选择了两个用于读取Excel文件的库,NPOI for XLS和EPPlus for XLSX。

答案 1 :(得分:0)

As a suggestion for alternative libraries, a good one is Apache POI. I've used it extensively for both XLSX and XLS files and it does the job well. Here's a gist to do a quick test on your file.

The only format Apache POI doesn't cover is the old format XML files for which Xelem can be used instead.