1.引入依赖
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency>
2. 工具类
package com.jeesite.modules.common.utils; import freemarker.template.Configuration; import freemarker.template.Template; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; public class fltWordUtils { private static Configuration configuration = null; private static final String templateFolder = fltWordUtils.class.getClassLoader().getResource("").getPath(); static { configuration = new Configuration(Configuration.VERSION_2_3_23); configuration.setDefaultEncoding("utf-8"); try { configuration.setDirectoryForTemplateLoading(new File(templateFolder)); } catch (IOException e) { e.printStackTrace(); } } private fltWordUtils() { throw new AssertionError(); } public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile) throws IOException { //ftl模板文件 //这里需要将模板文件放在项目的resources目录下 configuration.setClassForTemplateLoading(fltWordUtils.class,"/templatesWord"); //获取模板 Template template = configuration.getTemplate(ftlFile); File file = null; InputStream fin = null; ServletOutputStream out = null; try { // 调用工具类的createDoc方法生成Word文档 file = createDoc(map, template); fin = new FileInputStream(file); Date date = new Date(); // 创建date对象名 SimpleDateFormat nowright = new SimpleDateFormat("yyyyMMdd"); // 调用SimpleDateFormat的有参构造且赋值模板 String format = nowright.format(date); // 调用格式化方法 String fileName = title + format + ".doc"; response.setCharacterEncoding("utf-8"); response.setContentType("application/msword"); //response.setContentType("application/octet-stream"); // 设置浏览器以下载的方式处理该文件名 response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8")))); out = response.getOutputStream(); byte[] buffer = new byte[512]; // 缓冲区 int bytesToRead = -1; // 通过循环将读入的Word文件的内容输出到浏览器中 while ((bytesToRead = fin.read(buffer)) != -1) { out.write(buffer, 0, bytesToRead); } } finally { if (fin != null) { fin.close(); } if (out != null) { out.close(); } if (file != null) { file.delete(); } // 删除临时文件 } } //渲染文件 private static File createDoc(Map<?, ?> dataMap, Template template) { String name = "test.doc"; File f = new File(name); Template t = template; try { // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开 Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); t.process(dataMap, w); w.close(); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } return f; } }
3.制作模版
先把word模版转成后缀xml ,导入idea,改后缀ftl ,格式化,将数据改成${}形式
ftl 基本语法参考地址
FTL基本语法_ftl语法_如之奈何的博客-CSDN博客
https://www.cnblogs.com/mjtabu/p/14370912.html
4. controller
接收参数 调用工具类
Map<String, Object> map = new HashMap<String, Object>();
把参数放入map
fltWordUtils.exportMillCertificateWord(request, response, map, "文件名称", "template.ftl");
5. 前端调用 form表单提交 传参 不需要返回值
var barBase64Info; 全局变量
barBase64Info = myChart.getDataURL(); 渲染echarts 里面 注意
animation: false, 添加到series,否则获取不到echarts数据
代码参考 java使用ftl模板导出word文档_ftl文件转word_马大厨下饭的博客-CSDN博客
以freemarker导出echarts图表到word文档_spring boot freemarker 导出word 带echarts图形报表_呼延一觅儿的博客-CSDN博客