poi-tl区块对循环生成

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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值