上篇文章已经介绍了自定义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 ArrayList<>();
AnalysisEventListener listener = new AnalysisEventListener() {
@Override
public void invoke(Object data, AnalysisContext context) {
list.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("导入数据完毕");
}
};
try {
EasyExcel.read(file.getInputStream(), User.class, listener).sheet(0).doRead();
} catch (IOException e) {
log.error("导入出错:{}", e.getMessage());
}
list.forEach(System.out::println);
}
@PostMapping("/exportData")
public void exportData(HttpServletResponse response) {
List<User> list = getList();
try {
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setCharacterEncoding("utf-8");
String fileName = "三好学生表";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx");
EasyExcel.write(response.getOutputStream(), User.class).registerConverter(new LocalDateTimeConverter()).sheet("test").doWrite(list);
} catch (Exception e) {
log.error("下载报表异常:{}", e.getMessage());
throw new RuntimeException("下载报表异常");
}
}
private List<User> getList() {
List<User> list = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
User xd = new User("熊大", 10, 1, now);
User ne = new User("牛二", 20, 0, now);
User zs = new User("张三", 30, 1, now);
User ls = new User("李四", 40, 1, now);
User ww = new User("王五", 50, 0, now);
list.add(xd);
list.add(ne);
list.add(zs);
list.add(ls);
list.add(ww);
return list;
}
}
访问exportData接口会发现不是我们想要的结果:为什么导出的文件性别那一列只有0 1 没有男 女呢?好吧!那我们就自定义转换器,因为我们要转换0和1即Integer,所以编写如下转换器!注意要实现的是com.alibaba.excel.converters.Converter
接口,别引用错包了!
public class SexConverter implements Converter<Integer> {
@Override
public Class supportJavaTypeKey() {
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return "男".equals(cellData.getStringValue()) ? 1 : 0;
}
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData(value.equals(1) ? "男" : "女");
}
}
修改代码完毕之后再重启就可以了,不管是导入还是导出都没问题!Excel中的男/女导入之后在代码中会自动变为1/0,代码中的1/0导出之后会自动变为男/女!
赶快试试吧!
加油!