Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

java - update excel cell in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

Reference Links :
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...