基于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·大数据·word·powerpoint·.net
俊昭喜喜里2 天前
java中的继承和多态的区别
java
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕2 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖2 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时2 天前
01-06-A-JVM排查实战详解
java·jvm
vipxieliang2 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
ba_pi2 天前
mysql 查询所有表名并授权
java
ProcessOn官方账号2 天前
java函数式编程--入门基础
java·编程·函数式编程