- 制作word模板
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e888a93e2e4e5a8bf3b61d2fe34ff124.png)
- 代码
public class test1 {
private static String inputUrl = "C:\\Users\\Administrator\\Desktop\\xdoc\\test.docx";
private static String outputUrl = "C:\\Users\\Administrator\\Desktop\\xdoc\\temp.docx";
public static void main(String[] args) throws Exception {
WordprocessingMLPackage worldPackage;
try {
worldPackage = WordprocessingMLPackage.load(new File(inputUrl));
MainDocumentPart documentPart = worldPackage.getMainDocumentPart();
Map<String,String> maps = new HashMap<>();
maps.put("name", "22222");
maps.put("sex", "22222");
maps.put("age", "22");
documentPart.variableReplace(maps);
ClassFinder find = new ClassFinder(Tbl.class);
new TraversalUtil(worldPackage.getMainDocumentPart().getContent(), find);
Tbl table = (Tbl) find.results.get(0);
Tr dynamicTr = (Tr)table.getContent().get(1);
Tr dynamicTr2 = (Tr)table.getContent().get(2);
String dynamicTrxml = XmlUtils.marshaltoString(dynamicTr);
String dynamicTrxml2 = XmlUtils.marshaltoString(dynamicTr2);
List<Map<String,Object>> data = getDataList();
for(Map<String, Object> map :data){
Tr newTr = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrxml, map);
table.getContent().add(newTr);
List<Map<String, Object>> list = (List<Map<String,Object>>) map.get("list");
for (Map<String, Object> map1 :list){
Tr newTr1 = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrxml2, map1);
table.getContent().add(newTr1);
}
}
table.getContent().remove(1);
table.getContent().remove(1);
Docx4J.save(worldPackage, new File(outputUrl));
}catch (Docx4JException e){
e.printStackTrace();
}
}
private static List<Map<String , Object>> getDataList() {
List<Map<String , Object>> dataList = new ArrayList<Map<String , Object>>();
Map<String , Object> m1 = new HashMap<String , Object>();
m1.put("number", "1");
m1.put("name", "关银萍");
m1.put("address", "女");
m1.put("skill", "来吧,青龙偃月刀");
dataList.add(m1);
Map<String , Object> m2 = new HashMap<String , Object>();
m2.put("number", "2");
m2.put("name", "马云禄");
m2.put("address", "女");
m2.put("skill", "啥玩意,手里方片摸牌,占位占位看到换行的样式效果,占位占位看到换行的样式效果");
dataList.add(m2);
Map<String , Object> m3 = new HashMap<String , Object>();
m3.put("number", "3");
m3.put("name", "张星彩");
m3.put("address", "女");
m3.put("skill", "长缨在手,擦擦擦擦");
dataList.add(m3);
Map<String, Object> map;
map = new HashMap<>();
map.put("num", "1");
map.put("test", "2222222222");
map.put("list", dataList);
List<Map<String , Object>> data = new ArrayList<Map<String , Object>>();
data.add(map);
data.add(map);
return data;
}
}
- 单行表格循环时注释以下代码
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d343e85edd50c4a4a5873babbb84c6c7.png)
- 结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e4c0f7bc80f7a51f91a9f4b70b1ae231.png)
注意:
- 将docx 转换为xml 检查 $(var)是否被分割
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/332b81e1790eb0ff9030068aa1616ee3.png)
正常情况
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f66de7b9e25ef5a9902d1c377120ab74.png)
- 多行模板替换 删除模板行时应注意 都是删除第一行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/670c03da3ccee0670884911095f44705.png)