java - 使用 Libre Office 更新 .xls 文件后,POI API 中的 org.apache.poi.POIXMLException : java. lang.reflect.InvocationTargetException

标签 java excel exception apache-poi

我正在使用 POI api 读取 Excel,我使用以下代码读取 excel 文件。

    public class ExcelWorkBook {

        private static ExcelWorkBook excelWorkBook = null;
        private static String filePath;
        private static XSSFWorkbook workbook;

        /**
         * This  constructor used to initialize the {@link ExcelWorkBook}
         * @throws IOException
         */
        private ExcelWorkBook() throws IOException {
            try {
                workbook = new XSSFWorkbook(getFile());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ExcelWorkBook.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        /**
         * This method create a new object {@link ExcelWorkBook}
         * @return {@link ExcelWorkBook}
         * @throws IOException
         */
        public static ExcelWorkBook getInstance() throws IOException {
            excelWorkBook = new ExcelWorkBook();
            return excelWorkBook;
        }

        /**
         * This method create a {@link FileInputStream}
         * @return {@link FileInputStream}
         * @throws FileNotFoundException
         */
        private FileInputStream getFile() throws FileNotFoundException {
          FileInputStream file = new FileInputStream(new File(filePath));
          return file;
        }
        .
        .
        .
        .

但它在使用 MS Office 2010 创建且使用 Libre office 未注明日期的特定文件中抛出以下异常。可能是什么问题呢。下面附上异常的Stack trace

 org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:61) ~[poi-ooxml-3.7.jar:3.7]
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:277) ~[poi-ooxml-3.7.jar:3.7]
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:282) ~[poi-ooxml-3.7.jar:3.7]
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:186) ~[poi-ooxml-3.7.jar:3.7]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:189) ~[poi-ooxml-3.7.jar:3.7]
    at com.excel.ExcelWorkBook.<init>(ExcelWorkBook.java:48) 
    at com.excel.ExcelWorkBook.getInstance(ExcelWorkBook.java:60) 
    at com.excel.ExcelParser.<init>(ExcelParser.java:64) 
    at com.database.DatabaseLoader.process(DatabaseLoader.java:85) 
    at com.fileuploader.FileUploadHandler.doPost(FileUploadHandler.java:116) 
.
.
.
.
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
[STDOUT] Caused by: java.lang.reflect.InvocationTargetException: null
[STDOUT]    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_45]
[STDOUT]    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_45]
[STDOUT]    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_45]
[STDOUT]    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_45]
[STDOUT]    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:59) ~[poi-ooxml-3.7.jar:3.7]
[STDOUT]    ... 42 common frames omitted
[STDOUT] Caused by: org.apache.xmlbeans.XmlException: error: duplicate attribute 'type'
[STDOUT]    at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3471) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    at org.apache.xmlbeans.XmlObject$Factory.parse(XmlObject.java:663) ~[xmlbeans-2.3.0.jar:2.3.0-r540734]
[STDOUT]    at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.read(XSSFVMLDrawing.java:107) ~[poi-ooxml-3.7.jar:3.7]
[STDOUT]    at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.<init>(XSSFVMLDrawing.java:102) ~[poi-ooxml-3.7.jar:3.7]
[STDOUT]    ... 47 common frames omitted
[STDOUT] Caused by: org.xml.sax.SAXParseException: duplicate attribute 'type'
[STDOUT]    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439) ~[xmlbeans-2.3.0.jar:na]
[STDOUT]    ... 53 common frames omitted

最佳答案

在我用 Libe Office 编辑一个包含评论的 .xlsx 文件后,我遇到了完全相同的异常。删除评论解决了这个问题。

关于java - 使用 Libre Office 更新 .xls 文件后,POI API 中的 org.apache.poi.POIXMLException : java. lang.reflect.InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364338/

相关文章:

java - Micronaut 和 JUnit 回滚

vba - Worksheet_Change 事件中的 Excel VBA 静态时间戳

java - 我无法弄清楚的扫描仪错误 : NoSuchElementException

python - 在 python 中有异常命中两个 except block

c++ - 捕获异常 : divide by zero

java - 如何在 Janusgraph 中演化模式?

java - 突出显示 JSF 数据表的行

java - 如何在点击 View 时加载不同的 View 类型?

java - 使用 Apache POI 4.1.1 从 excel 文件中读取值时出错

vba - 在 UDF 中设置时,在需要 range.cell 的范围内选择一个单元格