简体   繁体   English

使用Excel工作簿时出现异常

[英]exception while using excel workbook

I am getting these exceptions in my code while im writing some data in excel workbook using poi jars: 我在使用poi jars在excel工作簿中写入一些数据时,在代码中得到了这些异常:

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: 我添加了以下罐子:

  1. xmlbeans-2.4.0 xmlbeans-2.4.0
  2. poi-ooxml-schemas-3.11 poi-ooxml-schemas-3.11
  3. poi-3.11 poi-3.11
  4. commons-logging-1.1 commons-logging-1.1
  5. dom4j-1.6.1 dom4j-1.6.1
  6. log4j-1.2.17 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(); } } } 

You have missing jar-files. 您缺少jar文件。 I ran your code in my workspace and I added required jars to build path. 我在工作区中运行了您的代码,并添加了所需的jar来构建路径。 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. 添加罐子以构建路径。

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. 文件输出流是用于将数据写入文件或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. 特别是某些平台,一次只允许一个FileOutputStream(或其他文件写入对象)打开一个文件进行写入。 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. 由于您正在显式创建File对象,并将其传递给FileOutputStream构造函数。 It assumes that the file "exps.xlsx" is already created. 假定文件"exps.xlsx"已经创建。 [Reference.] [参考。]

Incase, it is not, you simply pass the name of the file in FileOutputStream constructor. 如果不是这样,您只需在FileOutputStream构造函数中传递文件名FileOutputStream

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. 此外,我测试了您的给定代码,并使用了以下Maven依赖关系,并且可以正常工作。

<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. 如果您不使用maven项目,则可以显式添加上述版本的上述jars

the below list of jars required for apachePOI library should all be of same version like shown below : apachePOI库所需的jar的以下列表应均为相同版本,如下所示:

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" 如果同一库的上述jar版本不同,则您将得到异常,例如“ java.lang.NoClassDefFoundError:org / apache / poi / UnsupportedFileFormatException”

you can check for duplicate poi jars as well, suppose you need poi-ooxml-3.9. 您也可以检查是否有重复的poi jar,假设您需要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. 但是在您的lib文件夹中,既有poi-ooxml-3.11.jar,又有poi-ooxml-3.9.jar,然后删除poi-ooxml-3.11.jar,则它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM