在Selenium Webdriver中更新Excel单元格 [英] update excel cell in selenium webdriver

查看:124
本文介绍了在Selenium Webdriver中更新Excel单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在excel中,当代码运行时,特定的单元格已输入联系人名称,例如"Test-01",现在显示联系人名称.现在,我想将同一工作表和单元格即联系人名称编辑为"ABCD-200",并且它应该在再次运行代码时显示.请有人告诉我如何在Selenium Webdriver中进行操作.

In a excel particular cell has input such as contact name as "Test-01" when the code is run contact name is displayed.Now i want to edit the same sheet and cell i.e contact name to "ABCD-200" and it should be displayed when code is run again.please can anyone tell me how to do it in selenium webdriver.

推荐答案

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

In case you are using JTable, refer to below code:

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;
}

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

Then you can use the following to set & get cell values :

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

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

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

If you are using POI, refer to below code:

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

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

You can modify above code as per your requirement to get particular row and column.

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

Reference Links :
http://www.coderanch.com/t/537168/java/java/Writing-existing-excel-xls-file

如何通过其列名获取/设置JTable的单元格值

这篇关于在Selenium Webdriver中更新Excel单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆