Poi-tl word模版导出

pom文件

<dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.10.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>xml-apis</artifactId>
                    <groupId>xml-apis</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml-schemas</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

使用时需要注意排除jar冲突,否则会报错,目前使用的都是4.1.2版本的

模板放在resource下的template目录下

模版内容,填充的参数放在 {{}} 中,参考链接 http://deepoove.com/poi-tl/

 

 部分代码实现,返回流提供前端下载

@Override
    public void wordExport(HttpServletResponse response, SewageSafeContinueImprove sewageSafeContinueImprove) throws IOException {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition","attachment;filename=\""+"out_template.docx"+"\"");
        String safeContinueImproveId = sewageSafeContinueImprove.getSafeContinueImproveId();
        SewageSafeContinueImprove continueImprove = this.getById(safeContinueImproveId);
        Map<String, Object> beanToMap = BeanUtil.beanToMap(continueImprove);
        beanToMap.put("date", DateUtil.format(new Date(),"yyyy年MM月dd日"));
        ClassPathResource res = new ClassPathResource("template/safe.docx");
        InputStream is = res.getInputStream();
        XWPFTemplate template = XWPFTemplate.compile(is).render(beanToMap);
        // template.writeToFile("D:\\桌面\\sssssssssssss.docx");

        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        //关闭流
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);
    }

  


posted @ 2024-07-30 16:44  Sea_wxx  阅读(22)  评论(0编辑  收藏  举报