我目前正在开发一个Android应用程序,其被读取需要/写数据从/到Excel电子表格(的.xls 或的.xlsx )。>
为了集成Android <-> Excel,我使用this poi library,主要是因为我在正确配置对原始Apache POI的依赖项方面遇到一些问题。
注意:在执行操作(单击按钮)时,实例化工作簿对象为this.workbook = new XSSFWorkbook(super.file);
以下是我的 build.gradle (模块)文件的内容:
apply plugin: 'com.android.application
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.ricardomiranda.expenses"
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Workaround - "Program type already present"
dexOptions {
preDexLibraries = false
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:27.1.1'
// Workaround - "com.bea.xml.stream.EventFactory not found"
implementation('com.fasterxml:aalto-xml:1.0.0'){
// Workaround - "Program type already present"
exclude module: 'stax'
exclude module: 'stax-api'
exclude module: 'xpp3'
exclude group: 'com.android.support'
exclude module: 'support-v7'
exclude module: 'fasterxml'
}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('libs/poishadow-all.jar')
}`
正如人们可以看到的那样,先前的代码片段包含一些变通办法(在注释中正确地突出显示了),以便尝试解决我面临的问题,但无济于事。
考虑前面的的build.gradle 代码段的没有解决方法
this.workbook = new XSSFWorkbook(super.file);
以及从poi库继承的其他一些类和方法-此按钮试图将数据写入Excel扩展名。但是,未应用预期的行为,因为执行中止并带有“致命异常”,例如:`
2019-01-31 18:35:14.377 20682-20682/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ricardomiranda.expenses, PID: 20682
org.apache.poi.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found
at org.apache.poi.javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72)
at org.apache.poi.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:178)
at org.apache.poi.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92)
at org.apache.poi.javax.xml.stream.XMLEventFactory.newInstance(XMLEventFactory.java:30)
at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:130)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:295)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:201)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:323)
at com.ricardomiranda.expenses.ExcelManagement.initializeWorkbook(ExcelManagement.java:141)
at com.ricardomiranda.expenses.ExcelManagement.<init>(ExcelManagement.java:51)
at com.ricardomiranda.expenses.AddExpense$1.onClick(AddExpense.java:44)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
为了修复该问题,并基于this comment,我在构建implementation('com.fasterxml:aalto-xml:1.0.0')
中添加了以下依赖项,并设置了所需的系统属性(也在本文档的Getting started部分中进行了介绍)库的自述文档)。
Error: Program type already present: com.fasterxml.aalto.ValidationException
据我所知这似乎是duplicated dependency problem,但我无法解析gradlew app:dependencies
(输出的范围太广了),类似的命令也不会输出任何配置。