有 Java 编程相关的问题?

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

java在使用关键字驱动从Excel文件中获取数据时遇到了执行测试用例的问题

我面临的问题是,当我试图导入库时,库没有被导入,比如WorkbookRowCellHSSFWork等等

这里是控制台的链接:https://prnt.sc/15oajs9

 public class ReusableFunction {
    WebDriver driver;
    public  String [][] fetchDataFromExcel() {
        Workbook wb = null;
        String [][] data= null;
        try {
            String path = fetchprop("KEYWORD_PATH");
            File excel = new File(path);
            FileInputStream file  = new FileInputStream(excel);
            
            System.out.println(path);
            String extn = path.substring(path.indexOf(".")+ 1);
            System.out.println(extn);
            if (extn.equals("xlsx")) {
                wb = new XSSFWorkbook(file);
            } else {
                wb = new HSSFWorkbook(file);
            }
            Sheet sheet = wb.getSheet("Sheet1");
            
            int rownum = sheet.getLastRowNum();
            System.out.println("Rows: " + rownum);
            
            int column = sheet.getRow(0).getLastCellNum();
            
            data  = new String [rownum][column];
            
            for (int i =0; i < rownum; i++) {
                Row row = sheet.getRow(i);
                for (int j =0;  j < column; j++) {
                    Cell cell = row.getCell(j);
                    String value = cell.toString();
                    data[i][j] =  value;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return data;
    }`

有人知道我们如何解决这个问题吗


共 (1) 个答案

  1. # 1 楼答案

    在pom中添加这些依赖项。xml文件:

    poi

    <!  https://mvnrepository.com/artifact/org.apache.poi/poi  >
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.0.0</version>
    </dependency>
    

    poi-oxml:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.0.0</version>
    </dependency>
    

    commons系列:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.4</version>
    </dependency>
    

    公用压缩:

    <!  https://mvnrepository.com/artifact/org.apache.commons/commons-compress  >
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.20</version>
    </dependency>
    

    xmlbeans

    <!  https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans  >
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>5.0.0</version>
    </dependency>
    

    dom4j

    <!  https://mvnrepository.com/artifact/dom4j/dom4j  >
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    

    建立你的项目,然后运行,应该对你有用