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

7
  • Possible duplicate of NoSuchFieldError when reading Excel sheet in java Commented Jan 4, 2018 at 7:45
  • Check the sheet name which you passed. As per the code, the sheet name should be 'ExcelGuru99Demo'. Commented Jan 4, 2018 at 17:17
  • Yup, that i resolved later on but still facing error Commented Jan 5, 2018 at 4: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 : Commented Jan 5, 2018 at 4: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 Commented Jan 5, 2018 at 4:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.