基于poi-tl生成word

一、需求描述

使用poi-tl技术,通过既有的word模板,使用poi-tl语法,生成定制后的word

二、maven

<!--引入poi-tl开始-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

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

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>
        <!--引入poi-tl结束-->

三、word模板

 任意位置放入以下内容

模板位置

配置文件参考,此处扔到了resources下

 导出路径自己随便写吧,愿意仍哪仍哪

四、代码

自己看代码吧,极其简单

 public static void generateWord(List<RecordModel> recordList, String exportPath, String templatePath) throws Exception {
        //设置表格头
        RowRenderData header = RowRenderData.build(
                new TextRenderData("FFF7FB", "文件名称"),
                new TextRenderData("FFF7FB", "文件后缀"),
                new TextRenderData("FFF7FB", "文件路径"),
                new TextRenderData("FFF7FB", "异常类别"),
                new TextRenderData("FFF7FB", "异常信息")
        );
        //设置表格背景色
        TableStyle tableStyle = new TableStyle();
        tableStyle.setBackgroundColor("0072E3");
        header.setStyle(tableStyle);
        //准备录入的数据
        List<RowRenderData> rowList = new ArrayList<>();
        for (RecordModel record : recordList) {
            RowRenderData row = RowRenderData.build(record.getFileName(), record.getFileSuffix(), record.getFilePath(), record.getErrorType(), record.getErrorMsg());
            rowList.add(row);
        }
        //渲染表格
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(
                new HashMap<String, Object>() {{
                    put("title", "GIM解析表格如下");
                    put("name", "我是一个name");
                    put("table", new MiniTableRenderData(header, rowList));
                }});
        //生成word
        template.write(new FileOutputStream(exportPath+ System.currentTimeMillis() + ".docx"));
        //关闭模板流
        template.close();
    }

 五,执行代码

看个热闹就行了,就这么个意思

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值