2

I am trying to read and write data from excel using java selenium webdriver apache poi. But my code is reading data from excel sheet but not write data into excel sheet. I have included all jar files from poi-4.0.1 here is my code

try {

          // Specify the file path which you want to create or write

          File src=new File("E:\\Dharshan\\test.xlsx");

          // Load the file

          FileInputStream fis=new FileInputStream(src);

           // load the workbook

           XSSFWorkbook wb=new XSSFWorkbook(fis);

          // get the sheet which you want to modify or create

           XSSFSheet sh1= wb.getSheetAt(0);

         // getRow specify which row we want to read and getCell which column

         System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());

         System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());

         System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());

         System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());

         System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());

         System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());

        // here createCell will create column

        // and setCellvalue will set the value

         sh1.getRow(0).createCell(3).setCellValue("2.41.0");

         sh1.getRow(1).createCell(3).setCellValue("2.5");

         sh1.getRow(2).createCell(3).setCellValue("2.39");


        // here we need to specify where you want to save file

        FileOutputStream fout = new FileOutputStream(src);

         wb.write(fout);
         fout.close();

          } catch (Exception e) {

           System.out.println(e.getMessage());

          }
}

}

3
  • What did you see in the console?Is there any line showing null? Commented Jan 9, 2019 at 13:36
  • Ya in console it's showing null Commented Jan 10, 2019 at 6:33
  • Your code should be throwing NullPointerException when no data in excel, hence the workbook is not saved. Commented Jan 10, 2019 at 7:17

1 Answer 1

1

I am trying to read data from empty excel sheet once i have updated excel sheet with data now it is writing into excel sheet

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.