项目需要,从接口平台里面把数据导出为word文档。做个记录
以前使用过officegen,那个比较繁琐,基于node形式的。这次改成java还是挺方便的
主要使用 poi-tl,文档可参考 http://deepoove.com/poi-tl/#hack-loop-table
解决两个问题
- 字段轮询 通过{{#}}
- 表格轮询 通过{{?}}{{/}}
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.80</version>
</dependency>
template.docx 如图
package com.zxy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.TableRenderData;
import com.deepoove.poi.data.Tables;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
String s = readFile("info.json");
JSONObject jsonObject = JSON.parseObject(s);
JSONArray sons = jsonObject.getJSONArray("data");
List<Map<String, Object>> list = new ArrayList<>();
Map<String, List<Map<String, Object>>> datasMap = new HashMap<>();
int i=0;
for (Object item : sons) {
JSONObject obj = (JSONObject) item;
JSONArray requestJson = obj.getJSONArray("requestJson");
JSONArray responseJson = obj.getJSONArray("responseJson");
TableRenderData requestList = Tables.create();
TableRenderData responseList = Tables.create();
RowRenderData header = Rows.create(
"字段",
"类型",
"描述",
"必填"
);
requestList.addRow(header);
responseList.addRow(header);
//处理请求报文
setBody(requestJson, requestList, "");
String successCase = obj.getString("successCase");
//处理响应报文
setBody(responseJson, responseList, "");
int finalI = ++i;
list.add(new HashMap<String, Object>() {{
put("index", finalI);
put("name", obj.getString("name"));
put("url", obj.getString("url"));
put("requestType", obj.getString("requestType"));
put("requestJson", requestList);
put("responseJson", responseList);
put("successCase", successCase);
}});
}
datasMap.put("item", list);
XWPFTemplate.compile("template.docx").render(datasMap).writeToFile("out_template.docx");
}
public static void setBody(JSONArray jsonArray, TableRenderData renderList, String prefix) {
for (Object item : jsonArray) {
JSONObject itemObj = (JSONObject) item;
RowRenderData row0 = Rows.create(
prefix + itemObj.getString("argname"),
itemObj.getString("argtype"),
itemObj.getString("argdesc"),
itemObj.getString("argrequire")
);
renderList.addRow(row0);
if (itemObj.get("children") != null) {
setBody((JSONArray) itemObj.get("children"), renderList, prefix + " ");
}
}
}
public static String readFile(String path) {
StringBuffer sb = new StringBuffer();
try {
Reader reader = new InputStreamReader(new FileInputStream(path));
int ch = 0;
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
info.json如下
{"code":"0","msg":"success","data":[{"id":8474,"libraryId":126,"groupIds":",0,1808,","groupId":null,"agreement":"HTTP","requestBodyType":"2","requestType":"GET","url":"/mom/platform/designer/template/config/load","name":"读取配置","status":1,"header":"[]","requestJson":"[{\"argname\":\"dataId\",\"argtype\":\"number\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"0\"}]","responseJson":"[{\"argname\":\"code\",\"argtype\":\"string\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"0\"},{\"argname\":\"msg\",\"argtype\":\"string\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"1\"},{\"argname\":\"data\",\"argtype\":\"object\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"2\",\"children\":[{\"argname\":\"dataId\",\"argtype\":\"number\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"2-0\"},{\"argname\":\"dpi\",\"argtype\":\"number\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"2-1\"},{\"argname\":\"height\",\"argtype\":\"number\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"2-2\"},{\"argname\":\"width\",\"argtype\":\"number\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"2-3\"},{\"argname\":\"config\",\"argtype\":\"object\",\"argrequire\":false,\"argdesc\":\"\",\"key\":\"2-4\"}]}]","successCase":"{\n \"code\": \"00000\",\n \"msg\": \"ok\",\n \"data\": null\n}","failCase":"","createTime":"2022-04-13 16:53:41","updateTime":"2022-04-13 18:22:36","createUserId":305,"createUserName":"","updateUserId":305,"updateUserName":"","deleted":0}]}
效果图: