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/

相关推荐
逸Y 仙X3 分钟前
文章十三:ElasticSearch数据更新实战
java·大数据·elasticsearch·搜索引擎·jenkins
说给风听.4 分钟前
从零学会 Java 异常处理 —— 核心语法、自定义异常与面试指南
java·开发语言·面试
身如柳絮随风扬6 分钟前
Java JDBC 从入门到进阶
java·开发语言
Joker`s smile8 分钟前
Spring Cloud Alibaba 基础入门实践
java·spring boot·后端·spring cloud
nbsaas-boot10 分钟前
AI编程的现实困境与未来路径:从“可用”到“可靠”的跃迁
java·运维·开发语言·架构
东离与糖宝15 分钟前
Java 26 Vector API 第十一轮孵化:AI 推理性能提升 80% 实战
java·人工智能
星如雨グッ!(๑•̀ㅂ•́)و✧34 分钟前
WebClient请求样例
java
两年半的个人练习生^_^36 分钟前
dynamic-datasource多数据源使用和源码讲解
java·开发语言·数据库·mybatis
无籽西瓜a38 分钟前
【西瓜带你学设计模式 | 第一期-单例模式】单例模式——定义、实现方式、优缺点与适用场景以及注意事项
java·后端·单例模式·设计模式
空空潍40 分钟前
Spring AI 实战系列(二):ChatClient封装,告别大模型开发样板代码
java·人工智能·spring