使用Apache POI创建.xlsx文件时的java.lang.NoClassDefFoundError

我正在尝试使用Apache POI创建.xlsx文件。 这是我的代码:

FileOutputStream outputStream1=null; XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("data"); try { target.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { outputStream1 = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/file_A/"+"test_Ab"+".xlsx"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i=0;i<iteration;i++) { XSSFRow row = sheet.createRow(i); Cell cell = row.createCell(0); cell.setCellValue("dsfasdf"); } try { FileOutputStream out = new FileOutputStream(target); workbook.write(out); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

运行代码时出现以下exception:

 02-19 11:48:13.387: E/AndroidRuntime(18736): FATAL EXCEPTION: main 02-19 11:48:13.387: E/AndroidRuntime(18736): java.lang.NoClassDefFoundError: org.apache.poi.xssf.usermodel.XSSFWorkbook 02-19 11:48:13.387: E/AndroidRuntime(18736): at com.example.filegenerator.MainActivity$2.onClick(MainActivity.java:248) 02-19 11:48:13.387: E/AndroidRuntime(18736): at android.view.View.performClick(View.java:4101) 02-19 11:48:13.387: E/AndroidRuntime(18736): at android.view.View$PerformClick.run(View.java:17088) 02-19 11:48:13.387: E/AndroidRuntime(18736): at android.os.Handler.handleCallback(Handler.java:615) 02-19 11:48:13.387: E/AndroidRuntime(18736): at android.os.Handler.dispatchMessage(Handler.java:92) 02-19 11:48:13.387: E/AndroidRuntime(18736): at android.os.Looper.loop(Looper.java:153) 02-19 11:48:13.387: E/AndroidRuntime(18736): at android.app.ActivityThread.main(ActivityThread.java:5096) 02-19 11:48:13.387: E/AndroidRuntime(18736): at java.lang.reflect.Method.invokeNative(Native Method) 02-19 11:48:13.387: E/AndroidRuntime(18736): at java.lang.reflect.Method.invoke(Method.java:511) 02-19 11:48:13.387: E/AndroidRuntime(18736): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 02-19 11:48:13.387: E/AndroidRuntime(18736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 02-19 11:48:13.387: E/AndroidRuntime(18736): at dalvik.system.NativeStart.main(Native Method) 

我正在使用poi-3.7.jar 。 这里有什么问题?

正如在Apache POI组件页面上详细说明的那样,要使用像XSSF这样的OOXML代码,您需要在类路径中包含poipoi-ooxml jar以及它们的依赖项。

截至编写时,最新版本的Apache POI为3.10。 我建议你去下载二进制发布包,在那里你会找到所有的POI jar,以及它们的所有依赖项。 组件页面将帮助您找到您需要的东西,但是对于使用XSSF,简短的答案是您基本上需要所有这些!

你需要下载poi-ooxml-3.9.jar

NoClassDefFoundError原因是:在exception中指定的类的定义需要执行代码,但是在指定的包下找不到它。

根本原因:通常会发现包含该类定义的jar文件已添加或未添加到构建路径中。

您可以在此处下载各种版本的apache POI jar

我也认为你不会将poi-3.7.jar导入到你的项目中! 首先,你应该创建一个文件夹lib,然后将jar包文件复制到这个lib,然后切换到构建路径,你可以google它。

在我的情况下,它仅适用于您的应用程序包含这些库:

 compile files('libs/dom4j-1.6.1.jar') compile files('libs/stax-api-1.0.1.jar') compile files('libs/xmlbeans-2.6.0.jar') compile files('libs/poi-3.10.1-20140818.jar') compile files('libs/poi-ooxml-3.10.1-20140818.jar') compile files('libs/poi-ooxml-schemas-3.10.1-20140818.jar') 

你可以在这里下载它们: 这里 ,全部在’poi-bin-3.10.1-20140818.zip’存档中。

并且请不要尝试安装更新的版本,它将无法工作(至少对于Android),只有3.10.1及更低版本。