关于springboot通过jxls用模板导出excel的前后台实现
后端代码
public void exportExcel(@RequestParam(required = true) String Id, HttpServletResponse response, HttpServletRequest request) { String msg =""; try { Dto.setId(Id); List<HashMap<String, Object>> list = tbsMainInfoService.exportExcel(Dto); //文件名称 String filename = "导出目录"; String agent = request.getHeader("User-Agent"); String filenameEncoder = ""; if (agent.contains("MSIE")) { // IE浏览器 filenameEncoder = URLEncoder.encode(filename, "utf-8"); filenameEncoder = filenameEncoder.replace("+", " "); } else if (agent.contains("Firefox")) { // 火狐浏览器 BASE64Encoder base64Encoder = new BASE64Encoder(); filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // 其它浏览器 filenameEncoder = URLEncoder.encode(filename, "utf-8"); } //获取配置路径 String url = FtpClientUtil.getStaticFile() + "/" + "目录模板.xlsx"; //获取文件流 InputStream in = new FileInputStream(url); // 创建操作excel文件对象 XLSTransformer transformer = new XLSTransformer(); // 设置ContentType请求信息格式 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + filenameEncoder + ".xlsx"); // 要下载的这个文件的类型-----客户端通过文件的MIME类型去区分类型 response.setContentType(request.getServletContext().getMimeType(filename)); ServletOutputStream outputStream = response.getOutputStream(); Map<String, Object> map = new HashMap<String, Object>(); map.put("list", list); Workbook workbook = transformer.transformXLS(in, map); workbook.write(outputStream); workbook.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); msg = e.getMessage(); throw new BusinessException("导出失败"); } }
模板
前台vue代码
Export() { let api ="" let a = document.createElement(\'a\'); a.href = api; a.click(); },
版权声明:本文为dragonKings原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。