java - Apache POI : Why data is not properly inserted?

标签 java excel apache for-loop apache-poi

我想使用 Apache POI 将字符串插入到 Excel 中的单元格矩阵中,代码如下:

for (int j=0;j<13;j++){
     for(int i=6;i<30;i++)
     {
        XSSFRow row=sheet6.createRow(i);
        cell0=row.createCell(j);
        cell0.setCellValue("SampleRules_CI_01");
    }
}

执行程序后,我正确填充了 j=12 列,但其他列(从 0 到 11)仍然为空

最佳答案

当您调用 createRow method 时,每次返回到相同的 i 值时,您都会替换该行。 。这具有删除该行上已经存在的所有内容的效果,并且只有最后一列不会以这种方式被删除。

首先,在您的 i for 循环中,调用 the getRow method查看它是否已经存在。如果它返回null,则该行尚不存在,然后您可以调用createRow

XSSFRow row = sheet6.getRow(i);
if (row == null)
{
    row = sheet6.createRow(i);
}

关于java - Apache POI : Why data is not properly inserted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23499079/

相关文章:

java - 填充二维数组中的主对角线

java - 如何在 Eclipse 中将 Lanterna 包导入到 Java

java - 如何使用正则表达式捕获空字段?

Java Swing完全圆角的JButton(圆圈外的区域无法点击也看不到)

vba - 错误: Paste method of worksheet class failed

java - Django-Piston 和 Python 客户端与 Java 客户端

apache - 通配符 SSL - HTTPS 重定向 www 证书错误

c++ - 将 Excel 数据加载到 Linux/wxWidgets C++ 应用程序中?

c# - 如果我在 Visual Studio 中开发 Excel 应用程序,是否会失去宏录制的好处?

PHP脚本保护