poi-tl区块对循环生成:
提示:这里简述项目相关背景:
需求:从阿里云上获取到word文档,然后根据模板进行文档生成,生成完后上传回阿里云
根据word文档模板生成文件
poi-tl maven依赖——》(官方文档)
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.3</version>
</dependency>
word文档模板:
实现代码:
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
//创建插件实例
ListRenderPolicy policy = new ListRenderPolicy();
//插件配置
Configure config = Configure.builder().bind("pictures", policy).build();
//存入区块对数据
List<Map<String, Object>> list = new ArrayList<>();
//自己的数据
List<JSONObject> jsonObjects=new ArrayList<>();
for (int i = 0; i < jsonObjects.size(); i++) {
//每一个块的数据
Map<String, Object> datas = new HashMap<>();
//加入文本数据
datas.put("title", new TextRenderData((i + 1) + "." + jsonObjects.get(i).getString("title")));
//加入图片数据
if (jsonObjects.get(i).getString("picture") != null) {
datas.put("picture", Pictures.ofUrl(jsonObjects.get(i).getString("picture"), PictureType.PNG).size(550, 450).create());
}
list.add(datas);
}
map.put("Item", list);
XWPFTemplate template = XWPFTemplate.compile("aliyun_url", config).render(map);
//获取poi类
XWPFDocument xwpfDocument = template.getXWPFDocument();
//转为inputStream
InputStream inputStream = PoitlIOUtils.docToInputStream(xwpfDocument);
//上传至阿里云
String upload = OssBootUtil.upload(inputStream , "upload_url");
}
参考案例 https://blog.csdn.net/robin90814/article/details/107413380