org.apache.poi.POIXMLException目前不支持严格的OOXML,请参阅bug#57699

时间:2016-06-10 13:43:05

标签: java excel apache-poi

我想用java解析Excel文件,所以我使用apache poi库,这里你是maven依赖项:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.14</version>
</dependency>

这将包括一系列依赖项:

poi-ooxml-3.14.jar
poi-3.14.jar
commons-codec-1.10.jar
poi-ooxml-schemas-3.14.jar
xmlbeans-2.6.0.jar
stax-api-1.0.1.jar
curvesapi-1.03.jar

当我尝试使用以下代码读取Office 365 Excel文件(.xslx)时:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelConverter {

    public static void main(String[] args) throws Exception{
        String excelFilePath = "C:/temp/Book1.xlsx";
        File myFile = new File(excelFilePath);
        System.out.println("File exists: " + myFile.exists());
        FileInputStream inputStream = new FileInputStream(myFile);

        Workbook workbook = new XSSFWorkbook(inputStream);
   }
}

我收到了以下控制台消息:

File exists: true
Exception in thread "main" org.apache.poi.POIXMLException: Strict OOXML isn't currently supported, please see bug #57699
    at org.apache.poi.POIXMLDocumentPart.getPartFromOPCPackage(POIXMLDocumentPart.java:679)
    at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:122)
    at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:115)
    at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:61)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:273)
    at org.myCompany.excel.ExcelConverter.main(ExcelConverter.java:25)

您知道我该怎么做才能解决问题吗? 提前致谢

4 个答案:

答案 0 :(得分:4)

目前似乎没有其他解决方案:

  

请勿以“严格OOXML”格式保存电子表格。

答案 1 :(得分:1)

我使用@PJFanning的ooxml转换器https://github.com/pjfanning/ooxml-strict-converter的稍作修改的版本来检查和转换严格的Excel文件,然后使用POI读取它们。尽管我拥有的文件非常简单,但在有限的测试中它似乎可以正常工作。

答案 2 :(得分:0)

将文件另存为Excel Workbook(.xlsx)对我有用。

答案 3 :(得分:0)

excel-streaming-reader库现在具有Beta版功能,可通过在convertFromOoXmlStrict构建器中设置标志StreamingReader从Strict OOXML进行转换:

https://github.com/pjfanning/excel-streaming-reader/blob/aaf05d058c8e969e9737d27e6844b4a7d32f6fe6/src/main/java/com/github/pjfanning/xlsx/StreamingReader.java#L245