gpt4 book ai didi

java - 用于阅读的 Apache POI Streaming (SXSSF)

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:37 25 4
gpt4 key购买 nike

我需要读取大型 excel 文件并将其数据导入我的应用程序。

由于 POI 占用大量堆工作,经常抛出 OutOfMemory 错误,我发现有一个 Streaming API 用于串行处理 excel 数据时尚(而不是将文件完全加载到内存中)

我创建了一个包含单个工作表的 xlsx 工作簿,并在单元格中输入了多个值,并得出以下代码来尝试读取它:

public static void main(String[] args) throws Throwable {
// keep 100 rows in memory, exceeding rows will be flushed to disk
SXSSFWorkbook wb = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream("C:\\test\\tst.xlsx")));
SXSSFSheet sheet = (SXSSFSheet) wb.getSheetAt(0);
Row row = sheet.getRow(0);
//row is always null
while(row.iterator().hasNext()){ //-> NullPointerException
System.out.println(row.getCell(0).getStringCellValue());
}
}

然而,尽管能够正确获取其工作表,但它始终带有空 (null) 行。

我在互联网上研究并找到了几个 Streaming API 的例子,但没有一个是关于读取现有文件的,它们都是关于生成 excel 文件的。

是否真的可以从流中的现有 .xlsx 文件中读取数据?

最佳答案

在进一步挖掘之后,我发现了这个 library :

If you've used Apache POI in the past to read in Excel files, you probably noticed that it's not very memory efficient. Reading in an entire workbook will cause a severe memory usage spike, which can wreak havoc on a server.

There are plenty of good reasons for why Apache has to read in the whole workbook, but most of them have to do with the fact that the library allows you to read and write with random addresses. If (and only if) you just want to read the contents of an Excel file in a fast and memory effecient way, you probably don't need this ability. Unfortunately, the only thing in the POI library for reading a streaming workbook requires your code to use a SAX-like parser. All of the friendly classes like Row and Cell are missing from that API.

This library serves as a wrapper around that streaming API while preserving the syntax of the standard POI API. Read on to see if it's right for you.

InputStream is = new FileInputStream(new File("/path/to/workbook.xlsx"));
StreamingReader reader = StreamingReader.builder()
.rowCacheSize(100) // number of rows to keep in memory (defaults to 10)
.bufferSize(4096) // buffer size to use when reading InputStream to file (defaults to 1024)
.sheetIndex(0) // index of sheet to use (defaults to 0)
.sheetName("sheet1") // name of sheet to use (overrides sheetIndex)
.read(is); // InputStream or File for XLSX file (required)

还有SAX Event API , 它读取文档并通过事件解析其内容。

If memory footprint is an issue, then for XSSF, you can get at the underlying XML data, and process it yourself. This is intended for intermediate developers who are willing to learn a little bit of low level structure of .xlsx files, and who are happy processing XML in java. Its relatively simple to use, but requires a basic understanding of the file structure. The advantage provided is that you can read a XLSX file with a relatively small memory footprint.

关于java - 用于阅读的 Apache POI Streaming (SXSSF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com