0

Below attached is the code for reading the Excel file using POI As a beginner need help

package genericReusable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader 
{

        public String[] readExcel(String filePath,String fileName,String sheetName) throws IOException
        {

                    //Create a object of File class to open xlsx file
                        File file =    new File(filePath);//new File(filePath+"\\"+fileName);

                    //Create an object of FileInputStream class to read excel file
                        FileInputStream inputStream = new FileInputStream(file);


                        Workbook wrkbk = new XSSFWorkbook(inputStream);

                        //Read sheet inside the workbook by its name
                        Sheet sheet = wrkbk.getSheet(sheetName);

                        //Find number of rows in excel file
                        int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();

                        // Array list to Store the Data
                        ArrayList<String> Data_Array = new ArrayList<String>( );

                        //Create a loop over all the rows of excel file to read it
                        for (int i = 1; i < rowCount+1; i++) 
                        {
                            Row row = sheet.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()+"|| ");

                            Data_Array.add(row.getCell(j).getStringCellValue());

                            }

                            System.out.println();

                        }

                        return (String[]) Data_Array.toArray( new String[ Data_Array.size() ] );

        }




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

{


ExcelReader obj = new ExcelReader();
String filePath = System.getProperty("user.dir")+"\\src\\genericReusable";


obj.readExcel(filePath,"ExportExcel.xlsx","Login");

}
}

Running the code displays the below error. Not sure why there is NoClassDefFoundError as i have placed the file in the proper location Need Help on passing the excel and reading using Apache POI

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at genericReusable.ExcelReader.readExcel(ExcelReader.java:26)
at genericReusable.ExcelReader.main(ExcelReader.java:73)
Caused by: java.lang.ClassNotFoundException:    org.apache.xmlbeans.XmlObject
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
mrfreester
  • 1,981
  • 2
  • 17
  • 36
Abhishek
  • 1
  • 2

1 Answers1

1

As the error suggests, it isn't able to find the dependent class which is being used in the project.

You have to add the XmlBeans dependency in your project. Add it and the error will go.

You can download the jar from here.

Paras
  • 3,197
  • 2
  • 20
  • 30