Java 使用apache POI在excel文件中写入单元格时出现问题

Java 使用apache POI在excel文件中写入单元格时出现问题,java,excel,apache-poi,Java,Excel,Apache Poi,我在将数据写入excel文件时遇到问题。 我正在使用ApachePOI4.1.2版本库。 下面是示例代码 try { outputStream = new FileOutputStream(EXCEL_FILE_PATH); } catch (Exception e) { System.out.println("Exception While writing excel file " + e.getMessage());

我在将数据写入excel文件时遇到问题。 我正在使用ApachePOI4.1.2版本库。 下面是示例代码

    try {
        outputStream = new FileOutputStream(EXCEL_FILE_PATH);
    } catch (Exception e) {
        System.out.println("Exception While writing excel file " + e.getMessage());
    }

    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("FileCompare");

    Row row = sheet.createRow(0);

    Cell cellfilename = row.createCell(0);
    cellfilename.setCellValue("File Name");

    Cell cellfilename1 = row.createCell(1);
    cellfilename1.setCellValue("Difference in File 1");

    Cell cellfilenam2 = row.createCell(2);
    cellfilenam2.setCellValue("Difference in File 2");

    for (int diffcol = 1; diffcol < 3; diffcol++) {
        for (int i = 1; i < 57; i++) {

            Row rows = sheet.createRow(i);
            // System.out.println("Difference Coln number " + diffcol);
            Cell diffcell = rows.createCell(diffcol);
            diffcell.setCellValue("abacds");

            /*
             * Cell diffcell2 = row.createCell(2); diffcell2.setCellValue("abacds");
             */

        }
        
    }


    try {
        workbook.write(outputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }
试试看{
outputStream=新文件outputStream(EXCEL文件路径);
}捕获(例外e){
System.out.println(“写入excel文件时出现异常”+e.getMessage());
}
工作簿=新的HSSF工作簿();
工作表=工作簿.createSheet(“文件比较”);
Row Row=sheet.createRow(0);
Cell cellfilename=row.createCell(0);
cellfilename.setCellValue(“文件名”);
Cell cellfilename1=row.createCell(1);
cellfilename1.setCellValue(“文件1中的差异”);
Cell cellfilenam2=row.createCell(2);
setCellValue(“文件2中的差异”);
对于(int-diffcol=1;diffcol<3;diffcol++){
对于(int i=1;i<57;i++){
Row rows=sheet.createRow(i);
//System.out.println(“差异列编号”+diffcol);
Cell diffcell=rows.createCell(diffcol);
diffcell.setCellValue(“abacds”);
/*
*Cell diffcell2=row.createCell(2);diffcell2.setCellValue(“abacds”);
*/
}
}
试一试{
workbook.write(outputStream);
}捕获(例外e){
e、 printStackTrace();
}最后{
outputStream.flush();
outputStream.close();
workbook.close();
}
在此列中,只有最后一列单元格保存在excel文件中,之前的单元格保留为空白。 如果我做错了什么,请帮助并让我知道


不确定实际的api,但我认为内部循环应该创建列,外部循环应该创建这样的行

  for (int row=1:row<57;row++)
  {
     Row rows = sheet.createRow(row);
     for (int diffCol = 1; diffCol < 3; difCol++) 
     {
        Cell diffcell = rows.createCell(diffcol);
        diffcell.setCellValue("abacds");
     }
  }

for(int row=1:row不确定实际的api,但我认为内部循环应该创建列,外部循环应该创建这样的行

  for (int row=1:row<57;row++)
  {
     Row rows = sheet.createRow(row);
     for (int diffCol = 1; diffCol < 3; difCol++) 
     {
        Cell diffcell = rows.createCell(diffcol);
        diffcell.setCellValue("abacds");
     }
  }

for(int row=1:row问题在于,在循环中,您总是使用
sheet.createRow(i)
来检索所需的行,但是,正如所说的(实际上写得不太清楚的文档),此方法总是重新创建一个全新的空行,删除现有行(如果该i位置的行已经存在)

这意味着YOUR循环的每个迭代实际上都在删除以前的行,创建全新的行:在最后,只有上一次迭代创建的行仍然存在

要解决您的问题,请仅使用
sheet.createRow(i)
一次,以便在i位置创建行,然后仅使用来检索它

因此,请(在代码中)替换以下错误行

使用以下代码

Row row = sheet.getRow(i);
if (row == null) row = sheet.createRow(i);
仅当新行不存在时才创建新行


你完成了,它就像一个符咒一样工作!

问题是,在你的循环中,你总是使用
sheet.createRow(i)
来检索你需要的行,但是正如所说的(实际上写得不太清楚的文档),这个方法总是重新创建一个全新的空行,删除现有的行(如果该i位置的行已经存在)

这意味着YOUR循环的每个迭代实际上都在删除以前的行,创建全新的行:在最后,只有上一次迭代创建的行仍然存在

要解决您的问题,请仅使用
sheet.createRow(i)
一次,以便在i位置创建行,然后仅使用来检索它

因此,请(在代码中)替换以下错误行

使用以下代码

Row row = sheet.getRow(i);
if (row == null) row = sheet.createRow(i);
仅当新行不存在时才创建新行

你完成了,它就像一个符咒