Updating excel file using Apache POI

前端 未结 3 720
傲寒
傲寒 2021-02-07 08:34

I am trying to update an existing excel file using Apache POI. Every time I run my code I receive an error as shown below. I have also tried FileInputStreamNewFile thing.

<
3条回答
  •  名媛妹妹
    2021-02-07 08:56

    If you replace

    //Update the value of cell
    cell = sheet.getRow(row).getCell(col);
    cell.setCellValue("Pass");
    

    With

    //Retrieve the row and check for null
    HSSFRow sheetrow = sheet.getRow(row);
    if(sheetrow == null){
        sheetrow = sheet.createRow(row);
    }
    //Update the value of cell
    cell = sheetrow.getCell(col);
    if(cell == null){
        cell = sheetrow.createCell(col);
    }
    cell.setCellValue("Pass");
    

    It will work!

提交回复
热议问题