easyexcel将数据库枚举字段转换成字符串类型(例:1/男,2/女)的解决方法

实现Converter 接口

重写下面几个方法:

public class SettlementConverter implements Converter {
    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }

    @Override
    public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }
}

在convertToExcelData()方法里面写将数据库字段转换成EXCEL显示字段的逻辑,如:

    @Override
    public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if(integer == 1){
            return new CellData("男");
        }else {
            return new CellData("女");
        }
    }

然后在实体类上使用:

@ExcelProperty(value = "性别",converter = GenderConverter.class)

 

你可能感兴趣的:(java,笔记,java)