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

相关推荐
SimonKing1 小时前
震惊:怎么回事,opencode一轮对话耗尽免费额度
java·后端·程序员
装不满的克莱因瓶1 小时前
【SpringAI Alibaba】ReactAgent响应SSE实时流式传输案例实战
java·人工智能·ai·sse·springai
杨凯凡1 小时前
Spring Boot 文件上传怎么做才安全?流式落盘、路径校验与数据库一致性实战
java·spring boot·文件上传·后端安全·数据库一致性
这个昵称不容易2 小时前
旅游管理系统的设计与实现(SpringBoot + MySQL + Vue 前后端分离)
java·springboot·毕设
Rain的Java大神之路2 小时前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
天天被压力3 小时前
【A股分钟级高频数据完全手册 #11】券商实时行情数据接口:实时交易(券商源)接入与字段
java·人工智能·python
天天被压力3 小时前
【A股分钟级高频数据完全手册 #12】股票五档盘口数据接口:买卖五档实时盘口解析
java·人工智能·python
Wang's Blog12 小时前
Java 项目实战: 交友项目 - FastDFS:入门案例
java·开发语言·交友
apcipot_rain13 小时前
C++转Java日记(03)
java·开发语言·c++
皮皮林55113 小时前
Java实现微信扫码登录
java