Apache POI getRow()返回null并且.createRow失败 [英] Apache POI getRow() returns null and .createRow fails

查看:144
本文介绍了Apache POI getRow()返回null并且.createRow失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Apache POI v3.12时出现以下问题:我需要使用具有49行[0..48]的XLSX文件作为模板,用数据填充其单元格并将其写为其他文件,以便再次使用该模板.我正在做的大概是这样:

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:

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);

/稍后在/

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

在调试时,事实证明getRow()返回null,但是尝试.createRow()失败并显示:

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

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)
...

我在这里想念什么吗?据我在Apache文档和论坛中阅读的内容,如果getRow()返回null,则需要createRow().根据.getPhysicalRows()、. getFirstRowNum()和.getLastRowNum(),工作表不包含任何行

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()

谢谢.

推荐答案

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

See the documentation for the SXSSFWorkbook constructor that takes the XSSFWorkbook as 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.

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

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

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

这篇关于Apache POI getRow()返回null并且.createRow失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆