- Читаем XLSX на Android при помощи Apache POI
- Читаем XLSX на Android 5 (ART) c Apache POI XSSF
- Apache poi android implementation
- Latest commit
- Git stats
- Files
- README.md
- About
- Apache POI — the Java API for Microsoft Documents
- Project News
- 1 November 2021 — POI 5.1.0 available
- 13 January 2021 — CVE-2021-23926 — XML External Entity (XXE) Processing in Apache XMLBeans versions prior to 3.0.0
- 20 October 2019 — CVE-2019-12415 — XML External Entity (XXE) Processing in Apache POI versions prior to 4.1.1
- 26 March 2019 — XMLBeans 3.1.0 available
- 11 January 2019 — Initial support for JDK 11
- Mission Statement
- Why should I use Apache POI?
- Components
- Contributing
Читаем XLSX на Android при помощи Apache POI
UPD (06.09.2014): Больше нет необходимости компилировать с опцией —core-library Сама по себе задача прочитать формат XLSX при помощи Apache POI XSSF совершенно не хитрая. Не хитрая ровно до тех пор, пока вы не решите подключить эту библиотеку к вашему приложению для Android. К сожалению, гугление ни к чему не привело — мне не удалось найти реальных инструкций или рекомендаций по использованию XSSF на Android. Вообще ни одного упоминания о том, что эта библиотека действительно может работать на Android.
HSSF(XLS) — работает без проблем, XSSF(XLSX) — не удалось ничего найти, только рекомендации обрабатывать все при помощи POI где-нибудь на сервере.
Далее раскрываю, в чем заключается проблема и как я её решил :
Дело в том, что в Android есть ограничение на 65К методов в приложении, а для работы XSSF необходим jar, содержащий Open XML Schemas, в котором даже в сокращенной версии для POI содержится около 67К методов.
Напрашивается мысль, что можно все это дело попытаться уменьшить при помощи proguard. Однако почему-то никак не получалось сократить всю библиотеку так, чтобы приложение работало.
В итоге саму библиотеку уменьшил при помощи proguard, а схемы — вручную. Просто при помощи архиватора удалил из jar со схемами все файлы, которые, на мой взгляд, не нужны для чтения XLSX, после этого раз за разом запускал приложение, пытался читать файл и добавлял классы из-за отсутствия которых приложение падало. В конце концов — заработало!
Пример работающего приложения можно найти на GitHub: github.com/andruhon/AndroidReadXLSX
Для работы библиотеки и её зависимостей необходимы классы из javax, которых, к сожалению, нет в Android.
Здесь нас спасает , однако для того, чтобы скомпилировать приложение придется добавить опцию —core-library к DX.
UPD (06.09.2014):
Перекомпилировал StAX предварительно переименовав пространство имен «javax» в «aavax», после этого во всех остальных бинариках *.class заменил все строки ‘javax/xml/stream’, ‘javax/xml/namespace’ и ‘javax.xml.strings’ на соответствующие строки с «aavax». После этого «испорченый» stax-api запаковал в общий jar, все равно по отдельности работать не будет. Теперь вы можете просто скопировать два JAR файла в ваш lib и наслаждаться без дополнительных танцев с бубном.
Использованный конфиг proguard (по большему счету делает только ‘-shrink’):
[на входе dom4j-1.6.1.jar, poi-3.10-FINAL-20140208.jar, poi-ooxml-3.10-FINAL-20140208.jar, xmlbeans-2.3.0.jar]
Источник
Читаем XLSX на Android 5 (ART) c Apache POI XSSF
Некоторое время назад я писал, как при помощи языческих танцев и прочих паранормальных активностей я добился работы Apache POI XSSF на Android 4. Всё становится гораздо проще с Android Build Tools (21+) и Android 5 (ART).
Теперь достаточно собрать проект с поддержкой multi-dex и всё будет работать* на устройствах c ART. Я полагаю, что это происходит благодаря Ahead-of-time (AOT) compilation на устройстве и multi-dex, теперь, как таковой, нужен только как промежуточный этап.
* К сожалению не всё так гладко. В зависимостях проекта есть xmlbeans-2.6.0.jar, который запакован с ошибкой и содержит дубликаты классов. Это приводит к провалу сборки на задаче packageAllDebugClassesForMultiDex, со следующей ошибкой:
XMLBeans придётся перепаковать.
Этот дефект уже сто лет в обед как зарегистрирован issues.apache.org/jira/browse/XMLBEANS-499, однако воз и ныне там.
Для полноценной работы нам понадобятся следующие JAR файлы:
poi-3.12-20150511.jar
poi-ooxml-3.12-20150511.jar
poi-ooxml-schemas-3.12-20150511.jar
stax-1.2.0.jar
stax-api-1.0.1.jar
xmlbeans-2.6.0.jar — из-за этого файла не получится беззаботно добавить POI в зависимости проекта
Все вышеуказанные файлы доступны в загрузках POI archive.apache.org/dist/poi/release/bin
Чтобы всё заработало нужно:
- добавить com.android.support:multidex в зависимости проекта.
- Включить multiDex в defaultConfig
- для задачи com.android.build.gradle.tasks.Dex добавить параметр —core-library, чтобы избежать ошибок из за классов с namespace javax.
Если использовать gradle, то получим конфиг примерно следующего содержания:
Всё! Теперь мы имеем, предположительно, полностью функциональный POI на Android, который, опять таки, предположительно, может читать и все остальные openxml форматы, поскольку нам не пришлось обрезать содержимое jarов, как это было сделано в прошлый раз. Прошу любиить, экспериментировать и делиться результатами.
При желании можно также озадачиться тем, чтобы сделать javax->aavax хак, но в этот раз я счёл это лишним.
Источник
Apache poi android implementation
A sample project that shows how Apache POI can be used in an Android application
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This is a sample Android application to show how Apache POI can be used on Android.
It consists of two projects:
- poishadow: A small helper project to produce a shaded jar-file for Apache POI which includes all necessary dependencies and fixes a few things that usually hinder you deploying Apache POI on Android
- poitest: A very small sample Android application which performs some actions on an XLSX-file using Apache POI. See DocumentListActivity for the actual code
In order to work around problems with finding a suitable XML Parser, currently the following system properties need to be set manually during startup of your application (let me know if you know of a better way to do this, see issue #10)
Supported Android versions
The sample project uses API level 26, which maps to Android 8.0. Higher versions should work as well, older ones might not be supported any more.
Note: The dependencies of Apache POI are not included in the shading to keep it’s size at bay. If you use code areas which require commons-codec, commons-collections4 or any of the other dependencies, you will need to define them in your Android application in addition to the poishadow-all.jar file dependency.
Use a pre-built jar
If you want to get started quickly, there is a ready-made jar-file available in the release section.
You should be able to simply add this to your Android project and use the Apache POI classes from it.
Build the jar yourself
If you would like to change how the jar-file is built, e.g. if you need classes that are excluded, use a different version of POI or would like to adjust the build in some other way, you can build the shaded jar with the following steps:
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 included with Android Studio or a separate download, see https://developer.android.com/studio/index.html#downloads
Configure where your Android SDK resides:
Configure the version of the Android Build Tools that you have installed.
Then build the shadow-jar, for some reason this works better if executed separately:
Finally run the build and some testing. Make sure you have a device connected, e.g. the Android emulator.
For only the jar-files run just build
Support this project
If you find this tool useful and would like to support it, you can Sponsor the author
Run the Android emulator
List available emulators
Start an Android emulator
Install the apk
- You can use the resulting jar-file poishadow/build/libs/poishadow-all.jar in your own project, the code in directory poitest is just a small sample Android application to show that it works.
- This was only tested in Android Studio with the Android emulator until now, should work on real Android as well, though!
- Tested with targetSdkVersion 26 and minSdkVersion 26
- Add more actual functionality to the sample application, currently it just creates a new spreadsheet, adds some data then stores it in the Application storage area and reads it again from there.
Copyright 2015-2021 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, software distributed 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 and limitations under the License.
About
A sample project that shows how Apache POI can be used in an Android application
Источник
Apache POI — the Java API for Microsoft Documents
Project News
1 November 2021 — POI 5.1.0 available
The Apache POI team is pleased to announce the release of 5.1.0. Several dependencies were updated to their latest versions to pick up security fixes and other improvements.
A summary of changes is available in the Release Notes. A full list of changes is available in the change log. People interested should also follow the dev list to track progress.
See the downloads page for more details.
POI requires Java 8 or newer since version 4.0.1.
13 January 2021 — CVE-2021-23926 — XML External Entity (XXE) Processing in Apache XMLBeans versions prior to 3.0.0
Description:
When parsing XML files using XMLBeans 2.6.0 or below, the underlying parser created by XMLBeans could be susceptible to XML External Entity (XXE) attacks.
This issue was fixed a few years ago but on review, we decided we should have a CVE to raise awareness of the issue.
Mitigation:
Affected users are advised to update to Apache XMLBeans 3.0.0 or above which fixes this vulnerability. XMLBeans 4.0.0 or above is preferable.
20 October 2019 — CVE-2019-12415 — XML External Entity (XXE) Processing in Apache POI versions prior to 4.1.1
Description:
When using the tool XSSFExportToXml to convert user-provided Microsoft Excel documents, a specially crafted document can allow an attacker to read files from the local filesystem or from internal network resources via XML External Entity (XXE) Processing.
Mitigation:
Apache POI 4.1.0 and before: users who do not use the tool XSSFExportToXml are not affected. Affected users are advised to update to Apache POI 4.1.1 which fixes this vulnerability.
Credit: This issue was discovered by Artem Smotrakov from SAP
26 March 2019 — XMLBeans 3.1.0 available
The Apache POI team is pleased to announce the release of XMLBeans 3.1.0. Featured are a handful of bug fixes.
The Apache POI project has unretired the XMLBeans codebase and is maintaining it as a sub-project, due to its importance in the poi-ooxml codebase.
A summary of changes is available in the Release Notes. People interested should also follow the POI dev list to track progress.
The XMLBeans JIRA project has been reopened and feel free to open issues.
POI 4.1.0 uses XMLBeans 3.1.0.
XMLBeans requires Java 6 or newer since version 3.0.2.
11 January 2019 — Initial support for JDK 11
We did some work to verify that compilation with Java 11 is working and that all unit-tests pass.
See the details in the FAQ entry.
Mission Statement
The Apache POI Project’s mission is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2). In short, you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is your Java Excel solution (for Excel 97-2008). We have a complete API for porting other OOXML and OLE2 formats and welcome others to participate.
OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats. The project provides APIs for the OLE2 Filesystem (POIFS) and OLE2 Document Properties (HPSF).
Office OpenXML Format is the new standards based XML file format found in Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX. The project provides a low level API to support the Open Packaging Conventions using openxml4j.
For each MS Office application there exists a component module that attempts to provide a common high level Java api to both OLE2 and OOXML document formats. This is most developed for Excel workbooks (SS=HSSF+XSSF). Work is progressing for Word documents (WP=HWPF+XWPF) and PowerPoint presentations (SL=HSLF+XSLF).
The project has some support for Outlook (HSMF). Microsoft opened the specifications to this format in October 2007. We would welcome contributions.
As a general policy we collaborate as much as possible with other projects to provide this functionality. Examples include: Cocoon for which there are serializers for HSSF; Open Office.org with whom we collaborate in documenting the XLS format; and Tika / Lucene, for which we provide format interpretors. When practical, we donate components directly to those projects for POI-enabling them.
Why should I use Apache POI?
A major use of the Apache POI api is for Text Extraction applications such as web spiders, index builders, and content management systems.
So why should you use POIFS, HSSF or XSSF?
You’d use POIFS if you had a document written in OLE 2 Compound Document Format, probably written using MFC, that you needed to read in Java. Alternatively, you’d use POIFS to write OLE 2 Compound Document Format if you needed to inter-operate with software running on the Windows platform. We are not just bragging when we say that POIFS is the most complete and correct implementation of this file format to date!
You’d use HSSF if you needed to read or write an Excel file using Java (XLS). You’d use XSSF if you need to read or write an OOXML Excel file using Java (XLSX). The combined SS interface allows you to easily read and write all kinds of Excel files (XLS and XLSX) using Java. Additionally there is a specialized SXSSF implementation which allows to write very large Excel (XLSX) files in a memory optimized way.
Components
The Apache POI Project provides several component modules some of which may not be of interest to you. Use the information on our Components page to determine which jar files to include in your classpath.
Contributing
So you’d like to contribute to the project? Great! We need enthusiastic, hard-working, talented folks to help us on the project, no matter your background. So if you’re motivated, ready, and have the time: Download the source from the Subversion Repository, build the code, join the mailing lists, and we’ll be happy to help you get started on the project!
Please read our Contribution Guidelines. When your contribution is ready submit a patch to our Bug Database.
Источник