https://bbs.csdn.net/topics/390393096?page=1
這幾天,我也遇到了這個問題,東查西查,終於解決。
首先要搞清楚HSSF、XSSF、SXSSF
HSSF是POI工程對Excel 97(-2007)文件操作的純Java實現
XSSF是POI工程對Excel 2007 OOXML (.xlsx)文件操作的純Java實現
從POI 3.8版本開始,提供了一種基於XSSF的低內存佔用的API----SXSSF
在POI的API文檔中,對SXSSF的說明如下:
『
public SXSSFWorkbook(XSSFWorkbook workbook)
Construct a workbook from a template.
There are three use-cases to use SXSSFWorkbook(XSSFWorkbook) :(這部分解釋了什麼情況下使用SXSSF)
Append new sheets to existing workbooks. You can open existing workbook from a file or create on the fly with XSSF.
Append rows to existing sheets. The row number MUST be greater than max(rownum) in the template sheet.
Use existing workbook as a template and re-use global objects such as cell styles, formats, images, etc.
All three use cases can work in a combination.
What is not supported:
Access initial cells and rows in the template. After constructing SXSSFWorkbook(XSSFWorkbook) all internal windows are empty and SXSSFSheet@getRow and SXSSFRow#getCell return null.
(這句解釋了爲什麼是Null)
Override existing cells and rows. The API silently allows that but the output file is invalid and Excel cannot read it.
Parameters:
workbook - the template workbook
』
上面說是不能獲取row,但是下面還有一個方法,可以指定flush大小。sheet copy之後也可以getrow()
SXSSFWorkbook(int rowAccessWindowSize)
Construct an empty workbook and specify the window for row access.
SXSSFWorkbook targetbook1 = new SXSSFWorkbook(1000);
XSSFWorkbook tempWorkbook = new XSSFWorkbook(fis);
tempWorkbook----》sheet copy到 targetbook1 之後就可以用targetbook1.getSheetAt(0).getRow 了。