1

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?

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
SoulRayder
  • 5,072
  • 6
  • 47
  • 93

5 Answers5

2

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!

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
0

you need to download poi-ooxml-3.9.jar

Ashish
  • 1,943
  • 2
  • 14
  • 17
  • when i run this, I get a java.lang.VerifyError. I have posted a similar query here . http://stackoverflow.com/questions/21872019/creating-a-docx-file-using-poi-ooxml-jar-file Please help out. – SoulRayder Feb 19 '14 at 09:26
  • I took care of that, but now this is what happens : http://stackoverflow.com/questions/21877532/how-to-create-docx-files-and-xlsx-files – SoulRayder Feb 19 '14 at 10:22
  • See my edited question here : http://stackoverflow.com/questions/21877532/how-to-create-docx-files-and-xlsx-files – SoulRayder Feb 21 '14 at 10:30
0

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

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
0

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.

0

In my case, it works only if your app contains these libraries:

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')

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.

Pavel Kataykin
  • 1,527
  • 15
  • 14