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/

相关推荐
ldj20204 分钟前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿5 分钟前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
风象南17 分钟前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山18 分钟前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos
Y40900118 分钟前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
YuTaoShao19 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
布朗克16819 分钟前
java常见的jvm内存分析工具
java·jvm·数据库
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
Dcs6 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java