@CrossOrigin @GetMapping("/listAll") @ResponseBody @ApiOperation(value = "汇总", notes = "", response = Informant.class) @ApiImplicitParams({ @ApiImplicitParam(name = "datetimeBegin", value = "datetimeBegin", required = false), @ApiImplicitParam(name = "datetimeEnd", value = "datetimeEnd", required = false), @ApiImplicitParam(name = "type", value = "type", required = false), }) public Object listAll(String datetimeBegin, String datetimeEnd, String type) { StpUtil.checkPermission("27"); Informant informant = informantMapper.selectAll(datetimeBegin, datetimeEnd, type, null); return AjaxResult.OK(informant); } // 导出informant数据为Word文档 private @NotNull Map<String, Object> assertMap(Informant informant,String datetimeBegin,String datetimeEnd) { Map<String, Object> params = new HashMap<>(); // 获取informant中的zj值 params.put("zj", informant.getZj()); // 将科学计数法转换为两位小数字符串 params.put("sckk4", String.format("%.2f", informant.getSckk4())); return params; } /** * 将项目中的模板文件拷贝到根目录下 * @return */ private String copyTempFile(String templeFilePath) { InputStream inputStream = getClass().getClassLoader().getResourceAsStream(templeFilePath); String tempFileName = System.getProperty("user.home") + "/" + "ggtp.docx"; File tempFile = new File(tempFileName); try { FileUtils.copyInputStreamToFile(inputStream, tempFile); } catch (IOException e) { throw new RuntimeException(e); } return tempFile.getPath(); } private void down(HttpServletResponse response, String filePath, String realFileName) { String percentEncodedFileName = null; try { percentEncodedFileName = percentEncode(realFileName); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } StringBuilder contentDispositionValue = new StringBuilder(); contentDispositionValue.append("attachment; filename=").append(percentEncodedFileName).append(";").append("filename*=").append("utf-8''").append(percentEncodedFileName); response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename"); response.setHeader("Content-disposition", contentDispositionValue.toString()); response.setHeader("download-filename", percentEncodedFileName); try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath)); // 输出流 BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());) { byte[] buff = new byte[1024]; int len = 0; while ((len = bis.read(buff)) > 0) { bos.write(buff, 0, len); } } catch (Exception e) { e.printStackTrace(); } } /** * 百分号编码工具方法 * @param s 需要百分号编码的字符串 * @return 百分号编码后的字符串 */ public static String percentEncode(String s) throws UnsupportedEncodingException { String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); return encode.replaceAll("\\+", "%20"); } @RequestMapping("genera") public void genera(HttpServletResponse response,String type,String datetimeBegin,String datetimeEnd) { Informant informant = informantMapper.selectAll(datetimeBegin, datetimeEnd, type, null); //1.组装数据 Map<String, Object> params = assertMap(informant,datetimeBegin, datetimeEnd); //2.获取根目录,创建模板文件 String path = copyTempFile("word/ggtp.docx"); // String fileName = System.currentTimeMillis() + ".docx"; String fileName = datetimeBegin+"至"+datetimeEnd+"益企汇总情况统计表.docx"; String tmpPath = "D:\\" + fileName; try { //3.将模板文件写入到根目录 //4.编译模板,渲染数据 XWPFTemplate template = XWPFTemplate.compile(path).render(params); //5.写入到指定目录位置 FileOutputStream fos = new FileOutputStream(tmpPath); template.write(fos); fos.flush(); fos.close(); template.close(); //6.提供前端下载 down(response, tmpPath, fileName); } catch (Exception e) { e.printStackTrace(); } finally { //7.删除临时文件 File file = new File(tmpPath); file.delete(); File copyFile = new File(path); copyFile.delete(); } }