废话不说,上代码!
一、导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.5.2</version>
</dependency>
二、从配置文件中读取word模板路径以及文件生成的父目录
package com.shangyuninfo.core.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
@Configuration
public class WordCreateConfig {
private static String templateBaseDir;
private static String fileBaseDir;
public static String getTemplateBaseDir() {
return templateBaseDir;
}
@Value("${word.templateBaseDir}")
public void setTemplateBaseDir(String templateBaseDir) {
WordCreateConfig.templateBaseDir = templateBaseDir;
}
public static String getFileBaseDir() {
return fileBaseDir;
}
@Value("${word.fileBaseDir}")
public void setFileBaseDir(String fileBaseDir) {
WordCreateConfig.fileBaseDir = fileBaseDir;
}
}
三、根据xml模板生成doc文件
package com.shangyuninfo.core.util;
import cn.hutool.core.util.StrUtil;
import com.shangyuninfo.core.configuration.WordCreateConfig;
import com.shangyuninfo.core.exception.BusinessException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.util.Map;
public class WordUtil {
/**
* @Title: createWord
* @Description: TODO 将数据添加到xml中,并生成word文件
* @param: map 需要替换数据的map
* @param: templateName 模板名称
* @param filePath 想要生成的文件的路径
* @param fileName 想要生成的文件的名字
* @return: String 返回生成文件地址
* @throws
*/
public static String createWord(Map map, String templateName, String filePath, String fileName) throws IOException, TemplateException {
if (StrUtil.isBlank(WordCreateConfig.getTemplateBaseDir()) || StrUtil.isBlank(WordCreateConfig.getFileBaseDir())){
throw new BusinessException("缺少必要参数,请在配置文件中进行配置!");
}
if (StrUtil.isBlank(templateName)){
throw new BusinessException("模板名称不能为空!");
}
if (StrUtil.isBlank(filePath)){
throw new BusinessException("文件保存路径不能为空!");
}
if (StrUtil.isBlank(fileName)){
throw new BusinessException("文件名不能为空!");
}
if (!fileName.endsWith(".doc") && !fileName.endsWith(".docx") && !fileName.endsWith(".DOC") && !fileName.endsWith(".DOC")){
throw new BusinessException("文件扩展名错误!");
}
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
//word模板路径
configuration.setDirectoryForTemplateLoading(new File(WordCreateConfig.getTemplateBaseDir()));
Template template = configuration.getTemplate(templateName);
//文件全路径
String fileFullName ;
if (filePath.startsWith("/") || filePath.startsWith("\\")){
fileFullName = WordCreateConfig.getFileBaseDir() + filePath + File.separator + fileName;
}else {
fileFullName = WordCreateConfig.getFileBaseDir() + File.separator + filePath + File.separator + fileName;
}
File outFile = new File(fileFullName);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
Writer out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(outFile), "UTF-8"));
template.process(map, out);
out.flush();
out.close();
return fileFullName;
}
}
四、顺便补上模板取值
1.基本数据类型或其包装类型取值
${var}
2.对象取值
${var.property}
3.list循环
<#list dataList as data>
${data}
</#list>
4.map取值
<#assign map=data/>
<#assign keys=map?keys/><!-- map的key的集合,可以配合list标签循环取值,然后根据key取value -->
<!-- 取第一个key的值 -->
${map[keys[0]]!''}
5.标记为纯文本,var是啥就是啥
<![CDATA[${var}]]>