Akhi21
Akhi21

Reputation: 229

How to write in cell in excel using apache POI in JAVA?

How to write into excel file to the particular cell.I have a method which accepts file location and row number to which i have to write into it.

What i tried is read the file then created a cell and set the value into it, but when i look into it the cell is still blank.

    public void writeInExcelRowCell(String file , int rowNum) throws Exception {
        FileInputStream excelFile = new FileInputStream(new File(file));
        Workbook workbook = new XSSFWorkbook(excelFile);

        Sheet sheet = workbook.getSheetAt(0);
        sheet.getRow(rowNum).createCell(2).setCellValue("DONE");
        sheet.getRow(rowNum).createCell(3).setCellValue("PENDING");
        workbook.close();        
        
    }

Upvotes: 1

Views: 1524

Answers (1)

Sam
Sam

Reputation: 632

What you did is right, but have forgot to write your changes. You need to open an Outputstream to write those changes.

    FileOutputStream outputStream = new FileOutputStream("yourfile");
    workbook.write(outputStream);
    workbook.close();
    outputStream.close();

Hope this works!!

Upvotes: 3

Related Questions