1.先上模板图
2.效果图
链接:https://pan.baidu.com/s/1n204S3M-2z3slFITk6pCbw
提取码:z3s5
{"jhj1":[{"name1":"H3C-S5024PV2-E1","name2":"政务内网核心交换机主1","name3":"1","name4":"备注1"},{"name1":"H3C-S5024PV2-E2","name2":"政务内网核心交换机主2","name3":"0","name4":"备注2"},{"name1":"H3C-S5024PV2-E3","name2":"政务内网核心交换机主3","name3":"0","name4":"备注3"}],"jhj2":[{"name1":"硬件/模块运行状况","name2":"1","name3":"硬件备注1","radio":1},{"name1":"运行环境-供电状况","name2":"0","name3":"环境备注2","radio":1},{"name1":"运行环境-机房温度","name2":"36℃","name3":"机房备注3","radio":2},{"name1":"运行环境-机房湿度","name2":"%","name3":"湿度备注4","radio":2},{"name1":"运行环境-通风状况","name2":"良好","name3":"通风备注5","radio":2}],"jhjform":{"azaddress":"武汉市蔡甸区大数据中心","jctime":"2020-06-09","remarks":"建议经常查看情况"}}
4.测试代码+工具代码
public static void main(String[] args) {
String jhj="{\"jhj1\":[{\"name1\":\"H3C-S5024PV2-E1\",\"name2\":\"政务内网核心交换机主1\",\"name3\":\"1\",\"name4\":\"备注1\"},{\"name1\":\"H3C-S5024PV2-E2\",\"name2\":\"政务内网核心交换机主2\",\"name3\":\"0\",\"name4\":\"备注2\"},{\"name1\":\"H3C-S5024PV2-E3\",\"name2\":\"政务内网核心交换机主3\",\"name3\":\"1\",\"name4\":\"备注3\"}],\"jhj2\":[{\"name1\":\"硬件/模块运行状况\",\"name2\":\"1\",\"name3\":\"硬件备注1\",\"radio\":1},{\"name1\":\"运行环境-供电状况\",\"name2\":\"0\",\"name3\":\"环境备注2\",\"radio\":1},{\"name1\":\"运行环境-机房温度\",\"name2\":\"36℃\",\"name3\":\"机房备注3\",\"radio\":2},{\"name1\":\"运行环境-机房湿度\",\"name2\":\"66%\",\"name3\":\"湿度备注4\",\"radio\":2},{\"name1\":\"运行环境-通风状况\",\"name2\":\"良好\",\"name3\":\"通风备注5\",\"radio\":2}],\"jhjform\":{\"azaddress\":\"政务中心\",\"jctime\":\"2020-06-09\",\"remarks\":\"建议经常查看情况\"}}";
Map map = (Map) JSON.parse(jhj); //json转map
Object jhj1 = map.get("jhj1"); //获取jhj1数据
Object jhj2 = map.get("jhj2"); //获取jhjq2数据
Object jhjform = map.get("jhjform"); //获取jhjform数据
List
public class ToWord {
public static void toWord(Map dataMap, String docName, String ftlName) throws IOException {
try {
//Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
/*以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是
* 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
//指定路径的第一种方式(根据某个类的相对路径指定)
//configuration.setClassForTemplateLoading(this.getClass(),"");
configuration.setClassForTemplateLoading(ToWord.class, "/com/havenliu/document/template");
//指定路径的第二种方式,我的路径是C:/a.ftl
//configuration.setDirectoryForTemplateLoading(new File("C:\\Users\\张胜强\\Desktop"));
// 输出文档路径及名称
File outFile = new File("C:\\Users\\张胜强\\Desktop\\"+docName);
//以utf-8的编码读取ftl文件
Template t = configuration.getTemplate(ftlName, "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
可以用IDEA打开 然后Ctrl+Alt+L 整理格式宜读
1.单值赋值 直接搜索之前赋值(如111),找到对应的位置,然后编辑 指令为 ${}
111对应word的是安装位置,现在赋值.json中单独的字段azaddress表示安装位置,这修改为如下,就可以了.
2.List遍历赋值.搜索3333找到遍历值位置,行是以上一行的"备注"结尾,然后观察,找到一行的所有数据
3.确定位置,然后开始遍历然后赋值, 闭合标签记得放到后面去
4.list的子属性.是直接点出来的
5.写if 判断,图一简单if ,图2是把字体样式都包含进去了
6.都赋好值后直接执行main方法就可以生产了
7.freemaker语法指令 : https://www.cnblogs.com/Jimc/p/9791507.html
8.如果没有集合需要遍历,都是单数据填充,可以推荐easypoi处理,更为简单,参考 https://www.jianshu.com/p/0a40b90cc72c