'Dependencies for using Apache POI to read excel files in Android
I want to use Apache POI to read and write excel files (both xlsx and xls) in Android. But I cannot resolve dependency issues that I currently have.
If I have only these two dependencies in my gradle file
compile files('libs/poi-3.12-20150511.jar')
compile files('libs/poi-ooxml-3.12-20150511.jar')
Then I get the following error:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/xmlbeans/XmlOptions;
at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:59)
at com.gargzdai.spreadsheet.MainActivity.readData(MainActivity.java:131)
at com.gargzdai.spreadsheet.MainActivity.prepareForReadingData(MainActivity.java:109)
at com.gargzdai.spreadsheet.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
This is the code I'm using to read excel files:
if (isXlsxFile)
myWorkbook = new XSSFWorkbook(OPCPackage.open(file));
else
myWorkbook = new HSSFWorkbook(fileStream);
Sheet mySheet = myWorkbook.getSheetAt(0);
Iterator rowIterator = mySheet.rowIterator();
It seems that I should add xmlbeans library. After I add this dependency to my gradle file:
compile files('libs/xmlbeans-2.6.0.jar')
After I add this dependency I get the following error during gradle build:
Error:Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_40\bin\java.exe'' finished with non-zero exit value 1
Any ideas on how should I approach this problem?
Solution 1:[1]
This worked for me: dependencies {
implementation 'org.apache.poi:poi:3.12'
implementation 'org.apache.poi:poi-ooxml:3.12'
implementation 'com.fasterxml:aalto-xml:1.0.0'}
Solution 2:[2]
Just add these two dependencies in your build.gradle module file
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
implementation 'javax.xml.stream:stax-api:1.0'
Solution 3:[3]
You need to remember to sync the gradle file after saving it. That solved the issue for me. The sync button appeared at the top of the gradle file after a change was made.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Aman Pasricha |
| Solution 3 | Doh09 |

