0

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

Daniel Bernsons
  • 650
  • 7
  • 20
Saket Belokar
  • 25
  • 1
  • 8
  • Possible duplicate of [NoSuchFieldError when reading Excel sheet in java](https://stackoverflow.com/questions/37618369/nosuchfielderror-when-reading-excel-sheet-in-java) – Eugene S Jan 04 '18 at 07:45
  • Check the sheet name which you passed. As per the code, the sheet name should be 'ExcelGuru99Demo'. – Auro Sarma Jan 04 '18 at 17:17
  • Yup, that i resolved later on but still facing error – Saket Belokar Jan 05 '18 at 04:50
  • I re designed my script again with solving some errors so now error I am getting is got changed : Now I am getting below mentioned error : – Saket Belokar Jan 05 '18 at 04:52
  • Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException at excelpackage.Excel.readExcel(Excel.java:40) at excelpackage.Excel.main(Excel.java:94) Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more – Saket Belokar Jan 05 '18 at 04:53
  • ++ My Code is not giving errors while build, it is giving me run time error – Saket Belokar Jan 05 '18 at 04:54
  • Eugene S, Hi I googled for it & found solution of my error : I had to include one more jar. xmlbeans-2.3.0.jar, bdw Thanks for your help. – Saket Belokar Jan 05 '18 at 05:13

1 Answers1

1

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..

Saket Belokar
  • 25
  • 1
  • 8