本文介绍了使用POI异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用POI 3.7,上载文件的.xlsx

控制台显示:

  org.apache.poi.POIXMLException:org.apache.poi.openxml4j.exceptions.InvalidFormatException:包装应包含的内容类型的一部分​​[M1.13]
    在org.apache.poi.util.PackageHelper.open(PackageHelper.java:41)
    在org.apache.poi.xssf.usermodel.XSSFWorkbook<&初始化GT;(XSSFWorkbook.java:186)
    在poi.POITest.ReadAndPrintExcelFile(POITest.java:15)
    在poi.POITest.main(POITest.java:59)
org.apache.poi.openxml4j.exceptions.InvalidFormatException:引起包装应包含的内容类型的一部分​​[M1.13]
    在org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:147)
    在org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:592)
    在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:222)
    在org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
    ... 3个


解决方案

只需使用
org.apache.poi.ss.usermodel.WorkbookFactory ,而不是创建实例:新HSSFWorkbook()或新XSSFWorkbook()

 工作簿exWorkBook = WorkbookFactory.create(excelInputStream);

I'm using poi 3.7 , upload the file is .xlsx

The console show:

org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:186)
    at poi.POITest.ReadAndPrintExcelFile(POITest.java:15)
    at poi.POITest.main(POITest.java:59)
Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:147)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:592)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:222)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
    ... 3 more
解决方案

Just use org.apache.poi.ss.usermodel.WorkbookFactory instead of creating instances : new HSSFWorkbook() or new XSSFWorkbook().

Workbook exWorkBook = WorkbookFactory.create(excelInputStream);

这篇关于使用POI异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:29