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/

相关推荐
是有头发的程序猿2 分钟前
电商自动化实战:淘宝/天猫item_get商品详情API全量采集教程(Python源码)
java·python·自动化
古韵17 分钟前
告别手写分页逻辑:usePagination 从 50 行到 3 行
java·前端
北城以北888825 分钟前
Quartz定时任务
java·spring boot·intellij-idea
许彰午43 分钟前
JWT的四种设计策略——轻量负载缓存外置上下文线程统一验证
java·安全·缓存·tomcat
小bo波1 小时前
Java Swing 可视化素数筛:动态演示 1~120 质数筛选【附完整源码】
java·算法·可视化·swing·素数
天若有情6732 小时前
【C++趣味实战】仿写Burp代理逻辑!自定义可控迭代器:拦截Intercept/放行Forward/重放Repeater全实现
java·开发语言·c++
迷藏4942 小时前
Python+DuckDB:轻量级BI流水线实战
java·开发语言·python·原型模式
磊 子2 小时前
C++function与bind绑定器讲解
java·jvm·c++
咋吃都不胖lyh2 小时前
短期记忆和长期记忆都存 MySQL
android·java·开发语言
浮游本尊2 小时前
前端vue转后端java学习路径
java·前端·vue.js