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?
you need to download poi-ooxml-3.9.jar
Cause of
NoClassDefFoundError
is: Definition of class specified in exception was need to execute the code, but it could not be found under the specified package.Root cause: Usually it turns out that either jar file containing that Class definition was added or it was not added to build path.
You can download apache POI jars for various releases here
I also think you do not import the poi-3.7.jar into your project! first, you should create a folder lib, then copy the jar package file to this lib,then switch to build path, you can google it.
As detailed on the Apache POI components page, to use the OOXML code like XSSF, you need both the
poi
andpoi-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!
In my case, it works only if your app contains these libraries:
You can download them here: here, all in the 'poi-bin-3.10.1-20140818.zip' archive.
And please do not try to install a newer version, it will not work (at least for Android), only 3.10.1 and lower.