如何使用 selenium 在 excel 文件中查找字符串的位置
How to find the position of the string in excel file using selenium
案例:我的字符串位置是动态的,所以我必须在 excel 中搜索字符串位置,然后获取该位置旁边的值。
这是我试过的
public class AssetRegisterdepreciationReport
{
public static void ReadAssetExcelData() throws IOException
{
String cellContent = "Grand Total:";
int rownr, colnr = 5;
FileInputStream AssetReport = new FileInputStream("C:\Users\Admin\Downloads\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
@SuppressWarnings("resource")
XSSFWorkbook Assetworkbook = new XSSFWorkbook(AssetReport);
XSSFSheet sheetnumber1 = Assetworkbook.getSheetAt(0);
rownr = findRow(sheetnumber1, cellContent);
output(sheetnumber1, rownr, colnr);
finish();
}
private static void output(XSSFSheet sheetnumber1, int rownr, int colnr)
{
XSSFRow row = sheetnumber1.getRow(rownr);
XSSFCell cell = row.getCell(colnr);
System.out.println("Your total is: " + cell);
}
private static int findRow(XSSFSheet sheetnumber1, String cellContent)
{
int rowNum = 0;
for(Row row : sheetnumber1)
{
for(Cell cell : row)
{
switch(cell.getCellType())
{
case STRING:
if(cell.getRichStringCellValue().getString() == cellContent)
{
rowNum = row.getRowNum();
System.out.println(rowNum);
}
default:
break;
}
}
}
return rowNum;
}
private static void finish()
{
System.exit(0);
}
}
findrow() 没有返回任何 rownum 值(返回默认值)。
对于 Java 中的字符串比较,请使用 .equals() 而不是 ==。
if(cell.getRichStringCellValue().getString().equals(cellContent))
案例:我的字符串位置是动态的,所以我必须在 excel 中搜索字符串位置,然后获取该位置旁边的值。
这是我试过的
public class AssetRegisterdepreciationReport
{
public static void ReadAssetExcelData() throws IOException
{
String cellContent = "Grand Total:";
int rownr, colnr = 5;
FileInputStream AssetReport = new FileInputStream("C:\Users\Admin\Downloads\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
@SuppressWarnings("resource")
XSSFWorkbook Assetworkbook = new XSSFWorkbook(AssetReport);
XSSFSheet sheetnumber1 = Assetworkbook.getSheetAt(0);
rownr = findRow(sheetnumber1, cellContent);
output(sheetnumber1, rownr, colnr);
finish();
}
private static void output(XSSFSheet sheetnumber1, int rownr, int colnr)
{
XSSFRow row = sheetnumber1.getRow(rownr);
XSSFCell cell = row.getCell(colnr);
System.out.println("Your total is: " + cell);
}
private static int findRow(XSSFSheet sheetnumber1, String cellContent)
{
int rowNum = 0;
for(Row row : sheetnumber1)
{
for(Cell cell : row)
{
switch(cell.getCellType())
{
case STRING:
if(cell.getRichStringCellValue().getString() == cellContent)
{
rowNum = row.getRowNum();
System.out.println(rowNum);
}
default:
break;
}
}
}
return rowNum;
}
private static void finish()
{
System.exit(0);
}
}
findrow() 没有返回任何 rownum 值(返回默认值)。
对于 Java 中的字符串比较,请使用 .equals() 而不是 ==。
if(cell.getRichStringCellValue().getString().equals(cellContent))