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

相关推荐
rannn_1119 小时前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
IKUN家族10 小时前
Spring IoC&DI
java·spring·rpc
山荷枝12 小时前
03-框架--Spring
java·后端·spring
ChaHae-In12 小时前
SpringBoot统一功能处理
java·spring boot·后端
coder!mq12 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding12 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
心平气和量大福大13 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
9523614 小时前
Spring-cloud配置中心
java·spring·spring cloud·微服务
ydd10010014 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
真上帝的左手15 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso