public class Down { @GetMapping("/a.htm") public void cooperation(HttpServletRequest request, HttpServletResponse response) { ServletOutputStream out = response.getOutputStream(); ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true); String fileName = new String(("UserInfo " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())) .getBytes(), "UTF-8"); Sheet sheet1 = new Sheet(1, 0); sheet1.setSheetName("第一个sheet"); writer.write0(getListString(), sheet1); writer.finish(); response.setContentType("multipart/form-data"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx"); out.flush(); } } }
Q
[alibaba/easyexcel]网页上下载excel,为什么下载的是zip格式的压缩包,而不是xlsx或者xls结尾的excel文件?
3
A
回答
5
参照一下#217
6
解决了,多谢。
如果有时间,请更新github上的下载示例,以免浪费不必要的时间
8
具体的下载示例代码如下。 `` public void cooperation(HttpServletRequest request, HttpServletResponse response) throws IOException { String fileName = new String(("UserInfo" + new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8"); response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx"); response.setContentType("multipart/form-data"); response.setCharacterEncoding("utf-8");
ServletOutputStream out = response.getOutputStream();
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一个sheet");
writer.write0(Arrays.asList(Arrays.asList("number1", "number2", "number3"), Arrays.asList("str1", "str2", "str3")), sheet1);
writer.finish();
out.flush();
}
6
解决了?我咋还是zip压缩包
7
你给我点详细信息。 或者你在看看,我上面的描述和你的有什么地方不同,确认过,还是不行在来找我
8
你把项目发给我看看。 如果不允许,请截取关于excel下载的部分代码
8
已经解决,我看原来代码把reponse设置放后边了,这样会导致失败,我把它提前了
2
好的
5
谢谢~,之前没详细看,拿过来就用了,发现他原来代码写的是有问题的