在selenium webdriver(java)中从excel读取数据时出错

时间:2018-01-04 05:05:47

标签: selenium selenium-webdriver

这是我的代码

public class MyClass
{
    public void readExcel(String filePath,String fileName,String sheetName) throws IOException{
        System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        // To Maximize browser screen       
        driver.manage().window().maximize();     
        //Test 5 : Excel Read   

        File file =    new File(filePath+"\\"+fileName);
        FileInputStream inputStream = new FileInputStream(file);
        String fileExtensionName = fileName.substring(fileName.indexOf("."));
        Workbook guru99Workbook = null;

        if(fileExtensionName.equals(".xlsx")) {
            guru99Workbook = new XSSFWorkbook(inputStream);
        }    
        else if(fileExtensionName.equals(".xls")){
            guru99Workbook = new HSSFWorkbook(inputStream);
        }

        Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);

        //Find number of rows in excel file

        int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();

        for (int i = 0; i < rowCount+1; i++) {

            Row row = guru99Sheet.getRow(i);

            //Create a loop to print cell values in a row

            for (int j = 0; j < row.getLastCellNum(); j++) {

                //Print Excel data in console

                System.out.print(row.getCell(j).getStringCellValue()+"|| ");

            }

        }    
    }

    //Main function is calling readExcel function to read data from excel file

    public static void main(String...strings) throws IOException{

        //Create an object of ReadGuru99ExcelFile class

        MyClass objExcelFile = new MyClass();

        //Prepare the path of excel file

        String filePath = System.getProperty("user.dir")+"\\src\\newpackage";
        //excelExportAndFileIO

        //Call read file method of the class to read data

        objExcelFile.readExcel(filePath,"Keywords.xlsx","ExcelGuru99Demo");

    }
}

这是错误:

  

线程“main”java.lang.NoSuchFieldError中的异常:   RAW_XML_FILE_HEADER在   org.apache.poi.poifs.filesystem.FileMagic。(FileMagic.java:42)     在   org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:208)     在org.apache.poi.openxml4j.opc.ZipPackage。(ZipPackage.java:98)     在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)     在org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)at   org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:295)     at newpackage.MyClass.readExcel(MyClass.java:139)at   newpackage.MyClass.main(MyClass.java:184)

PS:我是Selenium的新手,所以从以下方面学习这个功能: https://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html

请帮助我,TIA

1 个答案:

答案 0 :(得分:1)

嗨我用谷歌搜索了它&amp;找到了我的错误的解决方案:

我不得不再加一个罐子。

的xmlbeans-2.3.0.jar

这样的错误或建议虽然在创建/构建代码时没有给出,但我想知道为什么不...