生成word模板的时候,可能会在一个map中包含多个list,list中又嵌套map。参考了一些做法,记录下来以便以后使用。
一、 生成模板的工具类
public class DocumentHandler {
private Configuration configuration = null;
//推荐初始化加载模板使用单例模式
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
/**
*
* @param dir 模板加载路径
* @param tempName 模板名称
* @param savePath 文件保存的路径、文件名
* @param sDate 数据集
*/
public void createDoc(String dir, String tempName, String savePath, Map<String, Object> sDate) {
Template template = null;
// 文件保存位置
File outFile = new File(savePath);
Writer out = null;
try {
// 设置模板加载路径
configuration.setDirectoryForTemplateLoading(new File(dir));
// 异常控制
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
// 加载模板
template = configuration.getTemplate(tempName);
//生成文件的路径及文件名
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
out =bufferedWriter;
// 合并模板和数据
template.process(sDate, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、 生成数据模板
两种list、map的嵌套方式
- map中包含一个list
public class Test1 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map = getData();
DocumentHandler documentHandler = new DocumentHandler();
// String dir = "C:/Users/2018/Desktop/testFolder/";
String fileName = "新的测试.ftl";
String savePath = dir+"aaaaa.doc";
documentHandler.createDoc(dir, fileName, savePath, map);
}
public static Map<String, Object> getData(){
Map<String, Object> allMap = new HashMap<>();
//1.学校名称
allMap.put("school", "̫太阳花小学");
//2.班级信息
List<Map<String,Object>> classList = new ArrayList<>();
//一班
Map<String, Object> class_1 = new HashMap<>();
class_1.put("className", "一班");
//学生
List<Map<String,Object>> studentList_1 = new ArrayList<>();
for (int i=0; i<3; i++) {
Map<String, Object> student = new HashMap<>();
student.put("number", "000"+i);
student.put("name", "学生"+i+"号");
studentList_1.add(student);
}
class_1.put("list", studentList_1);
classList.add(class_1);
//二班
Map<String, Object> class_2 = new HashMap<>();
class_2.put("className", "二班");
//学生
List<Map<String,Object>> studentList_2 = new ArrayList<>();
for (int i=0; i<3; i++) {
Map<String, Object> student = new HashMap<>();
student.put("number", "000"+i);
student.put("name", "学生"+i+"号");
studentList_2.add(student);
}
class_2.put("list", studentList_2);
classList.add(class_2);
allMap.put("allList", classList);
return allMap;
}
}
模板1:
学校:${school}
<#list allList as middleMap>
<#list middleMap?keys as itemKey>
<#if itemKey="className">
班级:${middleMap[itemKey]}
</#if>
<#if itemKey="list">
<#list middleMap[itemKey] as middleMap>
${middleMap.name}
</#list>
</#if>
</#list>
</#list>
效果
2.map中包含多个list
public class Test2 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map = getData();
DocumentHandler documentHandler = new DocumentHandler();
// String dir = "C:/Users/2018/Desktop/testFolder/";
String dir = "E:/workspaces/yctp/test/src/com/freemarkerDemo/test2/";
String fileName = "新的测试.ftl";
String savePath = dir+"aaaaa.doc";
documentHandler.createDoc(dir, fileName, savePath, map);
}
//组装数据模板
public static Map<String, Object> getData(){
Map<String, Object> allMap = new HashMap<>();
//1.学校
allMap.put("school", "太阳花小学");
//2.班级信息集合
List<Map<String,Object>> classList = new ArrayList<>();
//一班信息
Map<String, Object> class_1 = new HashMap<>();
class_1.put("className", "No.1 Class");
//一班学生
List<Map<String,Object>> nan_student1 = new ArrayList<>();
for (int i=0; i<3; i++) {
Map<String, Object> student = new HashMap<>();
student.put("number", "男000"+i);
student.put("name", "男学生"+i+"号");
nan_student1.add(student);
}
class_1.put("nanList", nan_student1);
List<Map<String,Object>> nv_student1 = new ArrayList<>();
for (int i=0; i<3; i++) {
Map<String, Object> student = new HashMap<>();
student.put("number", "女000"+i);
student.put("name", "女学生"+i+"号");
nv_student1.add(student);
}
class_1.put("nvList", nv_student1);
classList.add(class_1);
allMap.put("allList", classList);
String json = JSON.toJSONString(allMap);
System.out.println(json);
return allMap;
}
}
模板2:
${school}
<#list allList as middleMap>
<#list middleMap?keys as itemKey>
<#if itemKey="className">
班级:${middleMap[itemKey]}
</#if>
</#list>
<#list middleMap?keys as itemKey>
<#if itemKey="nanList">
<#list middleMap[itemKey] as middleMap>
编号:${middleMap.number} 姓名:${middleMap.name}
</#list>
</#if>
<#if itemKey="nvList">
<#list middleMap[itemKey] as middleMap>
编号:${middleMap.number} 姓名:${middleMap.name}
<!-- 例子的位置 -->
</#list>
</#if>
</#list>
</#list>
效果
如果在list中还包含map,可以根据key获取这个list,然后获取获取map中key的就可以了。这段代码可以放在 模板2 中的 例子的位置 进行测试。
例子:
<#list middleMap["list"] as unitMap>
输出:${key}
</#listp>
参考:
https://blog.youkuaiyun.com/qq_34538575/article/details/79256195