pom.xml
<properties>
<poi.version>4.1.2</poi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
1. 读写大量数据使用:
org.apache.poi.xssf.streaming.SXSSFWorkbook
以下是源码中对SXSSFWorkbook的说明:
Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. This allows to write very large files without running out of memory as only a configurable portion of the rows are kept in memory at any one time. You can provide a template workbook which is used as basis for the written data. See https://poi.apache.org/spreadsheet/how-to.html#sxssf for details. Please note that there are still things that still may consume a large amount of memory based on which features you are using, e.g. merged regions, comments, ... are still only stored in memory and thus may require a lot of memory if used extensively. SXSSFWorkbook defaults to using inline strings instead of a shared strings table. This is very efficient, since no document content needs to be kept in memory, but is also known to produce documents that are incompatible with some clients. With shared strings enabled all unique strings in the document has to be kept in memory. Depending on your document content this could use a lot more resources than with shared strings disabled. Carefully review your memory budget and compatibility needs before deciding whether to enable shared strings or not.
SXSSFWorkbook节省了内存,但是只能查看指定窗口位置的数据。默认的窗口大小是100. 如果不需要查看已经写出的数据,用这个还是很棒的。
2. 读写.xls文件
org.apache.poi.hssf.usermodel.HSSFWorkbook
最多可写出65535条数据 [2^16 - 1]
3. 读写.xlsx文件
org.apache.poi.xssf.usermodel.XSSFWorkbook