基于word模板导出人员信息

接口基本信息

接口功能: 导出指定人员的个人信息文档

技术实现

1. 控制器层实现

java 复制代码
/**
 * 导出人员个人信息
 */
@ApiOperationSupport(order = 12)
@ApiOperation("导出人员个人信息")
@CommonLog("导出人员个人信息")
@SaCheckPermission("/biz/user/exportUserInfo")
@GetMapping(value = "/biz/user/exportUserInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void exportUserInfo(BizUserIdParam bizUserIdParam, HttpServletResponse response) throws IOException {
    bizUserService.exportUserInfo(bizUserIdParam, response);
}

2. 服务层接口定义

java 复制代码
/**
 * 导出用户个人信息
 **/
void exportUserInfo(BizUserIdParam bizUserIdParam, HttpServletResponse response) throws IOException;

3. 服务层实现逻辑

java 复制代码
@Override
public void exportUserInfo(BizUserIdParam bizUserIdParam, HttpServletResponse response) throws IOException {
    File destTemplateFile = null;
    File resultFile = null;
    try {
        // 1. 根据ID查询用户信息
        BizUser bizUser = this.queryEntity(bizUserIdParam.getId());
        
        // 2. 数据转换(如机构信息翻译等)
        transService.transOne(bizUser);
        
        // 3. 读取Word模板文件
        InputStream inputStream = POICacheManager.getFile("userExportTemplate.docx");
        // 创建临时模板文件
        destTemplateFile = FileUtil.writeFromStream(inputStream, FileUtil.file(FileUtil.getTmpDir() + 
                File.separator + "userExportTemplate.docx"));
        
        // 4. 构造模板填充参数
        Map<String, Object> map = BeanUtil.beanToMap(bizUser);
        
        // 5. 处理头像数据
        String avatarBase64;
        if(ObjectUtil.isNotEmpty(bizUser.getAvatar())) {
            avatarBase64 = bizUser.getAvatar();
        } else {
            // 如果没有头像,生成默认头像
            avatarBase64 = CommonAvatarUtil.generateImg(bizUser.getName());
        }
        // 将头像转换为图片实体(120x160像素)
        ImageEntity imageEntity = new ImageEntity(
            ImgUtil.toBytes(ImgUtil.toImage(StrUtil.split(avatarBase64, StrUtil.COMMA).get(1)), 
            ImgUtil.IMAGE_TYPE_PNG), 120, 160);
        map.put("avatar", imageEntity);
        
        // 6. 计算年龄(如果有生日)
        if(ObjectUtil.isNotEmpty(bizUser.getBirthday())) {
            try {
                long age = cn.hutool.core.date.DateUtil.betweenYear(
                    cn.hutool.core.date.DateUtil.parseDate(bizUser.getBirthday()), 
                    DateTime.now(), true);
                if(age != 0) {
                    map.put("age", age + "岁");
                }
            } catch (Exception ignored) {
                // 生日格式解析失败,忽略年龄计算
            }
        }
        
        // 7. 添加导出时间
        map.put("exportDateTime", DateUtil.format(DateTime.now(), DatePattern.CHINESE_DATE_PATTERN));
        
        // 8. 使用模板生成Word文档
        XWPFDocument doc = WordExportUtil.exportWord07(destTemplateFile.getAbsolutePath(), map);
        
        // 9. 创建临时输出文件
        resultFile = FileUtil.file(FileUtil.getTmpDir() + File.separator + 
                "SNOWY2.0系统B端人员信息_" + bizUser.getName() + ".docx");
        
        // 10. 写入文件并响应下载
        BufferedOutputStream outputStream = FileUtil.getOutputStream(resultFile);
        doc.write(outputStream);
        outputStream.close();
        
        // 11. 下载文件
        CommonDownloadUtil.download(resultFile, response);
        
    } catch (Exception e) {
        log.error(">>> 导出人员个人信息异常:", e);
        CommonResponseUtil.renderError(response, "导出失败");
    } finally {
        // 12. 清理临时文件
        if(ObjectUtil.isNotEmpty(destTemplateFile)) {
            FileUtil.del(destTemplateFile);
        }
        if(ObjectUtil.isNotEmpty(resultFile)) {
            FileUtil.del(resultFile);
        }
    }
}

依赖关系

核心依赖库

xml 复制代码
<!-- Word文档处理 -->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.4.0</version>
</dependency>

<!-- 文件操作工具 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

<!-- 数据转换 -->
<dependency>
    <groupId>com.fhs</groupId>
    <artifactId>core-trans</artifactId>
    <version>1.0.0</version>
</dependency>

总结

一个基于Word模板的人员个人信息导出功能,通过模板引擎将用户数据填充到Word文档中,支持头像处理和动态字段计算(如年龄计算),最终生成个性化的个人信息文档。该接口具有完善的错误处理和资源清理机制,确保系统稳定性和用户体验。

相关推荐
西凉的悲伤1 小时前
redis和数据库实现分布式锁
java·数据库·redis·分布式
weixin_523185321 小时前
Java内存模型详解:栈、堆、方法区、本地方法栈与程序计数器
java·开发语言
ywl4708120871 小时前
泛型extends和super的区别
java
惜缘破军1 小时前
基于 Spring Boot 4 和 Spring Cloud 2025 的微服务基础框架 hdfk7-boot
java
小白起 v1 小时前
从零搭建一个现代化的验证码登录系统:Spring Boot + 阿里云短信实战教程
java·阿里云
未若君雅裁2 小时前
工厂模式详解:简单工厂、工厂方法与抽象工厂
java·开发语言
不会写DN2 小时前
通过php 中的Route:: 的写法了解什么是静态类调用
android·java·php
小刘|2 小时前
SpringAIAlibaba快速接入阿里云百炼
java·spring boot·spring·maven