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

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

用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有所不同。

相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2018-09-21 SqlServer :实现树形结构递归查询(无限极分类)
2018-09-21 MySQL:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
点击右上角即可分享
微信分享提示