Java 在selenium webdriver中更新excel单元格

Java 在selenium webdriver中更新excel单元格,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,在excel中,当代码运行时,特定单元格有输入,例如联系人姓名作为Test-01,此时将显示联系人姓名。现在,我想编辑相同的工作表和单元格,即将联系人姓名编辑为ABCD-200,并在代码再次运行时显示。请告诉我如何在selenium webdriver中执行此操作 如果您使用的是JTable,请参阅以下代码: private int getColumnByName(JTable table, String name) { for (int i = 0; i < table.getC

在excel中,当代码运行时,特定单元格有输入,例如联系人姓名作为Test-01,此时将显示联系人姓名。现在,我想编辑相同的工作表和单元格,即将联系人姓名编辑为ABCD-200,并在代码再次运行时显示。请告诉我如何在selenium webdriver中执行此操作

如果您使用的是JTable,请参阅以下代码:

private int getColumnByName(JTable table, String name) {
    for (int i = 0; i < table.getColumnCount(); ++i)
        if (table.getColumnName(i).equals(name))
            return i;
    return -1;
}
InputStream inp = new FileInputStream("workbook.xls");

Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
    cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
如果您使用的是POI,请参阅以下代码:

private int getColumnByName(JTable table, String name) {
    for (int i = 0; i < table.getColumnCount(); ++i)
        if (table.getColumnName(i).equals(name))
            return i;
    return -1;
}
InputStream inp = new FileInputStream("workbook.xls");

Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
    cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
您可以根据需要修改上述代码,以获得特定的行和列

参考链接:

您希望如何使用Selenium更新Excel文件?网页上显示的目标电子表格还是什么?