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?