java Apache POI getRow() 返回 null 并且 .createRow 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30868325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:46:18  来源:igfitidea点击:

Apache POI getRow() returns null and .createRow fails

javaexcelapache-poi

提问by Plamen Vasilev

I have the following problem using Apache POI v3.12: I need to use a XLSX file with 49 rows [0..48] as a template, fill it's cells with data and write it out as a different file, so I can reuse the template again. What I am doing is approximately this:

我在使用 Apache POI v3.12 时遇到以下问题:我需要使用 49 行 [0..48] 的 XLSX 文件作为模板,用数据填充它的单元格并将其写出作为不同的文件,这样我就可以重用再次模板。我正在做的大约是这样的:

XSSFWorkbook wbk_template = new XSSFWorkbook(new FileInputStream    (f_wbk_template));
SXSSFWorkbook wbk = new SXSSFWorkbook(wbk_template, 50, true);

Sheet sheet = wbk.getSheet(STR_SHEET_NAME);

/ later on/

/ 稍后的/

Row row = sheet.getRow(rownum);
if (null == row) {
    row = sheet.createRow(rownum);
}

Upon debugging it turns out that getRow() returns null, but the attempt to .createRow() fails with:

调试后发现 getRow() 返回 null,但尝试 .createRow() 失败:

java.lang.IllegalArgumentException: Attempting to write a row[2] in the range [0,48] that is already written to disk.
    at org.apache.poi.xssf.streaming.SXSSFSheet.createRow(SXSSFSheet.java:122)
...

am I missing something here? As far as I have read in the Apache docs and forums, I need to createRow() if getRow() returns null. The sheet does not contain any rows according to .getPhysicalRows(), .getFirstRowNum() and .getLastRowNum()

我在这里错过了什么吗?据我在 Apache 文档和论坛中读到,如果 getRow() 返回 null,我需要 createRow()。根据 .getPhysicalRows()、.getFirstRowNum() 和 .getLastRowNum(),工作表不包含任何行

Thanks.

谢谢。

采纳答案by IceMan

See the documentation for the SXSSFWorkbookconstructor that takes the XSSFWorkbookas param. You cannot override or access the initial rows in the template file. You are trying to overwrite an existing row and the API does not support this. Your exception message reflects this.

请参阅SXSSFWorkbook采用XSSFWorkbookas 参数的构造函数的文档。您不能覆盖或访问模板文件中的初始行。您正在尝试覆盖现有行,而 API 不支持此操作。您的异常消息反映了这一点。

https://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html#SXSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

https://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html#SXSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

For your use case, you may want to try http://jxls.sourceforge.net.

对于您的用例,您可能想尝试http://jxls.sourceforge.net

回答by Hymany

If you want to read or edit an exist row, you can firstly do it in xssftype, and then create the sxssffile base on the xssffile.
The code is something like below...

如果要读取或编辑xssf现有行,可以先在类型中进行,然后根据sxssf文件创建xssf文件。
代码如下所示......

XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));

//do the read and edit operation with xssf......
......
......

SXSSFWorkbook sXSSFbook = new SXSSFWorkbook(xssfWorkbook); 

//do the write operation with sxssf......
......
......