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/

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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 @   Sea_wxx  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2020-07-30 @RequestBody Content type 'multipart/form-data;boundary=----WebKitFormBoundarybEyHr0FZTTOHW7Vq;charset=UTF-8' not supported
点击右上角即可分享
微信分享提示