java.lang.NoClassDefFoundError的同时创造使用Apache POI一个.xlsx文件文件、NoClassDefFoundError、lang、java

2023-09-03 23:19:51 作者:男人调情是天性‰

我想创建使用Apache POI一个.xlsx文件。这是我的code:

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();
}

我运行code当出现以下情况例外:

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)

我现在用的是 POI-3.7.jar 。有什么问题吗?

推荐答案

至于详细的的Apache POI组件页面,使用OOXML code XSSF一样,你既需要 POI POI-OOXML 在classpath罐,以及它们的依赖关系。

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.

的Apache POI的最新版本,如写作,是3.10。我建议你​​去下载二进制发布包,在那里你会发现所有的POI罐子,以及他们所有的依赖关系。该组件页面会帮助你的工作,你需要的,但是对于XSSF短期工作答案是你基本上需要所有的人!

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!