1

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();        
        
    }

1 Answer 1

3

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!!

Sign up to request clarification or add additional context in comments.

4 Comments

Please note that you should always close resources in a finally block.
Yes forgot to mention that. Use finally or try-with-resources to close these stream. Thanks for that @AleksandrErokhin
@Sam i just had a question what if the cell has already some value will it override it?
Yes. I hope so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.