2.14 枚举的处理

实体对象属性是枚举支持几种处理模式,如下三总,最基础的无值数据,有值,以及有固定方法

  @Excel(name ="名字")
    private String name;
    @Excel(name ="性别")
    private Sex sex; //第一种
    @Excel(name ="基础状态" )
    private StatusEnum baseStatus; //第二种
    @Excel(name ="状态" , enumExportField = "message",
                        enumImportMethod = "getByMessage")
    private StatusEnum status;//第三种
public enum Sex {
    /**
     * 男
     */
    MAN,
    /**
     * 女
     */
    WOMAN
}
public enum StatusEnum {

    Init(0, "初始化"),
    Ready(1, "正常"),
    ChangePassword(2, "需要修改密码"),
    Frozen(4, "冻结"),
    Disabled(64, "禁用");
    private final Integer _code;
    private final String _message;

    StatusEnum(Integer code, String message) {
        _code = code;
        _message = message;
    }

    public Integer getValue() {
        return _code;
    }

    public String getMessage() {
        return _message;
    }

    public static StatusEnum getByMessage(String message){
        StatusEnum[] arr =  StatusEnum.values();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]._message.equals(message)){
                return arr[i];
            }
        }
        return null;
    }
}

上面的第一种和第二种都是输出原有的值,MAN,Init等枚举值
第三种是输出对应的方法值,Easypoi适应反射调用getByMessage方法获取方法值
测试代码


    @Test
    public void test() throws Exception {

        List<EnumDataEntity> list = new ArrayList<EnumDataEntity>();
        for (int i = 0; i < 100; i++) {
            EnumDataEntity client = new EnumDataEntity();
            client.setName("小明" + i);
            client.setSex(Sex.MAN);
            client.setStatus(StatusEnum.Init);
            client.setBaseStatus(StatusEnum.Ready);
            list.add(client);
        }
        Date start = new Date();
        ExportParams params = new ExportParams("枚举测试", "测试", ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, EnumDataEntity.class, list);
        System.out.println(new Date().getTime() - start.getTime());
        FileOutputStream fos = new FileOutputStream("D:/home/excel/EnumDataEntity.xlsx");
        workbook.write(fos);
        fos.close();
    }

输出效果

文档更新时间: 2020-04-27 14:22   作者:admin