itin
itin

Reputation: 450

Issue in reading XLS by POI on Android

I am facing an issue in reading an XLS file with POI in an Android project. To read the file I have done this:

InputStream is = null;
try {
    is = getInstrumentation().getContext().getAssets().open("abc.xlsx");
} catch (IOException e) {

}

This resolved the read issue but now I am facing another issue with this code:

XSSFWorkbook workbook = new XSSFWorkbook(is);
XSSFSheet sheet = workbook.getSheetAt(0);

This gives me the following error:

java.lang.NoClassDefFoundError: org.apache.poi.xssf.usermodel.XSSFWorkbook
at com.evernote.test.Xls_Reader.<init>(Xls_Reader.java:36)
at com.evernote.test.Main.readTestCaseXls(Main.java:87)
at com.evernote.test.Main.testCreateAccount(Main.java:62)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

I have included all necessary POI libraries and with those libraries it works fine in the Java project. The issue is in the Android project.

I have added this jar in project->properties->Javabuild path->Add external jar. I have included following jar poi-scratchpad-3.9-20121203, poi-ooxml-schemas-3.9-20121203, poi-ooxml-3.9-20121203, poi-ooxml-3.9, poi-excelant-3.9-20121203, poi-examples-3.9-20121203, poi-3.9-20121203, In order and export i have not checked the poi-ooxml-schemas-3.9-20121203 jar if i checked this then it give error {Dx trouble writing output: Too many methods: 66024; max is 65536. By package: 13 java.lang 1 java.lang.reflect}.

Thanks.

Upvotes: 1

Views: 194

Answers (2)

centic
centic

Reputation: 15872

Please note that there are a number of problems that you might run into when using Apache POI on Android. You can avoid the error about too many methods with a feature called "multiDex" in newer versions of Android.

There are currently two projects which try to solve most of the problems for you:

  1. https://github.com/andruhon/android5xlsx
  2. https://github.com/centic9/poi-on-android/ (mainted by me)

The first one is currently based on POI 3.12, whereas the second can be re-compiled with newer versions of POI more easily.

Upvotes: 1

itin
itin

Reputation: 450

if you get this exception even when you are sure that you have included all the jars then create a libs folder in the project root directory and copy all jars in libs folder. the add these jars in java build path (select all jars and right click add jars.)

Upvotes: 0

Related Questions