Springboot 集成freemark生成word
最近遇到一个业务 需要按照指定的模板填充数据 用到了freemark简单记录一下
制作模板
1首先把需要填充的模板,按照需要填充的数据,进行替换。
2把模板另存为xml文件,然后放入idea,进行数据的格式化,方便替换。大多时间圈起来的部分是不在一起的,进行操作放在一起即可。然后修改文件后缀名为.ftl
引入freemark
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
wordUtils 工具类
public class WordUtil {
private static final Logger logger = LoggerFactory.getLogger(WordUtil.class);
private Template template;
private final String path = "替换成自己的路径";
/**
* 初始化
*/
public WordUtil(String templateName) throws Exception {
System.out.println("path = " + path);
Configuration configuration = new Configuration(Configuration.VERSION_2_3_20);
configuration.setDirectoryForTemplateLoading(new File(path));
configuration.setOutputEncoding("utf-8");
template = configuration.getTemplate(templateName);
}
/**
* 导出到浏览器
*/
public void doExports(String fileName, Map<String, Object> results,
HttpServletResponse response) throws TemplateException, IOException {
StringWriter out = new StringWriter();
Writer writer = new BufferedWriter(out, 10240);
template.process(results, writer);
InputStream fin = new ByteArrayResource(out.toString().getBytes(StandardCharsets.UTF_8)).getInputStream();
ServletOutputStream outputStream;
response.setCharacterEncoding("utf-8");
response.setContentType("Content-Type;application/json");
response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
outputStream = response.getOutputStream();
// 缓冲区
byte[] buffer = new byte[1024];
int bytesToRead;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesToRead);
}
if (outputStream != null) {
outputStream.close();
}
fin.close();
}
wordService 主要进行数据填充
@Override
public Map<String, Object> assembleData(OracleInfMer oracleInfMer, OracleTermBase oracleTermBase) {
Map<String, Object> dataMap = new HashMap<String, Object>();
//组装模板需要的数据
//1年月日
Calendar calendar = Calendar.getInstance();
dataMap.put("year", calendar.get(Calendar.YEAR));
dataMap.put("month", calendar.get(Calendar.MONTH) + 1);
dataMap.put("day", calendar.get(Calendar.DAY_OF_MONTH));
//商户号
dataMap.put("cusid", oracleTermBase.getCusid());
//终端号
dataMap.put("termno", oracleTermBase.getTermno());
//机身号
dataMap.put("sequence", oracleTermBase.getSequence());
//工商名
dataMap.put("cusname", oracleInfMer.getCUSNAME());
//营业名称
dataMap.put("branchname", oracleTermBase.getBranchname());
//签购单
dataMap.put("branchname", oracleTermBase.getBranchname());
//终端类型
dataMap.put("termtype", oracleTermBase.getTermtype());
//押金
if (StringUtils.isEmpty(oracleTermBase.getGuaranty())) {
dataMap.put("guaranty", 0);
} else {
dataMap.put("guaranty", oracleTermBase.getGuaranty());
}
//安装地址
dataMap.put("branchaddr", oracleTermBase.getBranchaddr());
//联系人以及电话
dataMap.put("contacter", oracleTermBase.getContacter());
dataMap.put("contacttel", oracleTermBase.getContacttel());
//拓展人
dataMap.put("devlman", oracleInfMer.getDEVLMAN());
return dataMap;
}
controller控制器层
Map<String, Object> dataMap = oracleInfMerService.assembleData(oracleInfMer, oracleTermBase);
String fileName = "终端号" + oracleTermBase.getTermno() + ".doc";
WordUtil wordUtil = new WordUtil("word.ftl");
wordUtil.doExports(fileName, dataMap, response);
集成freemark相对简单,就是简单的数据填充,很好理解,制作模板的时候会稍微有点麻烦,会出错,细心一点就好。