EasyExcel 导出下拉框

实体类信息 UserExcelDto get/set个人需要添加

@ExcelIgnore
private String password;

@ExcelProperty({"用户", "姓名"})
private String realName;

@ExcelProperty(value = {"用户", "是否启用"})
private Integer enabled;

@ExcelProperty(value = {"用户", "是否锁定"})
private Integer locked;

EnableEnum 启用枚举

public enum EnableEnum {
    /**
     * 未启用
     */
    ENABLED(0, "未启用"),

    /**
     * 启用
     */
    DISABLED(1, "启用");

    private final Integer status;

    private final String statusName;

    public Integer getStatus() {
        return status;
    }

    public String getStatusName() {
        return statusName;
    }

    EnableEnum(Integer status, String statusName) {
        this.status = status;
        this.statusName = statusName;
    }

    public static String[] getAllStatusName() {
        EnableEnum[] enums = values();
        String[] statusName = new String[enums.length];
        for (int i = 0; i < enums.length; i++) {
            statusName[i] = enums[i].getStatusName();
        }
        return statusName;
    }
}

LockEnum 锁定枚举

public enum LockEnum {
    /**
     * 解锁
     */
    LOCK(0, "解锁"),

    /**
     * 锁定
     */
    UNLOCK(1, "锁定");

    private final Integer status;

    private final String statusName;

    public Integer getStatus() {
        return status;
    }

    public String getStatusName() {
        return statusName;
    }

    LockEnum(Integer status, String statusName) {
        this.status = status;
        this.statusName = statusName;
    }

    public static String[] getAllStatusName() {
        LockEnum[] enums = values();
        String[] statusName = new String[enums.length];
        for (int i = 0; i < enums.length; i++) {
            statusName[i] = enums[i].getStatusName();
        }
        return statusName;
    }
}

UserWriteHandler 继承 SheetWriteHandler

public class UserWriteHandler implements SheetWriteHandler {
    // 初始化下拉框数组
    private String[] enabledName = EnableEnum.getAllStatusName();
    // 初始化下拉框数组
    private String[] lockedNme = LockEnum.getAllStatusName();
    // 终止行
    private Integer lastRow;
    // 起始行
    private Integer firstRow = 0;

    public UserWriteHandler() {
    }

    public UserWriteHandler(Integer lastRow) {
        this.lastRow = lastRow;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        // 计算表头行数
        List firstRowList = new ArrayList<>();
        // 反射获取实体所有属性
        Field[] declaredFields = UserExcelDto.class.getDeclaredFields();
        // 循环获取属性注解
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(ExcelProperty.class)) {
                ExcelProperty declaredAnnotation = declaredField.getDeclaredAnnotation(ExcelProperty.class);
                firstRowList.add(declaredAnnotation.value().length);
            }
        }
        this.firstRow = Collections.max(firstRowList);

        // 计算最后一行 行数
        if (this.firstRow > 1) {
            this.lastRow += this.firstRow - 1;
        }
        if (this.firstRow < 1) {
            this.lastRow -= 1;
        }

        // 设置下拉框数据
        Map columnBoxMap = new HashMap<>();

        columnBoxMap.put(2, this.enabledName);
        columnBoxMap.put(4, this.lockedNme);

        Sheet sheet = writeSheetHolder.getSheet();
        // 开始设置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();
        for (Map.Entry entry : columnBoxMap.entrySet()) {
            // 起始行、终止行、起始列、终止列
            CellRangeAddressList addressList = new CellRangeAddressList(this.firstRow, this.lastRow,
                    entry.getKey(), entry.getKey());
            // 设置下拉框数据
            DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            // 处理Excel兼容性问题
            if (dataValidation instanceof XSSFDataValidation) {
                dataValidation.setSuppressDropDownArrow(true);
                dataValidation.setShowErrorBox(true);
            } else {
                dataValidation.setSuppressDropDownArrow(false);
            }
            sheet.addValidationData(dataValidation);
        }
    }

    public String[] getEnabledName() {
        return enabledName;
    }

    public void setEnabledName(String[] enabledName) {
        this.enabledName = enabledName;
    }

    public String[] getLockedNme() {
        return lockedNme;
    }

    public void setLockedNme(String[] lockedNme) {
        this.lockedNme = lockedNme;
    }

    public Integer getLastRow() {
        return lastRow;
    }

    public void setLastRow(Integer lastRow) {
        this.lastRow = lastRow;
    }

    public Integer getFirstRow() {
        return firstRow;
    }

    public void setFirstRow(Integer firstRow) {
        this.firstRow = firstRow;
    }
}

简单的导出 userExcelDtoList.size() 导出数据的总数

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