【问题标题】:selenium webdriver not writing data into excel sheet using apache poiselenium webdriver没有使用apache poi将数据写入excel表
【发布时间】:2019-01-09 21:38:04
【问题描述】:

我正在尝试使用 java selenium webdriver apache poi 从 excel 中读取和写入数据。但是我的代码是从 excel 表中读取数据,但没有将数据写入到 excel 表中。我已经包含了 poi-4.0.1 中的所有 jar 文件,这是我的代码

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

          }
}

}

【问题讨论】:

  • 你在控制台看到了什么?有没有显示空的行?
  • 你在控制台中显示为空
  • 当excel中没有数据时,您的代码应该抛出NullPointerException,因此不会保存工作簿。

标签: java selenium


【解决方案1】:

我正在尝试从空的 excel 表中读取数据,一旦我用数据更新了 excel 表,现在它正在写入到 excel 表中

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2017-04-18
    • 2016-03-13
    • 2021-05-30
    • 2021-10-29
    • 2023-01-26
    相关资源
    最近更新 更多