有 Java 编程相关的问题?

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

java Selenium Webdriver从Excel工作表读取数据

我有一个类似不同参数的场景,方法中的变量应该从Excel表格中获取数据,并获取其值。例如,有一个类似countryname的变量,在Excel表格的第8列中有一个国家名称列表。所以,当我第一次运行代码时,countryname变量应该取第8列中的第一个单元格值,下次运行代码时,它应该取第8列中的第二个单元格值。同样,还有其他变量应该从Excel表格中获取数据。我如何在java中自动化这个场景


共 (1) 个答案

  1. # 1 楼答案

    可以在项目中添加Apache POI jar文件,以使用Excel(XLSX)文件。 另外,附上一个示例代码供您参考

    void readXLSXFile(String fileName) throws IOException
        {
            FileInputStream fs= new FileInputStream(fileName);
            XSSFWorkbook wb = new XSSFWorkbook(fs);
    
            XSSFSheet ws = wb.getSheetAt(0);//you can change the value to index value of your required sheet
            Iterator<Row> rows = ws.rowIterator();
    
            XSSFRow row;            
            while(rows.hasNext())
            {
            row=(XSSFRow) rows.next();
            XSSFCell cell=row.getCell(7);//8th cell of the row
            String CName= cell.getStringCellValue(); //getting the cell value as string
    
            countryNames(CName);// Your method for passing the country name to the method for your requirement
            }
    
            wb.close();
        }