上篇文章已经介绍了自定义LocalDateTime转换器解决了LocalDateTime导入导出的问题!
那么这篇文章继续介绍一下性别转换的问题,一般我们代码中都会使用
1/0 分别代表 男/女
,可是Excel中都是用"男","女"表示的,怎么做才能单独将性别转化为1和0
呢?没错还是自定义转换器!
因为这个案例有LocalDateTime,所以请查看上篇文章采取任意一种解决方案,建议采用第二种:
新建User类,然后在类上添加EasyExcel的注解
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
// 1 男 0 女
@ExcelProperty(value = "性别", index = 2)
private Integer sex;
@ExcelProperty(value = "创建时间", index = 3)
private LocalDateTime createTime;
}
新建Controller用于测试
@Slf4j
@RestController
public class ExcelController {
@PostMapping("/importData")
public void importData(@RequestParam("file") MultipartFile file) throws IOException {
if (file == null) return;
ArrayList<Object> list = new