esaypoi导出excel后office打开报错

使用easyPOI导出Excel时遇到Office打开报错,经过排查,问题出在batik-transcoder依赖导致的jar冲突。通过设置response内容类型、检查流关闭、确认XSSF类型设置及依赖分析,最终发现并移除冲突的batik-transcoder依赖,成功解决导出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

esaypoi导出excel后office打开报错

**

使用esaypoi导出excel后office打开报错,提示 ”Excel 无法打开文件“导出表格 (1).xlsx”,因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。“

在这里插入图片描述

一脸懵逼,于是百度问度娘,大概的方案有以下几种:
1、XSSF的类型response设置为:
response.setContentType(“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(fileName, “UTF-8”));
查看设置已经是这个了,跳过继续找原因

   public static void successExcel(HttpServletResponse response,Workbook workbook,String fileName){
    ServletOutputStream out = null;
    try{
		out = response.getOutputStream();
        //response.setHeader("content-Type", "application/vnd.ms-excel");
        //response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
		workbook.write(out);
        out.flush();
    }catch (Exception e){
		log.error(e.getMessage(),e);
		throw new BusinessException("导出失败,发生未知错误,请联系系统管理员");
	}finally {
		out.close();
    }
}

2、xls格式的使用HSSF,xlsx的使用XSSF,发现跟我场景不符合,跳过继续找

3、ServletOutputStream out = response.getOutputStream(); 流没有out.flush()和out.close(),情况与自己不符合,跳过继续找

4、另外搭建一个项目,将easypoi相关jar导进去,启动然后下载,奇迹出现了,这次打开没问题,丫的那就是jar包依赖有问题了,网上看到 jop包会对excel导出有影响,于是使用maven命令查看依赖从哪里引入
maven命令: mvn dependency:tree
发现是batik-transcoder这个依赖在搞鬼导致jar冲突,影响到了excel导出,于是删掉重启,导出正常

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
处理方案:直接exclusion掉

     <dependency>
            <groupId>batik</groupId>
            <artifactId>batik-transcoder</artifactId>
            <version>1.6-1</version>
            <exclusions>
                <exclusion>
                    <groupId>fop</groupId>
                    <artifactId>fop</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值