另存为xml格式:
将保存的xml改成.ftl
格式化一下xml,看看文件中的带有${}是否正确
如果出现这种情况,手动修改下(可复制上一个正确的改下名字)
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
public void createDoc(Map<String,Object> dataMap,String templateName,String filePath) {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板是放在当前类包下面的template文件夹下
configuration.setClassForTemplateLoading(this.getClass(),"template");
// 输出文档路径及名称
File outFile = new File(filePath);
Writer out = null;
try {
// test.ftl为要装载的模板
Template t = configuration.getTemplate(templateName);
//设置模板编码方式
t.setEncoding("utf-8");
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
t.process(dataMap, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
OaMeetingMinutesServiceImpl service = new OaMeetingMinutesServiceImpl();
// 要填入模本的数据文件
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("week", "周一");
dataMap.put("name", "张三");
dataMap.put("date", "6.11");
dataMap.put("content", "哈师大");
service.createDoc(dataMap,"test.ftl","D:/test.doc");
System.out.println("end");
}
测试:
public static void main(String[] args) {
OaMeetingMinutesServiceImpl service = new OaMeetingMinutesServiceImpl();
// 要填入模本的数据文件
Map<String,Object> dataMap = new HashMap<>();
List<User> list = new ArrayList<>();
User user = new User();
user.setWeek("周一");
user.setName("张三");
user.setDate("6.22");
user.setContent("畜栏里");
list.add(user);
User user1 = new User();
user1.setWeek("周二");
user1.setName("李四");
user1.setDate("6.23");
user1.setContent("畜栏d地方里");
list.add(user1);
dataMap.put("userList", list);
service.createDoc(dataMap,"test.ftl","D:/test.doc");
System.out.println("end");
}
在要合并的一列上加入下面截图中的标签
测试:我这里合并的是星期一列
<w:tcPr>
<w:tcW w:w="2130" w:type="dxa"/>
<#if user.strMap.now == "1">
<#if user.strMap.pre == "0">
<w:vMerge w:val="restart"/>
<#else>
<w:vMerge/>
#if>
<#else>
<#if user.strMap.pre != "0">
<w:vMerge/>
#if>
#if>
w:tcPr>
Java代码:
public static void main(String[] args) {
OaMeetingMinutesServiceImpl service = new OaMeetingMinutesServiceImpl();
// 要填入模本的数据文件
Map<String,Object> dataMap = new HashMap<>();
List<User> list = new ArrayList<>();
User user = new User();
user.setWeek("周一");
user.setName("张三");
user.setDate("6.22");
user.setContent("畜栏里");
list.add(user);
User user1 = new User();
user1.setWeek("周一");
user1.setName("李四");
user1.setDate("6.23");
user1.setContent("畜栏d地方里");
list.add(user1);
User user2 = new User();
user2.setWeek("周一");
user2.setName("李四1");
user2.setDate("6.211");
user2.setContent("畜栏d地方1里");
list.add(user2);
User user3 = new User();
user3.setWeek("周二");
user3.setName("李四11");
user3.setDate("6.2111");
user3.setContent("畜栏d地方11里");
list.add(user3);
User user4 = new User();
user4.setWeek("周二");
user4.setName("李四111");
user4.setDate("6.21111");
user4.setContent("畜栏d地方111里");
list.add(user4);
for(int i =0;i<list.size();i++){
list.get(i).getStrMap().put("pre","0");
list.get(i).getStrMap().put("now","0");
list.get(i).getStrMap().put("next","0");
if(i<list.size()-1){
//当前和下一个相同就合并,将now设置为1
if(list.get(i).getWeek().equals(list.get(i+1).getWeek())){
list.get(i).getStrMap().put("now","1");
}
}
}
for(int i=0;i<list.size();i++){
//保存他的前一个元素是否需要合并
if(i>0){
list.get(i).getStrMap().put("pre",list.get(i-1).getStrMap().get("now"));
}
//保存他的后一个元素是否需要合并
if(i<list.size()-1){
list.get(i).getStrMap().put("next",list.get(i+1).getStrMap().get("now"));
}
}
dataMap.put("userList", list);
service.createDoc(dataMap,"test.ftl","D:/test.doc");
System.out.println("end");
}
说明:我这里采用了两次循环的方法合并,仅供参考