Java poi 模板导出Word 带图片

Java poi 模板导出Word 带图片

重点!!!

官方文档https://deepoove.com/poi-tl/#_maven

最终效果

模板

其实内容都在官方文档里写的非常明白了 我这里只是抛砖引玉。

Maven依赖

xml 复制代码
 	<poi.version>4.1.2</poi.version>
    <poi.tl.version>1.10.0</poi.tl.version>
<!-- excel工具 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!--poi Word-->
            <dependency>
                <groupId>com.deepoove</groupId>
                <artifactId>poi-tl</artifactId>
                <version>${poi.tl.version}</version>
            </dependency>

代码

java 复制代码
    @GetMapping("/word/export/{id}")
    public void word(@PathVariable("id") Long id,HttpServletResponse response)
    {
    	//这里自己查询需要导出的内容
        GenPersonWordInfoVO wordInfo = genPersonBaseService.getWordInfo(id);
        //将JavaBean转为map 
        HashMap<String, Object> map = new HashMap<>();
        Field[] fields = wordInfo.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(wordInfo));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //这里放入图片
        map.put("headImg", Pictures.ofUrl(wordInfo.getHeadImg()).size(100, 150).center().create());
        //这里声明这些字段是列表 需要使用LoopRowTableRenderPolicy 类来处理 看名字也可以发现是循环table的行
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder()
                .bind("healthHistory", policy)
                .bind("checkHistory", policy)
                .bind("touchHistory", policy)
                .bind("diagnosisHistory", policy)
                .bind("treatmentHistory", policy)
                .build();
        //加载模板文件 从resources目录下加载
        ClassPathResource resource = new ClassPathResource("template/person_file_template.docx");
        XWPFTemplate template = null;
        try {
            template = XWPFTemplate.compile(resource.getInputStream(),config).render(map);
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + System.currentTimeMillis()+".docx");
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

最后,官方文档非常详细,完全可以参考官方文档。

相关推荐
后端AI实验室3 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
程序员清风5 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme5 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
Be_Better5 小时前
学会与虚拟机对话---ASM
java
开源之眼7 小时前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
Maori3168 小时前
放弃 SDKMAN!在 Garuda Linux + Fish 环境下的优雅 Java 管理指南
java
用户908324602739 小时前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
小王和八蛋9 小时前
DecimalFormat 与 BigDecimal
java·后端
beata9 小时前
Java基础-16:Java内置锁的四种状态及其转换机制详解-从无锁到重量级锁的进化与优化指南
java·后端
IT探险家9 小时前
你的第一个 Java 程序就翻车?HelloWorld 的 8 个隐藏陷阱
java