使用 Apache POI 创建 .xlsx 文件时出现 java.lang.NoClassDefFoundError [英] java.lang.NoClassDefFoundError while creating a .xlsx file using Apache POI
问题描述
我正在尝试使用 Apache POI 创建 .xlsx 文件.这是我的代码:
FileOutputStream outputStream1=null;XSSFWorkbook 工作簿 = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet("data");尝试 {target.createNewFile();} catch (IOException e) {//TODO 自动生成的 catch 块e.printStackTrace();}尝试 {outputStream1 = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/file_A/"+"test_Ab"+".xlsx");} catch (FileNotFoundException e) {//TODO 自动生成的 catch 块e.printStackTrace();}for(int i=0;i<迭代;i++){XSSFRow 行 = sheet.createRow(i);单元格单元格 = row.createCell(0);cell.setCellValue("dsfasdf");}尝试 {FileOutputStream out = new FileOutputStream(target);workbook.write(out);关闭();} catch (IOException e) {//TODO 自动生成的 catch 块e.printStackTrace();}
运行代码时出现以下异常:
02-19 11:48:13.387: E/AndroidRuntime(18736): 致命异常: main02-19 11:48:13.387: E/AndroidRuntime(18736): java.lang.NoClassDefFoundError: org.apache.poi.xssf.usermodel.XSSFWorkbook02-19 11:48:13.387: E/AndroidRuntime(18736): 在 com.example.filegenerator.MainActivity$2.onClick(MainActivity.java:248)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 android.view.View.performClick(View.java:4101)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 android.view.View$PerformClick.run(View.java:17088)02-19 11:48:13.387:E/AndroidRuntime(18736):在 android.os.Handler.handleCallback(Handler.java:615)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 android.os.Handler.dispatchMessage(Handler.java:92)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 android.os.Looper.loop(Looper.java:153)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 android.app.ActivityThread.main(ActivityThread.java:5096)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 java.lang.reflect.Method.invokeNative(Native Method)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 java.lang.reflect.Method.invoke(Method.java:511)02-19 11:48:13.387:E/AndroidRuntime(18736):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)02-19 11:48:13.387: E/AndroidRuntime(18736): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)02-19 11:48:13.387:E/AndroidRuntime(18736):在 dalvik.system.NativeStart.main(Native Method)
我正在使用 poi-3.7.jar.这里有什么问题?
详见 Apache POI组件页面,要使用像 XSSF 这样的 OOXML 代码,您需要在类路径中同时使用 poi
和 poi-ooxml
jar,以及它们的依赖项.>
截至撰写本文时,Apache POI 的最新版本是 3.10.我建议你去下载二进制发布包,在那里你会找到所有的 POI jar 以及它们的所有依赖项.组件页面 将帮助您确定您需要的组件,但对于使用 XSSF 而言,简短的答案是您基本上需要所有这些!
I am trying to create a .xlsx file using Apache POI. This is my code:
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();
}
I get the following exception when run the code:
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)
I am using the poi-3.7.jar. What is the problem here?
As detailed on the Apache POI components page, to use the OOXML code like XSSF, you need both the poi
and poi-ooxml
jars on your classpath, along with their dependencies.
The latest version of Apache POI, as of writing, is 3.10. I'd suggest you go download the binary release package, in there you'll find all the POI jars, along with all their dependencies. The components page will help you work out which you need, but for working with XSSF the short answer is you'll basically need all of them!
这篇关于使用 Apache POI 创建 .xlsx 文件时出现 java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!