【问题标题】:update excel cell in selenium webdriver在 selenium webdriver 中更新 excel 单元格
【发布时间】:2016-01-14 10:18:04
【问题描述】:

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

【问题讨论】:

  • 您想如何用Selenium 更新Excel 文件?目标电子表格显示在网页上还是什么?

标签: java selenium selenium-webdriver


【解决方案1】:

如果您使用的是 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;
}

然后您可以使用以下设置和获取单元格值:

table.setValueAt(value, rowIndex, getColumnByName(table, colName));

table.getValueAt(rowIndex, getColumnByName(table, colName));

如果您使用 POI,请参考以下代码:

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

您可以根据需要修改上述代码以获取特定的行和列。

参考链接:
http://www.coderanch.com/t/537168/java/java/Writing-existing-excel-xls-file

How to get/set the cell value of a JTable by its column name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多