alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.79k stars 7.59k forks source link

填充模板下载excel,后缀是xls,提示:excel无法打开文件,文件格式或扩展名无效 #4052

Open zJohny opened 1 week ago

zJohny commented 1 week ago

image

异常代码

private static void extracted(HttpServletResponse response,List<HashMap<String, Long>> maps) throws FileNotFoundException { InputStream resourceAsStream = GpCellsController.class.getResourceAsStream("/fill/exceltemplate/模板.xls"); if (resourceAsStream == null) { throw new FileNotFoundException("未找到模板文件"); } try(ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(resourceAsStream).excelType(ExcelTypeEnum.XLS).build()){ if(maps!=null){ maps.forEach(map->{ WriteSheet writeSheet1 = EasyExcel.writerSheet("月报表").build(); excelWriter.fill(map, writeSheet1); }); } String fileNamedownload = "详细信息" + System.currentTimeMillis()+".xls"; log.info("生成的报表名:{}",fileNamedownload); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName1= URLEncoder.encode(fileNamedownload,"UTF-8").replaceAll("\+","%20"); response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName1); excelWriter.finish(); } catch (IOException e) { throw new RuntimeException(e); }

异常提示

代码无报错,本地测试无误,部署到Linux上就会出现扩展名无效的现象,而且我设置的后缀是.xls,下载下来就变成.xlsx

问题描述

下载下来之后,文件名也不正常,应该是“负债表1732104616044.xls”,而实际上是“负债表.xls”,时间戳也没有了

zJohny commented 1 week ago

问题已解决,不是easy excel的问题,是我们前端把文件流重新命名了。