问题
I am getting these exceptions in my code while i m writing some data in excel workbook using poi jars:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at WorkBookDemo.main(WorkBookDemo.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 13 more
I added following jars:
- xmlbeans-2.4.0
- poi-ooxml-schemas-3.11
- poi-3.11
- commons-logging-1.1
- dom4j-1.6.1
log4j-1.2.17
import java.io.File; import java.io.FileOutputStream; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WorkBookDemo { public static void main(String[] args) { //Blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet sheet = workbook.createSheet("Employee Data"); //This data needs to be written (Object[]) Map<String, Object[]> data = new TreeMap<String, Object[]>(); data.put("1", new Object[] {"ID", "NAME", "LASTNAME"}); data.put("2", new Object[] {1, "Amit", "Shukla"}); data.put("3", new Object[] {2, "Lokesh", "Gupta"}); data.put("4", new Object[] {3, "John", "Adwards"}); data.put("5", new Object[] {4, "Brian", "Schultz"}); //Iterate over data and write to sheet Set<String> keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object [] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if(obj instanceof String) cell.setCellValue((String)obj); else if(obj instanceof Integer) cell.setCellValue((Integer)obj); } } try { //Write the workbook in file system FileOutputStream out = new FileOutputStream(new File("exps.xlsx")); workbook.write(out); out.close(); System.out.println("exps.xlsx written successfully on disk."); } catch (Exception e) { e.printStackTrace(); } } }
回答1:
FileOutputStream out = new FileOutputStream(new File("exps.xlsx"));
A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
Since you are explicitly creating a File
object, and passing the same to FileOutputStream
constructor. It assumes that the file "exps.xlsx"
is already created. [Reference.]
Incase, it is not, you simply pass the name of the file in FileOutputStream
constructor.
FileOutputStream out = new FileOutputStream("exps.xlsx");
This will automatically create the file, incase it is already not created.
Moreover, I tested your given code and used the following Maven
dependency and it worked.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10</version>
</dependency>
Incase you are not using a maven
project, you can explicitly add the aforementioned jars
of the aforementioned versions.
回答2:
You have missing jar-files. I ran your code in my workspace and I added required jars to build path. It worked successively.
org/apache/poi/UnsupportedFileFormatException is under poi-x.xx-xxx-xx.jar
No error in your code it is correct. Add jars to build path.
回答3:
the below list of jars required for apachePOI library should all be of same version like shown below :
org.apache.poi.3.11
org.apache.poi-ooxml.3.11
org.apache.poi-ooxml-schemas.3.11
if the above jars of the same library are of different versions, then u will get the exception as "java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException"
回答4:
you can check for duplicate poi jars as well, suppose you need poi-ooxml-3.9. jar. but in your lib folder there is both poi-ooxml-3.11.jar, poi-ooxml-3.9.jar then remove poi-ooxml-3.11.jar, it should work then.
来源:https://stackoverflow.com/questions/28398763/exception-while-using-excel-workbook