本文最后更新于 2026年5月21日 上午
下载导入用户模板接口的实现
下载导入用户模板接口的需求
其实和导出差不多
就相当于导出一个空白的表
要有一条初始数据
代码实现
写起来不用像导出的那么麻烦 因为只是空表没有数据
AdminUserController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @GetMapping("/template") @SystemLog(businessName = "下载导入用户模板") @ApiOperation(value = "下载导入用户模板接口", notes = "下载导入用户的Excel模板") public void downloadImportUsersTemplate(HttpServletResponse response) { try { List<UserImportTemplateVo> templateData = Collections.singletonList( new UserImportTemplateVo( "云梦泽", "云梦泽", "弟弟", "2962933152@qq.com", "18329384753" ) );
String fileName = "用户导入模板_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); ExcelExportUtil.exportExcel(response, templateData, UserImportTemplateVo.class, fileName, "用户导入模板"); } catch (Exception e) { throw new RuntimeException("下载用户模板失败: " + e.getMessage(), e); }
|
创建 UserImportTemplateVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
@Data @NoArgsConstructor @AllArgsConstructor public class UserImportTemplateVo { @ColumnWidth(15) @ExcelProperty("用户名") @ApiModelProperty(value = "用户名", example = "云梦泽") @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) private String username;
@ColumnWidth(15) @ExcelProperty("昵称") @ApiModelProperty(value = "昵称", example = "云梦泽") @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) private String nickname;
@ColumnWidth(10) @ExcelProperty("性别") @ApiModelProperty(value = "用户性别,(0 男,1 女,2 隐藏)", example = "0") @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) private String sex;
@ColumnWidth(30) @ExcelProperty("邮箱") @ApiModelProperty(value = "用户邮箱", example = "2962933152@qq.com") @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) private String email;
@ColumnWidth(25) @ExcelProperty("手机号") @ApiModelProperty(value = "手机号", example = "18329384753") @JSONField(name = "mobile") @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER) private String phone; }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/