首页 > 其他分享 >根据word模板动态导出word文档

根据word模板动态导出word文档

时间:2024-01-25 14:24:46浏览次数:39  
标签:java 文档 io import word dataMap 模板

根据word模板动态导出word文档

前置条件:新建一个springboot项目

1.引jar包

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
        <groupId>com.deepoove</groupId>
         <artifactId>poi-tl</artifactId>
         <version>1.12.1</version>
 </dependency>

2.创建word模板

在resources文件夹下新建一个文件夹template(随意)新建一个test.docx内容如下:

你好:{{test1}}
我叫{{test2}}很高兴认识你
{{test3}}未来一定很好!

3.书写一个接口

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.util.PoitlIOUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


/**
 * @author wangfan
 */
@Api(tags = "测试接口")
@RestController
public class TestController {
    
    @ApiOperation("导出word")
    @GetMapping("/download")
    public void download(HttpServletResponse response){
        //为模板{{}}变量添加数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("test1", "java");
        dataMap.put("test2", "wangfan");
        dataMap.put("test3", "相信");
        //读取模板文件
        ClassPathResource templateFile = new ClassPathResource("/template/test.docx");
        try (OutputStream out = response.getOutputStream();
             BufferedOutputStream bos = new BufferedOutputStream(out)) {
            String fileName = "test.docx";
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition","attachment;filename="
                    + URLEncoder.encode(fileName, "UTF-8"));
            String filePath = templateFile.getFile().getPath();
            //核心
            XWPFTemplate template = XWPFTemplate.compile(filePath).render(dataMap);
            template.write(bos);
            bos.flush();
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        } catch (IOException e) {
            throw new RuntimeException("文件生成失败");
        }
    }
}

完成!!!测试

更多请查看poi-tl官网:Poi-tl Documentation (deepoove.com)

标签:java,文档,io,import,word,dataMap,模板
From: https://www.cnblogs.com/WangJingjun/p/17987037

相关文章

  • 【模板】并查集
    并查集是解决两元素是否属于同一集合,将一个集合合并另一集合的数据结构。具体来说,我们使用数字代替集合,比如集合1,集合2.使用数组f[i]维护元素i属于的集合,比如f[2]=4表示元素2属于集合4。具体我们有以下实现功能的代码1初始化表示集合的数组。cin>>n>>m;for(int......
  • 代码模板
    代码模板数论快速幂intqmi(inta,intb,intp){ intres=1; while(b){ if(b&1)res=res*a%p; a=a*a%p; b>>=1; } returnres;}线性筛法(素数+欧拉函数)intst[N1],pri[N1],cnt,phi[N1];intgetp(intn){ phi[1]=1; for(inti=......
  • P3389 【模板】高斯消元法
    #include<bits/stdc++.h>usingnamespacestd;doublemax(doublea,doubleb){ if(a>=b)returna; if(a<b)returnb;}intn;doublea[1010][1010];doublea1[1010][1010];intmain(){ scanf("%d",&n); for(inti=1;i<=n;i++) { ......
  • P3374 【模板】树状数组 1
    part1#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;structnode1{intl,r,value;};node1node[2000020];inta[500010];voidmt(intp,intl,intr){intmid=(l+r)>>1;node[p].l=l;node[p].r=r;if(l==r)......
  • P3368 【模板】树状数组 2
    #include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintMax=500005;inta[Max];intn,m;intlowbit(intx){ returnx&-x;}voidadd(intx,inty){ while(x<=n){ a[x]+=y; x+=lowbit(x); }}intsum(intx)......
  • ELK运维文档
    Logstash目录LogstashMonitoringAPINodeInfoAPIPluginsInfoAPINodeStatsAPIHotThreadsAPIlogstashexporter指标插件管理离线安装插件更新插件移除插件使用Gem私有库性能调优TroubleshootingLogstashFAQlogstash可能出现的问题?如何保证logstash事件不丢失?logstash是否......
  • U3D外包Unity最新版本的官方技术文档
    Unity5.0新特性——物理系统Unity5.0已升级到PhysX3.3SDK。在对4.x项目采取任何操作之前,请快速查看此博文。 关于更新的概述Unity5.0物理系统的预计工作速度是以前版本的2倍。以前熟悉的大多数组件仍然存在,它们的工作方式也和以前一样。当然,有些行为不可能......
  • html2canvas使用文档
    安装npminstallhtml2canvasoryarnaddhtml2canvas引入importhtml2canvasfrom'html2canvas'使用<divref="canvasDom"><h4>Helloworld!</h4></div>constcanvasDom=this.$refs.canvasDomthis.$toast.loading(&......
  • 【模板】多项式半家桶 version 2
    #include<bits/stdc++.h>usingnamespacestd;#ifdefLOCAL#definedebug(...)fprintf(stderr,##__VA_ARGS__)#else#defineendl"\n"#definedebug(...)void(0)#endiftypedeflonglongLL;template<unsignedumod>structmodint{......
  • 如何在word中优雅地插入代码
    如何在word中优雅地插入代码呢?网上的方法大致有这么几种:利用notepad++来实现(操作路径有点长,比较麻烦)自己在word做模版(这个模版折腾下来倒是可以一劳永逸,但是不支持不同语言的高亮)利用word的宏来实现,但需要写宏脚本(比较麻烦)国外的 www.planetb.ca 网站进去太慢了,体验很不......