java写入word表格(poi-tl)

1.导入依赖

复制代码
<!--poi-tl-->
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.12.0</version>
</dependency>

2.代码

自己创建模板。放在(resource/file)

复制代码
public ApiResult<String> download2(
            HttpServletResponse response,
            @RequestParam(value = "apiCode", required = true) String apiCode
    ) throws Exception {
        String templatePath = getResourceFilePath("file","wordtempalte.docx");//resource下面有file文件夹,文件夹下面是模板
        ApiPermitInfo data = getPermitInfo(apiCode);
        writeword(templatePath, response,data);//写入文件
        return null;
    }
    public static String getResourceFilePath(String folderPath, String fileName) {
        // 构建资源路径
        String resourcePath = folderPath + "/" + fileName;
        // 获取资源的URL
        URL resourceUrl = ResourceUtil.class.getClassLoader().getResource(resourcePath);
        if (resourceUrl != null) {
            // 对于JAR包中的资源,直接返回URL的路径部分
            return resourceUrl.getPath();
        } else {
            // 资源不存在
            return null;
        }
    }
    private void writeword(String templatePath, HttpServletResponse response,ApiPermitInfo data) throws IOException {
        //解析模板
        XWPFTemplate template = XWPFTemplate.compile(templatePath);
        //封装模型数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("apiLabel","apiLabel");
        map.put("apiPath", Texts.of("apiPath").color("0000FF").bold().create());
        
        //apiParams  getApiParams方法获取一个List类型
        map.put("apiParams", Tables.create(getApiParams(data.getApiParams()).toArray(new RowRenderData[0])));
        //渲染数据
        template.render(map);
        //以文件形式输出
        //      template.writeAndClose(new FileOutputStream(outputPath));//文件流
        //下载
        response.setContentType("application/octet-stream");
        String formattedNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));
        response.setHeader("Content-disposition", "attachment;filename=\"" + formattedNow+".docx" + "\"");
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);//浏览器下载
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        return;
    }

3.效果

参考:

poi-tl的使用(通俗易懂,全面,内含动态表格实现 !)-CSDN博客

https://deepoove.com/poi-tl/

相关推荐
程序猿阿越16 分钟前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
程序无bug21 分钟前
Spring6 当中 Bean 的生命周期的详细解析:有五步,有七步,有十步
java
二川bro24 分钟前
飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
java·人工智能·重构
Q_9709563942 分钟前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724091 小时前
Maven
java·数据库·maven
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
木头没有瓜2 小时前
idea离线安装插件
java·ide·intellij-idea
llwszx2 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
述雾学java3 小时前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel