基于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文档中,支持头像处理和动态字段计算(如年龄计算),最终生成个性化的个人信息文档。该接口具有完善的错误处理和资源清理机制,确保系统稳定性和用户体验。

相关推荐
企业数字化笔记2 分钟前
多租户配额怎么控制?用量口径、并发预留与超额处理的实现方法
java·数据库·spring boot
巴勒个啦30 分钟前
Prompt Engineering 实战:5 个让 AI 生成生产级业务代码的工程模式
java·前端
倚楼听风语1 小时前
Kotlin 协程与 suspend 原理详解
java·算法·kotlin
开开心心就好1 小时前
文件被占用删不掉?解锁工具强制删除
java·运维·服务器·开发语言·前端·pdf·ocr
weixin_460443561 小时前
Java+Linux私有化训考平台:服务、数据库与终端部署结构解析
java·linux·数据库·私有化部署·企业培训考试系统·企业考试系统
云和数据.ChenGuang1 小时前
python大小整数范围解析
java·开发语言·python
Java内核笔记2 小时前
Spring AI 生产级 Chat 接口:6 个开关、13 个坑,其中 2 个开关根本没生效
java·人工智能·后端
雷一言说2 小时前
Spring AI 2.0 RAG:用 Metadata Filter 做多租户检索隔离
java·spring
WeiXin_DZbishe2 小时前
基于springboot装修风格选择系统的设计与开发-计算机毕设【课程设计】77964
java·vue.js·spring boot·后端·python·php·课程设计
XZ-0700012 小时前
week9-三元表达式
java·前端·python