![](/img/trans.png)
[英]How to get the cell values from column1 and column 3 and put in map as key value pair using excel in java
[英]How to get column values from an excel using only column name?
我想从Excel工作表中读取一组用户名和密码。 我的用户名显示在第一列中,而我的密码值显示在第八列中。 我不想通过提供第8列地址来读取第8列的值。 相反,无论我将其放在Excel工作表中的任何位置,我都想读取密码值。 有什么办法吗?
FileInputStream fs = new FileInputStream(strReadFile);
工作簿wb = Workbook.getWorkbook(fs);
工作表sh = wb.getSheet(“ Sheet1”);
int rowCount = sh.getRows();
System.out.println(“行数:” +行数);for(int row=1;row<rowCount;row++) { String Username = sh.getCell(1,row).getContents(); driver.findElement(By.id("user_name")).sendKeys(Username); String Password = sh.getCell(2,row).getContents(); driver.findElement(By.id("user_pass")).sendKeys(Password); driver.findElement(By.id("login_button")).click(); }
根据我的理解,没有内置方法。 请在下面的方法栏中查看您的解决方案。
for(int row=1;row<rowCount;row++) {
String Username = sh.getCell(column("username column",sh),row).getContents();
driver.findElement(By.id("user_name")).sendKeys(Username);
String Password = sh.getCell(column("password column",sh),row).getContents();
driver.findElement(By.id("user_pass")).sendKeys(Password);
driver.findElement(By.id("login_button")).click();
}
public int column(String columnName, Sheet sheetAt) throws Exception{
Row row = sheetAt.getRow(0);
int col = -1;
for (int i=0; i<row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
continue;
}
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String text = cell.getStringCellValue();
if (columnName.equals(text)) {
col = i;
break;
}
}
}
if (col == -1) {
throw new Exception("None of the cells in the first row were Patch");
}
return col;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.