使用Apache POI创建简单工作簿时出错

时间:2013-02-21 11:47:29

标签: java apache-poi xls excel

我正在尝试使用Apache POI创建一个简单的工作簿并获得以下错误。使用apihe poi 3.9请帮助我摆脱这种感谢。

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at xlscreate.xlsclass.main(xlsclass.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    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)
    ... 1 more

CODE:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;






public class xlsclass {

     public static void main(String[] args)  throws Exception,Throwable{

           // Workbook wb = new HSSFWorkbook();
           // FileOutputStream fileOut = new FileOutputStream("workbook.xls");
           // wb.write(fileOut);
           // fileOut.close();

            Workbook wb = new XSSFWorkbook();
            FileOutputStream fileOut = new FileOutputStream("c:/workbook.xlsx");
            wb.write(fileOut);
            fileOut.close();


     }
}

2 个答案:

答案 0 :(得分:1)

Apache POI有依赖关系。您需要查找它们并将它们添加到类路径中。其中一个依赖库包含此异常类:org.apache.xmlbeans.XmlException。不确定是哪一个。之后,其他答案包含代码中实际错误的提示。

答案 1 :(得分:1)

Apache POI有a number of dependencies。 Apache POI are given on the website

的每个部分的完整依赖关系列表

如果您使用Maven作为构建工具,那么当您根据Apache POI(已全部定义)定义代码时,它会自动吸收依赖关系。如果您使用Eclipse或Ant,那么您应该download Apache POI的Binary(-bin-)包,然后您就可以找到所有依赖项了。

(对于你的具体错误,你错过了xmlbeans jar,但我怀疑你也可能会遗漏一些其他的东西而且这是第一次命中)