**
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>