Saket Belokar
Saket Belokar

Reputation: 35

Getting error in reading data from excel in selenium webdriver (java)

Here is my code

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");

    }
}

Here is the error :

Exception in thread "main" java.lang.NoSuchFieldError: RAW_XML_FILE_HEADER at org.apache.poi.poifs.filesystem.FileMagic.(FileMagic.java:42) at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:208) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:98) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324) at 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 : I am new to Selenium so learning this feature from : https://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html

Please help me , TIA

Upvotes: 0

Views: 2673

Answers (1)

Saket Belokar
Saket Belokar

Reputation: 35

Hi I googled for it & found solution of my error :

I had to include one more jar.

xmlbeans-2.3.0.jar

Such error or suggestion was not giving though while creating / building code, I wonder why not..

Upvotes: 1

Related Questions