1、response方式返回excel文件流
@GetMapping("/exportExcel") public void exportExcel(@RequestParam(value = "menu") String menu, @RequestParam(value = "dwflglId") int dwflglId, @RequestParam(value = "qjCode") String qjCode, HttpServletResponse response) throws IOException { //内容集合 List<ZjlrysIndexView> jthz = getJthz(dwflglId, qjCode); //表头集合 List<ZjlrysTableHead> header = getHeaderOfHz(dwflglId, qjCode); response.setHeader("Content-Disposition", "attachment; filename=Zjlryshz.xlsx"); // 响应类型,编码 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); String bigTitle = "资金流入预算表"; EasyExcel.write(response.getOutputStream()).head(getExcelHeader(header, bigTitle)).registerWriteHandler(new Custemhandler()).sheet("资金流入预算导出模板").doWrite(getExportData(jthz)); }
2、通过在服务器中生成excel临时文件,返回下载链接给前端下载
// 获取当前时间 String updTm = DateUtil.getAllTime(); //filePath为服务器存放临时文件的路径,本地开发环境的时候先可以用本地电脑的路径,比如c:users/,这里加上时间是因为不想被覆盖,也可以加uuid之类的 FileOutputStream fileOutputStream = new FileOutputStream(filePath + "xx导出模板" + updTm + ".xlsx"); EasyExcel.write(fileOutputStream).head(getDownLoadExcelHead()) .registerWriteHandler(new CustomSheetWriteHandler(map)).registerWriteHandler(new Custemhandler()) .sheet("数据字典导出模板").doWrite(downLoadExcelData); //url_prefix为服务器的域名之类的下载路径 String url = url_prefix + "xx导出模板" + updTm + ".xlsx"; /* * try { * * // 若不进行编码在IE下会乱码 * * url = URLEncoder.encode(url, "UTF-8"); * * } catch (UnsupportedEncodingException e) { * * e.printStackTrace(); * * } */
3、以流的形式上传到服务器
//创建流 ByteArrayOutputStream bos=new ByteArrayOutputStream(); //生成easyexcel的流 EasyExcel.write(bos).head(getHead()).sheet(sheetName).doWrite(getData()); //然后把bos上传到服务器,这里我是写了上传工具类,返回下载链接 String path=OssClientUtil.upload(fileName,bos.toByteArray());
参考网址:https://www.cnblogs.com/chenchengxuyuan/p/14374057.html