读取 XSSF 工作簿 (.xlsx) 导致应用程序崩溃 Android
Reading XSSF workbook (.xlsx) crashes Android app
问题: 我正在尝试读取 XLSX 文件以获取输入值。该代码适用于 XLS,但应用程序因 XLSX 文件而崩溃。但是 HSSF 和 XSSF 的实现似乎或多或少是一样的。以下是记录的错误。
到目前为止: 尝试了很多 SO 答案。下载并附加了以下罐子。但还是没区别。
xmlbeans-3.1.0.jar
poi-ooxml-4.1.0.jar
poi-ooxml-schemas-4.1.0.jar
commons-compress-1.18.jar
curvesapi-1.06.jar
poi-4.1.0.jar
commons-codec-1.12.jar
commons-collections4-4.3.jar
commons-math3-3.6.1.jar
代码:
if (GetFileExtension(FilePath).equals(".xls")) {
wb = new HSSFWorkbook(inStream);
sheet1 = wb.getSheetAt(0);
Formeval = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
}else if (GetFileExtension(FilePath).equals(".xlsx")) {
wb = new XSSFWorkbook(inStream);
sheet1 = wb.getSheetAt(0);
Formeval = new XSSFFormulaEvaluator((XSSFWorkbook) wb);
}
错误日志:
Process: com.example.newtest, PID: 14295
java.lang.BootstrapMethodError: Exception from call site #238 bootstrap method
at org.apache.poi.openxml4j.opc.PackagePartCollection.<init>(PackagePartCollection.java:47)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:241)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:732)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:310)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:47)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:309)
at com.example.newtest.DNsuper.onActivityResult(DNsuper.java:963)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NoClassDefFoundError: Invalid descriptor: AFTER.
at org.apache.poi.openxml4j.opc.PackagePartCollection.<init>(PackagePartCollection.java:47)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:241)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:732)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:310)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:47)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:309)
at com.example.newtest.DNsuper.onActivityResult(DNsuper.java:963)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Apache POI 和 Google 的 Android [长期悬而未决的场景 - 经过数周的研究后了解] 之间存在兼容性问题,这使得 XSSF[.XLSX] 读起来很复杂但并不复杂HSSF(.XLS)。向后移植到以前的库解决了这个问题。
库 - 依赖项: [JAVA Jars - GRADLE - MAVEN ] 使用以下版本并忽略更新消息。
对于 GRADLE:将其保留在 build.gradle(:app)
implementation "org.apache.poi:poi:3.10-FINAL"
implementation "org.apache.poi:poi-ooxml:3.10-FINAL"
implementation "org.apache.poi:poi-ooxml-schemas:3.10-FINAL"
implementation group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '2.3.0'
implementation group: 'stax', name: 'stax-api', version: '1.0.1'
implementation group: 'commons-codec', name: 'commons-codec', version: '1.5'
implementation group: 'xml-apis', name: 'xml-apis', version: '1.0.b2'
implementation group: 'dom4j', name: 'dom4j', version: '1.6.1'
implementation group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
.XLSX 和 .XLS Reader:(即分别为 XSSF 和 HSSF 工作簿
import android.content.ContentValues;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import java.util.Iterator;
public class Import2SQLite extends AppCompatActivity {
public static final String DNN = "DO";// 0 text(String)
public static final String ITT = "IT";// 1 integer
public static final String DNQ = "DN";// 2 text(String)
public static void ExcelToSqlite(ActiononDB dbAdapter, Sheet sheet) {
for (Iterator<Row> rit = sheet.rowIterator();
rit.hasNext(); ) {
Row row = rit.next();
ContentValues contentValues = new ContentValues();
row.getCell(0, Row.CREATE_NULL_AS_BLANK ).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(1, Row.CREATE_NULL_AS_BLANK).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(2, Row.CREATE_NULL_AS_BLANK).setCellType(Cell.CELL_TYPE_STRING);
contentValues.put(DNN, row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
contentValues.put(ITT, row.getCell(1, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
contentValues.put(DNQ, row.getCell(2, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
try {
if (dbAdapter.insert("Usertypes", contentValues) < 0) {
return;
}
} catch (Exception ex) {
Log.d("Exception in importing", ex.getMessage());
}
}
}
}
问题: 我正在尝试读取 XLSX 文件以获取输入值。该代码适用于 XLS,但应用程序因 XLSX 文件而崩溃。但是 HSSF 和 XSSF 的实现似乎或多或少是一样的。以下是记录的错误。
到目前为止: 尝试了很多 SO 答案。下载并附加了以下罐子。但还是没区别。
xmlbeans-3.1.0.jar
poi-ooxml-4.1.0.jar
poi-ooxml-schemas-4.1.0.jar
commons-compress-1.18.jar
curvesapi-1.06.jar
poi-4.1.0.jar
commons-codec-1.12.jar
commons-collections4-4.3.jar
commons-math3-3.6.1.jar
代码:
if (GetFileExtension(FilePath).equals(".xls")) {
wb = new HSSFWorkbook(inStream);
sheet1 = wb.getSheetAt(0);
Formeval = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
}else if (GetFileExtension(FilePath).equals(".xlsx")) {
wb = new XSSFWorkbook(inStream);
sheet1 = wb.getSheetAt(0);
Formeval = new XSSFFormulaEvaluator((XSSFWorkbook) wb);
}
错误日志:
Process: com.example.newtest, PID: 14295
java.lang.BootstrapMethodError: Exception from call site #238 bootstrap method
at org.apache.poi.openxml4j.opc.PackagePartCollection.<init>(PackagePartCollection.java:47)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:241)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:732)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:310)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:47)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:309)
at com.example.newtest.DNsuper.onActivityResult(DNsuper.java:963)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NoClassDefFoundError: Invalid descriptor: AFTER.
at org.apache.poi.openxml4j.opc.PackagePartCollection.<init>(PackagePartCollection.java:47)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:241)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:732)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:310)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:47)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:309)
at com.example.newtest.DNsuper.onActivityResult(DNsuper.java:963)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Apache POI 和 Google 的 Android [长期悬而未决的场景 - 经过数周的研究后了解] 之间存在兼容性问题,这使得 XSSF[.XLSX] 读起来很复杂但并不复杂HSSF(.XLS)。向后移植到以前的库解决了这个问题。
库 - 依赖项: [JAVA Jars - GRADLE - MAVEN ] 使用以下版本并忽略更新消息。
对于 GRADLE:将其保留在 build.gradle(:app)
implementation "org.apache.poi:poi:3.10-FINAL"
implementation "org.apache.poi:poi-ooxml:3.10-FINAL"
implementation "org.apache.poi:poi-ooxml-schemas:3.10-FINAL"
implementation group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '2.3.0'
implementation group: 'stax', name: 'stax-api', version: '1.0.1'
implementation group: 'commons-codec', name: 'commons-codec', version: '1.5'
implementation group: 'xml-apis', name: 'xml-apis', version: '1.0.b2'
implementation group: 'dom4j', name: 'dom4j', version: '1.6.1'
implementation group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
.XLSX 和 .XLS Reader:(即分别为 XSSF 和 HSSF 工作簿
import android.content.ContentValues;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import java.util.Iterator;
public class Import2SQLite extends AppCompatActivity {
public static final String DNN = "DO";// 0 text(String)
public static final String ITT = "IT";// 1 integer
public static final String DNQ = "DN";// 2 text(String)
public static void ExcelToSqlite(ActiononDB dbAdapter, Sheet sheet) {
for (Iterator<Row> rit = sheet.rowIterator();
rit.hasNext(); ) {
Row row = rit.next();
ContentValues contentValues = new ContentValues();
row.getCell(0, Row.CREATE_NULL_AS_BLANK ).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(1, Row.CREATE_NULL_AS_BLANK).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(2, Row.CREATE_NULL_AS_BLANK).setCellType(Cell.CELL_TYPE_STRING);
contentValues.put(DNN, row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
contentValues.put(ITT, row.getCell(1, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
contentValues.put(DNQ, row.getCell(2, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
try {
if (dbAdapter.insert("Usertypes", contentValues) < 0) {
return;
}
} catch (Exception ex) {
Log.d("Exception in importing", ex.getMessage());
}
}
}
}