有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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值(返回默认值)


共 (1) 个答案

  1. # 1 楼答案

    对于Java中的字符串比较,请使用。等于()而不是==

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