I am using eclipse to try and get a list of sheets from an excel spreadsheet but when I go to run it Java throws exceptions.
here is the code;
File myFile = new File("excel.xlsx");
Workbook wb = null;
try {
wb = WorkbookFactory.create(myFile);
} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> sheetNames = new ArrayList<String>();
for (int i=0; i<wb.getNumberOfSheets(); i++) {
sheetNames.add( wb.getSheetName(i) );
}
System.out.println(sheetNames);
here is the log;
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
at com.cogentautomation.view.View.main(View.java:19)
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)
... 4 more
I have included the following in the eclipse java build path as libraries;
I know the solution is something simple but my mind is blanking. I am not using maven.
You will need to add XMLBeans dependency.You can include the dependency from here https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0
As the exception message is telling you, you need the XMLBeans library. Poi probably depends on it.
If you were using a dependency management tool such as Gradle or Maven, it would have automatically been included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With