在excel中,当代码运行时,特定单元格会输入联系人姓名,如"Test-01"。联系人姓名会显示出来。现在,我想编辑相同的工作表和单元格,即联系人名称为"ABCD-200",当代码再次运行时,它应该显示出来。请告诉我如何在selenium网络驱动程序中执行此操作。
如果您使用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
如何通过列名称获取/设置JTable的单元格值