我正在尝试使用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工作表后,我便试图从空的Excel工作表中读取数据,现在它正在写入Excel工作表