使用poi-tl模板导出word

本文介绍如何利用Java库POI-TL 1.8.2版本进行Word文档的自动化生成,包括如何在Word中插入变量并从Java代码中读取数据填充这些变量,同时展示了如何将本地及网络图片添加到Word文档中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom:

        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.8.2</version>
        </dependency>

自我介绍.doc

{{?item}}
{{name}} {{age}} {{@weChatPicture}} {{@picture}}
{{/item}}

main:

    public static void main(String[] args) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tony");
        map.put("age", "18");
        // 读取本地磁盘图片
        map.put("weChatPicture", new PictureRenderData(100, 100, "e:\\1.jpg"));
        // 通过url读取网络图片
        map.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));

        List<Map<String, Object>> list1 = new ArrayList<>();
        list1.add(map);
        list1.add(map);
        Map<String, List<Map<String, Object>>> datasMap = new HashMap<>();
        datasMap.put("item",list1);
        File file = new File("D:\\自我介绍.docx");
        XWPFTemplate template = XWPFTemplate.compile(file).render(datasMap);

        FileOutputStream out = new FileOutputStream(new File("D:\\输出自我介绍.docx"));
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javachen__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值