1.引入freemarker
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency>
2.构建word模板,先创建word文件,注:本人使用的2003版本的word文档,其他版本未进行测试
3.创建完后word文件后,取名:temp.doc文件,并打开temp文件,另存为temp.xml格式文件;保存类型选择如下:
4.打开temp.xml文件,使用Notepad++文本编辑器,需要安装一个组件,选择插件=》插件管理,进入插件列表,并在可用里面搜索:Xml tool,并进行安装,安装后如下:
然后打开xml文件,进行文件格式化处理
打开文件后,并在文件找到其想替换的字符,如想替换jim,需要在xml中找到jim字符,
使用freemarker标签替换为${author}
表格替换
替换完成后,把temp.xml文件的后缀改成ftl格式,temp.ftl
5.构建ftl转换冲word的工具类
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author binshangwen
* @date 2020/2/18 14:27
*/
public class FtlToWordUtil11 {
private static Logger logger = LoggerFactory.getLogger(FtlToWordUtil11.class);
/**
* 初始化配置文件
*
* @param folderPath 生成word的文件夹路径
* @return Configuration
*/
private static Configuration initConfiguration(String folderPath) {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
configuration.setDefaultEncoding("utf-8");
try {
configuration.setDirectoryForTemplateLoading(new File(folderPath));
} catch (IOException e) {
logger.error("init configuration filed ,path not exist:{}", folderPath);
// e.printStackTrace();
}
return configuration;
}
/**
* 将数据写入word,并输出到指定路径
*
* @param dataMap 需要写入word的数据
* @param ftlFolderPath 指定的word模板所在的文件夹
* @param tempName 指定的word模板名称
* @param outputFilePath 输出路径
*/
public static boolean createWord(Map<String, Object> dataMap, String ftlFolderPath, String tempName, String outputFilePath) {
logger.info("create word file");
boolean result = false;
Configuration configuration = initConfiguration(ftlFolderPath);
Template t = null;
try {
t = configuration.getTemplate(tempName);
// t.setEncoding("utf-8");
} catch (IOException e) {
logger.error("file path :{} not found:{}", ftlFolderPath, tempName);
// e.printStackTrace();
}
File outFile = new File(outputFilePath);
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
//OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
t.process(dataMap, out);
out.close();
fos.close();
result = true;
logger.info("word create success");
} catch (FileNotFoundException e) {
logger.error("文件不存在:{}", outFile);
// e.printStackTrace();
} catch (UnsupportedEncodingException e) {
logger.error("未知的编码格式",e);
// e.printStackTrace();
} catch (TemplateException e) {
logger.error("模板异常",e);
//e.printStackTrace();
} catch (IOException e) {
logger.error("IO异常",e);
// e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
Map<String, Object> dataMap = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>(16);
for (int i = 0; i < 10; i++) {
Map<String, Object> itemMap = new HashMap<>(16);
itemMap.put("name", "name"+(i+1));
itemMap.put("age", "age"+ (i+1));
itemMap.put("sex", "sex"+ (i+1));
itemMap.put("phone", "phone" + (i+1));
list.add(itemMap);
}
dataMap.put("userList", list);
dataMap.put("author", "alex");
dataMap.put("date", "2020-01-05");
createWord(dataMap, "D:\\word_temp\\", "temp.ftl", "D:\\word_temp\\test.doc");
System.out.println("模板生成成功");
}
}
6.生成后的word文件,打开后展示结果如下
来源:oschina
链接:https://my.oschina.net/u/2251646/blog/3190379