EasyExcel⾃定义Converter解决性别转换问题
那么这篇⽂章继续介绍⼀下性别转换的问题,⼀般我们代码中都会使⽤ 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{
}catch(IOException e){
<("导⼊出错:{}", 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="+ de(fileName,"utf-8")+".xlsx");
EasyExcel.OutputStream(), User.class).registerConverter(new LocalDateTimeConverter()).sheet("test").doWrite(list);
}catch(Exception e){
<("下载报表异常:{}", e.getMessage());
throw new RuntimeException("下载报表异常");
}
}
private List<User>getList(){
List<User> list =new ArrayList<>();
LocalDateTime now = w();
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,所以编写如下转换器!注意要实现的是l.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"男".StringValue())?1:0;
}
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)throws Exception { return new CellData(value.equals(1)?"男":"⼥");
}
}
划重点!Sex的converter编写完了,怎么才能让它⽣效呢?有⼀种解决⽅案!
1. 到需要转换的字段,在@ExcelProperty上添加converter属性
修改代码完毕之后再重启就可以了,不管是导⼊还是导出都没问题!Excel中的男/⼥导⼊之后在代码中会⾃动变为1/0,代码中的1/0导出之后会⾃动变为男/⼥!赶快试试吧!
细⼼的⼩伙伴已经发现了,LocalDateTime的converter有三种⽣效⽅案,但是sex的converter只有⼀种!点击查看并带你追踪⼀下源码并给出具体的解决⽅案!
加油!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。