明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

用POI导入xls格式的excel正常,xlsx格式的文件报错:Package should contain a content type part [M1.13]

解决方案:

private <S, T> Pair<S, T> readExcel(File file, Function<Workbook, Pair<S, T>> handler) {
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            if (!fis.markSupported()) {
                fis = new PushbackInputStream(fis, 8);
            }
            if (POIFSFileSystem.hasPOIFSHeader(fis)) {
                return handler.apply(new HSSFWorkbook(fis));  //WPS创建文档
            }
            if (POIXMLDocument.hasOOXMLHeader(fis)) {
                return handler.apply(new XSSFWorkbook(OPCPackage.open(fis)));  //office创建文档
            }
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis);
        }
        return null;
    }

原因在于WPS创建文档和office创建文档, 生成的Workbook有所不同。