https://github.com/centic9/poi-on-android
This is a sample Android application to show howApache POI can be used on Android.
It consists of two projects:
- poishadow: A small helper project to producea shaded jar-file for Apache POI which includesall necessary dependencies and fixes a few thingsthat usually hinder you deploying Apache POI onAndroid
- poitest: A very small sample Android applicationwhich performs some actions on an XLSX-file usingApache POI. See
DocumentListActivity
for the actualcode
Getting started
Necessary System-Properties
In order to work around problems with finding a suitable XML Parser, currentlythe following system properties need to be set manually during startup of yourapplication (let me know if you know of a better way to do this, see issue #10)
System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");
Use a pre-built jar
If you want to get started quickly, there is a ready-made jar-file available in therelease section.
You should be able to simply add this to your Android project and use the Apache POIclasses from it.
Build the jar yourself
If you would like to change how the jar-file is built, e.g. if you need classes thatare excluded, use a different version of POI or would like to adjust the build in someother way, you can build the shaded jar with the following steps:
Preparation:
You will need the following pieces in order to get started
- A recent Java SDK, preferably Java 8
- An installation of the Android SDK, either the one includedwith Android Studio or a separate download, seehttps://developer.android.com/studio/index.html#downloads
Get the code:
git clone git://github.com/centic9/poi-on-android
cd poi-on-android
Configure where your Android SDK resides:
echo "sdk.dir=/opt/android-sdk-linux" > local.properties
Configure the version of the Android Build Tools that you have installed.
vi poitest/build.gradle
Finally run the build and some testing. Make sure you have a device connected, e.g. the Android emulator.
./gradlew build connectedCheck
For only the jar-files run just build
Run the Android emulator
List available emulators
<android-sdk>/tools/emulator -list-avds
Start an Android emulator
<android-sdk>/tools/emulator -avd <name>
Install the apk
<android-sdk>/platform-tools/adb install ./poitest/build/outputs/apk/poitest-debug.apk
Notes
- You can use the resulting jar-file
poishadow/build/libs/poishadow-all.jar
in your own project, the code in directorypoitest
isjust a small sample Android application to show that it works. - This was only tested in Android Studio with the Androidemulator until now, should work on real Android as well, though!
- Tested with
targetSdkVersion 25
andminSdkVersion 15
,although other versions should work as long as they supportmultiDexEnabled true
Todo
- Add more actual functionality to the sample application,currently it just creates a new spreadsheet, adds some datathen stores it in the Application storage area and reads itagain from there.
Links
- https://github.com/FasterXML/aalto-xml
- https://github.com/johnrengelman/shadow
- http://www.mysamplecode.com/2011/10/android-read-write-excel-file-using.html
- http://blog.kondratev.pro/2015/08/reading-xlsx-on-android-4-and-hopefully.html
- https://github.com/andruhon/android5xlsx
- http://stackoverflow.com/questions/8493507/trying-to-port-apache-poi-to-android
- http://www.cuelogic.com/blog/creatingreading-an-excel-file-in-android/
- http://en.b-s-b.info/office/excel/java-excel-poi.html
Licensing
Copyright 2015-2017 Dominik Stadler
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
https://github.com/andruhon/android5xlsx
#Reading and Writing XLSX on Android 5Reading and Writing XLSX and XLS on Android 5 with Apache POI
It was quite a challenging task to use Apache POI on android with Dalvik VM. It is much easier to use Apache POI on Android 5+ with ART VM and Build Tools 21+
Please refer to https://github.com/andruhon/AndroidReadXLSX if you need to read XLSX on Android 4 with Dalvik VM.
#ContentsI've repacked poi-ooxml to contain all dependencies in order to read and write XLSX (XLS as well, obviously)All javax classes from the STAX library and calls to them are renamed to aavax, to avoid --core-library warning and conflicts.
- poi-3.12-android-a.jar //Repacked POI with all dependencies
- poi-ooxml-schemas-3.12-20150511-a.jar //original schemas jar
Copy these two jars into your project's libs directory and use gradle config similar to build.gradle from this repo.
It will also work if you use all original files from https://poi.apache.org/download.html, however you need to re-pack xmlbeans-2.6.0.jar because for some reason it contains duplicates and Android does not like it. See below for contents of jar and details of javax -> aavax hack.
#ConfigurationPlease find gradle app configuration with comments in the build.gradle, the crucial thing is to enable multi-dex.
#Auto-downloading dependenciesThis might also be achieved by adding 'org.apache.poi:poi-ooxml:3.12' into dependencies, but it is not very straightforward because it is required to add some routine to re-pack xmlbeans and disable preDex. It will work. however the build process will be very slow, so it is easier just to prepare jars once and put them into libs directory (as described above)
#Usage examplePlease find usage example in example
#Contents of poi-3.12 jar
- poi-3.12-20150511.jar
- poi-ooxml-3.12-20150511.jar
- stax-1.2.0.jar
- stax-api-1.0.1.jar
- xmlbeans-2.6.0.jar //this one contained duplicates, Android does not like it
#javax -> aavax hackthe following classes and calls to them were renamed (as HEX strings + renaming namespace dir javax to aavax):
- javax/xml/namespace -> aavax/xml/namespace
- javax.xml.namespace -> aavax.xml.namespace
- javax/xml/stream -> aavax/xml/stream
- javax.xml.stream -> aavax.xml.stream
- javax/xml/XMLConstants -> aavax/xml/XMLConstant
- javax.xml.XMLConstants -> aavax.xml.XMLConstants
Please note that you'd better not replace all javax to aavax, because there might be calls other classes from javax package which are available in android.