定义实体类
在EasyExcel中,以面向对象思想来实现导入导出,无论是导入数据还是导出数据都可以想象成具体某个对象的集合,所以为了实现导出用户信息功能,首先创建一个用户对象UserDO
实体类,用于封装用户信息:
| |
| |
| |
| |
| |
| @Data |
| public class UserDO { |
| @ExcelProperty("用户编号") |
| @ColumnWidth(20) |
| private Long id; |
| |
| @ExcelProperty("用户名") |
| @ColumnWidth(20) |
| private String username; |
| |
| @ExcelIgnore |
| private String password; |
| |
| @ExcelProperty("昵称") |
| @ColumnWidth(20) |
| private String nickname; |
| |
| @ExcelProperty("生日") |
| @ColumnWidth(20) |
| @DateTimeFormat("yyyy-MM-dd") |
| private Date birthday; |
| |
| @ExcelProperty("手机号") |
| @ColumnWidth(20) |
| private String phone; |
| |
| @ExcelProperty("身高(米)") |
| @NumberFormat("#.##") |
| @ColumnWidth(20) |
| private Double height; |
| |
| @ExcelProperty(value = "性别", converter = GenderConverter.class) |
| @ColumnWidth(10) |
| private Integer gender; |
| } |
上面代码中类属性上使用了EasyExcel核心注解:
- @ExcelProperty:核心注解,
value
属性可用来设置表头名称,converter
属性可以用来设置类型转换器;
- @ColumnWidth:用于设置表格列的宽度;
- @DateTimeFormat:用于设置日期转换格式;
- @NumberFormat:用于设置数字转换格式。
自定义转换器
在EasyExcel中,如果想实现枚举类型到字符串类型转换(例如gender
属性:1 -> 男
,2 -> 女
),需实现Converter
接口来自定义转换器,下面为自定义GenderConverter
性别转换器代码实现:
| |
| |
| |
| |
| |
| public class GenderConverter implements Converter<Integer> { |
| @Override |
| public Class<?> supportJavaTypeKey() { |
| return Integer.class; |
| } |
| |
| @Override |
| public CellDataTypeEnum supportExcelTypeKey() { |
| return CellDataTypeEnum.STRING; |
| } |
| |
| @Override |
| public Integer convertToJavaData(ReadConverterContext<?> context) { |
| return GenderEnum.convert(context.getReadCellData().getStringValue()).getValue(); |
| } |
| |
| @Override |
| public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) { |
| return new WriteCellData<>(GenderEnum.convert(context.getValue()).getDescription()); |
| } |
| } |

| |
| |
| |
| |
| |
| @Getter |
| @AllArgsConstructor |
| public enum GenderEnum { |
| |
| |
| |
| |
| UNKNOWN(0, "未知"), |
| |
| |
| |
| |
| MALE(1, "男性"), |
| |
| |
| |
| |
| FEMALE(2, "女性"); |
| |
| private final Integer value; |
| |
| @JsonFormat |
| private final String description; |
| |
| public static GenderEnum convert(Integer value) { |
| return Stream.of(values()) |
| .filter(bean -> bean.value.equals(value)) |
| .findAny() |
| .orElse(UNKNOWN); |
| } |
| |
| public static GenderEnum convert(String description) { |
| return Stream.of(values()) |
| .filter(bean -> bean.description.equals(description)) |
| .findAny() |
| .orElse(UNKNOWN); |
| } |
| } |
定义接口
| |
| |
| |
| |
| |
| @RestController |
| @RequestMapping("/excel") |
| public class ExcelController { |
| |
| @GetMapping("/export/user") |
| public void exportUserExcel(HttpServletResponse response) { |
| try { |
| this.setExcelResponseProp(response, "用户列表"); |
| List<UserDO> userList = this.getUserList(); |
| EasyExcel.write(response.getOutputStream()) |
| .head(UserDO.class) |
| .excelType(ExcelTypeEnum.XLSX) |
| .sheet("用户列表") |
| .doWrite(userList); |
| } catch (IOException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException { |
| response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| response.setCharacterEncoding("utf-8"); |
| String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\+", "%20"); |
| response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| private List<UserDO> getUserList() throws IOException { |
| ObjectMapper objectMapper = new ObjectMapper(); |
| ClassPathResource classPathResource = new ClassPathResource("mock/users.json"); |
| InputStream inputStream = classPathResource.getInputStream(); |
| return objectMapper.readValue(inputStream, new TypeReference<List<UserDO>>() { |
| }); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-05-08 windows下查找端口、PID、查找进程、杀死进程