java - 如何使用selenium在excel文件中查找字符串的位置
问题描述
案例:我的字符串位置是动态的,所以我必须在 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))
推荐阅读
- python - Group_by 不适用于具有 mixin 列的表 [Astropy, Table]
- java - 用于测试我的 API 客户端的 JUnit 测试不起作用,“错误:ID 为 'junit-jupiter' 的测试引擎无法执行测试”
- tensorflow - CNN 训练准确率突然从 94% 下降到 48%
- dart - dart和flutter构建的内核文件有什么区别?
- haskell - 当中间值从未具体键入时,Haskell 编译器如何为 `(==) (fromInteger 0) (fromInteger 0)` 生成代码?
- javascript - 使用HTML5拖拽时如何修改拖拽效果?
- shared-libraries - .so文件中的函数如何自动导出?
- haskell-stack - 为什么堆栈不使用定义的额外依赖项?
- python-3.x - TKINTER:如果条目与存储的数据匹配,则读取文件?
- python - 检查列表中是否已存在值