springboot导出Excel文件给浏览器下载

Java代码:

 1 BufferedOutputStream fos = null;
 2 try {
 3     response.setContentType("application/x-msdownload");
 4     response.setCharacterEncoding("UTF-8");
 5     response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("mock", "UTF-8") + ".xlsx");
 6     fos = new BufferedOutputStream(response.getOutputStream());
 7     xssfWorkbook.write(fos);
 8 } catch (Exception e) {
 9     log.error("excel生成报错:{}", e);
10 } finally {
11     if (fos != null) {
12         try {
13             fos.close();
14         } catch (IOException e) {
15             log.error("流对象关闭失败");
16         }
17     }
18 }

 

posted @ 2022-03-23 17:37  没有你哪有我  阅读(425)  评论(0)    收藏  举报