如何使用Selenium在Excel文件中查找字符串的位置。

huangapple go评论75阅读模式
英文:

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("您的总数是:" + 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().equals(cellContent)) {
                            rowNum = row.getRowNum();
                            System.out.println(rowNum);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        return rowNum;
    }

    private static void finish() {
        System.exit(0);
    }
}

findRow()没有返回任何行号值(默认值被返回)。

英文:

case: my string position is dynamic so i have to search the string position in the excel and then fetch the value next to that position.

This is what i have tried

    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() is not returning any rownum value (default value is returned).

答案1

得分: 0

在Java中进行字符串比较时,请使用.equals()而不是==。

if(cell.getRichStringCellValue().getString().equals(cellContent))
英文:

For string comparison in Java please use .equals() and not ==.

    if(cell.getRichStringCellValue().getString().equals(cellContent))

huangapple
  • 本文由 发表于 2020年3月17日 00:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/60710120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定