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();
        }

    }

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

相关推荐
四斤年华6 分钟前
关于SpringBoot在MultipartFile上java.nio.file.NoSuchFileException: /tmp/undertow
java·spring boot·nio
木井巳6 分钟前
【递归算法】字母大小写全排列
java·算法·leetcode·决策树·深度优先
杰克尼10 分钟前
天机学堂项目总结(day3~day4)
java·开发语言·spring
摇滚侠18 分钟前
给我提供一个 sqlyog 下载地址
java
Seven9722 分钟前
【从0到1构建一个ClaudeAgent】协作-团队协议
java
希望永不加班31 分钟前
SpringBoot 中 AOP 实现接口限流
java·spring boot·后端·spring
恼书:-(空寄32 分钟前
虚拟线程:Java 高并发编程的终局?
java·虚拟线程
oyzz12033 分钟前
spring loC&DI 详解
java·spring·rpc
IDIOT___IDIOT36 分钟前
Python 命令行参数 + 环境变量
java·服务器·python