我一直在努力使上传(在一个旧的Web应用程序中)兼容xls和xlsx文件,xls运行正常,但上传xlsx会引发此错误:
SEVERE: Servlet.service() for servlet [MainServlet] in context with path [/timesheet] threw exception [Servlet execution threw an exception] with root cause
java.lang.AbstractMethodError: org.apache.crimson.tree.ElementNode2.getTextContent()Ljava/lang/String;
at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.readElement(PackagePropertiesUnmarshaller.java:146)
at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.loadCreated(PackagePropertiesUnmarshaller.java:162)
at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.unmarshall(PackagePropertiesUnmarshaller.java:124)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:788)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:144)
目前的poi lib版本是3.14,但我也试过3.16(同样的错误)和3.17,该应用程序目前正在使用Java 1.6
public List<AttendanceRecord> getAttendanceRecords(List<AttendanceRecordErrMsg> errorMessages) throws Exception {
Workbook wb = WorkbookFactory.create(new FileInputStream(arFile)); //app already throws an error upon reaching this line
List<AttendanceRecord> attendanceRecordList = new ArrayList<AttendanceRecord>();
出现这种错误的原因是因为Crimson Library包含在你的项目中,它只在Node
接口上提供实现(基于最新版本1.1.3的xml-apis 1.0.b2),并且没有实现根据引入的getTextContent()
方法。 Document Object Model (DOM) Level 3 Core Specification。
要修复此类错误,您可以执行以下操作之一:
1.从您的项目中删除Crimson Library。
2.指定javax.xml.parsers.DocumentBuilderFactory
系统属性以避免使用org.apache.crimson.jaxp.DocumentBuilderFactoryImpl
类添加以下行
System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");