当前位置: 首页 > article >正文

Java poi 模板导出Word 带图片

Java poi 模板导出Word 带图片

重点!!!

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

最终效果

在这里插入图片描述

模板

在这里插入图片描述

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

Maven依赖

 	<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>

代码

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

    }

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


http://www.kler.cn/a/391421.html

相关文章:

  • React中右击出现自定弹窗
  • 了解外呼系统线路分类,提升业务效率
  • Django框架:Form组件及参数
  • ubuntu 22.04 镜像源更换
  • 51单片机使用NRF24L01进行2.4G无线通信
  • 系统架构设计师论文:大数据Lambda架构
  • 【JAVA基础】JVM双亲委派
  • Redis 内存突增时,如何定量分析其内存使用情况
  • Python OpenCV孤立点检测
  • Ansible内置模块之known_hosts
  • aws申请ssl证书的方法【该证书仅供aws】
  • etcd之etcd集群实践(六)
  • RocketMQ发送消息之事务消息
  • Python字符串格式化与数字处理指南
  • Python 连接 Redis 进行增删改查(CRUD)操作
  • 【微服务设计】从理论到实践:2PC(两阶段提交)与SAGA的全面比较与示例
  • Rust 建造者模式
  • IEC60870-5-104 协议源码架构详细分析
  • Mysql学习笔记(一):Mysql的架构
  • 单片机工程使用链接优化-flto找不到定义_链接静态库