从Java到Excel的空单元格

时间:2015-12-06 10:21:28

标签: java excel cells

我正在尝试使用Excel制作基于数组的程序。我已经看过很多教程,我正在尝试使用下面的代码,但正如你在图片中看到的那样,Java只在每行的最后一列写入。我希望你能帮助我

for (int i=0; i<10; i++){
        Row row = sheetOLD.createRow(i);
        Cell cell = row.createCell(0);
        cell.setCellValue(i);
}

for (int i=0; i<3; i++){
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    Cell name = sheetOLD.createRow(i).createCell(1);
    name.setCellValue(input.nextLine());
}

Cell test = sheetOLD.createRow(7).createCell(1);
test.setCellValue("TEST");

the results in excel

1 个答案:

答案 0 :(得分:1)

使用getRow而不是createRow 2次和3次(因为createRow替换所有行并删除行中的所有单元格):

for (int i=0; i<10; i++) {
        Row row = sheetOLD.createRow(i);
        Cell cell = row.createCell(0);
        cell.setCellValue(i);
}

for (int i=0; i<3; i++){
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    Cell name = sheetOLD.getRow(i).createCell(1);
    name.setCellValue(input.nextLine());
    }

Cell test = sheetOLD.getRow(7).createCell(1);
    test.setCellValue("TEST");

您可以找到this

的更多信息