兰州市城乡建设局网站,旅游网站策划营销,一级消防工程师考试试题及答案,二手车网站建设该方法也就是通过freemarker生成固定的word文档#xff0c;动态的word模板布局不能用该方法。
也就是必须有一个固定的模板文档是.ftl类型
如果初始文件为 需要手动改为#xff1a; 也就是所有需要替换的地方#xff0c;都需要有${XX}替换。 主要步骤为#xff1a;
将 w…该方法也就是通过freemarker生成固定的word文档动态的word模板布局不能用该方法。
也就是必须有一个固定的模板文档是.ftl类型
如果初始文件为 需要手动改为 也就是所有需要替换的地方都需要有${XX}替换。 主要步骤为
将 word 中需要填充的数据用占位符${变量名}替换。将该 word 另存为 .xml 的格式并检查看格式是否有误主要看占位符有没被分割开来。将后缀.xml改成.ftl后再调用相关 API 即可生成 word 文档。
特别注意一定是将word另存为xml格式而不是在外面修改后缀不然会乱码。
其次一定要在xml或者ftl格式下检查格式查询${XX}是否正确看是否有占位符被分隔开的情况如果有只需将中间多余的部分删除即可。 处理普通文本 处理文本比较简单在原文件中直接用占位符 ${} 替换即可。
处理表格 处在生成 word 表格时FreeMarker 是利用列表一行一行循环填充的而表头只会生成一次因此我们还需手动改动一下 .ftl 文件。
注意w:tbl 表示一个表格 、w: tr 表示一行、w: tc 表示一列我们先找到第一行填充数据的那行在前后分别加上如下语句即可
#list itemList as item
/#list另一种情况还有一种情况即需要进行单元格的合并操作前面和上面都差不多不过还要加上另一种标签
w:vmerge w:valrestart/
w:vmerge/我们先用 ${item.startMerge}开始合并和 ${item.endMerge}结束合并分别替换上面2行。 我们可以看到第一列分组是有合并单元格存在的因此找到第一列的 w: tc 那如下图所示 1.导入相关依赖
我们主要用到了 FreeMarker在这里只需要导入以下依赖即可 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-freemarker/artifactId
/dependency2.生成 word 的工具类
此方法将生成的文档传到前端 import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;public class WordUtils {/*** 生成 word 文档方法** param dataMap 要填充的数据* param templateName 模版名称* param fileName 要输出的文件路径* throws Exception 抛出的异常*/public static void generateWord(HttpServletResponse response,MapString, Object dataMap, String templateName, String fileName) throws Exception {// 设置FreeMarker的版本和编码格式Configuration configuration new Configuration(new Version(2.3.28));configuration.setDefaultEncoding(UTF-8);// 设置FreeMarker生成Word文档所需要的模板的路径// configuration.setDirectoryForTemplateLoading(new File(/Users/xxx/Desktop/));// 此处把模版文件都放在 resources 下的 templates 中configuration.setClassForTemplateLoading(WordUtils.class, /templates);// 设置FreeMarker生成Word文档所需要的模板Template tem configuration.getTemplate(templateName, UTF-8);// 创建一个Word文档的输出流
// Writer out new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), StandardCharsets.UTF_8));
// FreeMarker使用Word模板和数据生成Word文档
// response.setCharacterEncoding(UTF-8);response.setContentType(multipart/form-data);
// response.setHeader(content-type, application/octet-stream);
// response.setHeader(Content-Disposition, attachment;filename URLEncoder.encode(fileName, UTF-8));response.setCharacterEncoding(utf-8);response.setContentType(applicaiton/msword);
// response.setHeader(Content-disposition, attachment;filename fileName .docx);response.addHeader(Content-Disposition, attachment; filename URLEncoder.encode(fileName, UTF-8));PrintWriter writer response.getWriter();//tem.process(dataMap,new FileWriter(C:\\Users\\14017\\Desktop\\word.docx));tem.process(dataMap, writer);writer.close();//tem.process(dataMap,response.getWriter());
// writer.flush();
// writer.close();
// tem.process(dataMap,out);
// out.flush();
// out.close();}
}此方法是将生成的文档存储到固定位置--目前在桌面
package com.iwiti.qcc.manage;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;public class WordUtils {/*** 生成 word 文档方法** param dataMap 要填充的数据* param templateName 模版名称* param fileName 要输出的文件路径* throws Exception 抛出的异常*/public static void generateWord(HttpServletResponse response,MapString, Object dataMap, String templateName, String fileName) throws Exception {// 设置FreeMarker的版本和编码格式Configuration configuration new Configuration(new Version(2.3.28));configuration.setDefaultEncoding(UTF-8);// 设置FreeMarker生成Word文档所需要的模板的路径// configuration.setDirectoryForTemplateLoading(new File(/Users/xxx/Desktop/));// 此处把模版文件都放在 resources 下的 templates 中configuration.setClassForTemplateLoading(WordUtils.class, /templates);// 设置FreeMarker生成Word文档所需要的模板Template tem configuration.getTemplate(templateName, UTF-8);// 创建一个Word文档的输出流
// Writer out new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), StandardCharsets.UTF_8));
// FreeMarker使用Word模板和数据生成Word文档
// response.setCharacterEncoding(UTF-8);response.setContentType(multipart/form-data);
// response.setHeader(content-type, application/octet-stream);
// response.setHeader(Content-Disposition, attachment;filename URLEncoder.encode(fileName, UTF-8));// response.setCharacterEncoding(utf-8);
// response.setContentType(applicaiton/msword);response.setHeader(Content-disposition, attachment;filename fileName .docx);
// response.addHeader(Content-Disposition, attachment; filename URLEncoder.encode(fileName, UTF-8));
// PrintWriter writer response.getWriter();tem.process(dataMap,new FileWriter(C:\\Users\\14017\\Desktop\\word.docx));
// tem.process(dataMap, writer);
// writer.close();//tem.process(dataMap,response.getWriter());
// writer.flush();
// writer.close();
// tem.process(dataMap,out);
// out.flush();
// out.close();}
}3.准备填充所需的数据
一般来说我们的数据可能是从数据库中查询出来的各种对象这里为了方便就直接利用 HashMap 存储数据了。 public static MapString,Object initData(){// 对应单元格的合并final String startMerge w:vmerge w:valrestart/;final String endMerge w:vmerge/;final LocalDate nowDate LocalDate.now();MapString, Object data new HashMap();data.put(name, 张三);data.put(dept, 办公室);data.put(time, nowDate);ListItem itemList new ArrayList();Item item1 new Item();item1.setProject(分组A).setProjectDetail.setTime(2-13).setProjectDetail.setPeople(小米)..setProjectDetail.setAddress(北京).setStartMerge(startMerge);itemList.add(item1);Item item2 new Item();item2.setProject(分组A).setProjectDetail.setTime(2-13).setProjectDetail.setPeople(小米)..setProjectDetail.setAddress(北京).setEndMerge(endMerge);itemList.add(item2);ProjectItem item3 new Item();item3.setProject(分组B).setProjectDetail.setTime(2-13).setProjectDetail.setPeople(小米)..setProjectDetail.setAddress(北京)itemList.add(item3);data.put(itemList, itemList);return data;}
注意的是表格的map的value是实体类且这个实体类还有一个实体类嵌套其次在修改模板是将itemList as item, 所以占位符就是${item.project}与${item.projectDetail.time}
4.生成 word 文档 String templateName QCC_TEMPLATE_WORD;String fileName DateUtil.format(new Date(), yyyyMMddHH) QCC__WORD ;WordUtils.generateWord(response,data, templateName, fileName);//Qcc模板文件public static final String QCC_TEMPLATE_WORD QccTemplate.ftl;//Qcc生成文件名public static final String QCC__WORD QCC_REPORT.docx;