首页 > 解决方案 > 第二个for循环没有运行

问题描述

我正在使用 Apache POI 读取和写入 excel 文件。但是,在尝试写入 1 个工作簿中的两个不同工作表时,我的第二个 for 循环没有执行。因此,只有第一个工作表被写入,而第二个工作表不被写入。

for (int r = 4; r < outTotalRows1; r++) {
    row2 = outSheet1.getRow(r);
    if (row2.getCell(3) == null) {
        for (int t = 3; t <= 47; t++) {
            outCell = row2.createCell(t);
            outCell.setCellValue(BSMeanArray[t - 3]);
            FileOutputStream outWrite = new FileOutputStream(new File(outputFileName));
            outWorkbook.write(outWrite);
            outWrite.close();
        }
    }
    if (row2.getCell(50) == null) { // if the velocity cell is == null, then set cell value to array established and then break
        for (int u = 50; u < 94; u++) {
            outCell = row2.createCell(u); // maybe this line wrong?
            outCell.setCellValue(BFMeanArray[u - 50]);
            FileOutputStream outWrite = new FileOutputStream(new File(outputFileName));
            outWorkbook.write(outWrite);
            outWrite.close();
        }
    }
}

// THIS LOOP NOT EXECUTING
for(int r = 4; r < outTotalRows2; r++){
    System.out.println("executed");
    row3 = outSheet2.getRow(r);
    if (row3.getCell(3) == null) {
        for (int u = 3; u <= 47; u++) {
            outCell = row3.createCell(u);
            outCell.setCellValue(FFSMeanArray[u - 3]);
            FileOutputStream outWrite = new FileOutputStream(new File(outputFileName));
            outWorkbook.write(outWrite);
            outWrite.close();
        }
    }
    if (row3.getCell(50) == null) {
        for (int u = 50; u <= 94; u++) {
            outCell = row3.createCell(u);
            outCell.setCellValue(FFFMeanArray[u - 50]);
            FileOutputStream outWrite = new FileOutputStream(new File(outputFileName));
            outWorkbook.write(outWrite);
            outWrite.close();
        }
    }
}

标签: javaapacheapache-poi

解决方案


推荐阅读