EasyExcel增加下拉选择框

概述

本文将用示例介绍如何用EasyExcel导出下拉框

示例

编写样式处理类: TitleHandler

@Slf4j
public class TitleHandler implements SheetWriteHandler {

    /**
     * 下拉框值
     */
    private Map<Integer,String[]> dropDownMap;

    /**
     * 多少行有下拉
     */
    private final static Integer rowSize = 200;

    public TitleHandler(Map<Integer,String[]> dropDownMap) {
        this.dropDownMap = dropDownMap;
    }

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

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        DataValidationHelper helper = sheet.getDataValidationHelper();

        dropDownMap.forEach((celIndex, strings) -> {
            // 区间设置
            CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, rowSize, celIndex, celIndex);
            // 下拉内容
            DataValidationConstraint constraint = helper.createExplicitListConstraint(strings);
            DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
            sheet.addValidationData(dataValidation);
        });
    }
}

编写工具类

public class EasyExcelUtil {
    public static <T> void writeExcelWithModel(OutputStream outputStream, Class<T> clazz, Map<Integer,String[]> dropDownMap) throws IOException {
        EasyExcel.write(outputStream, clazz).registerWriteHandler(new TitleHandler(dropDownMap)).sheet("模板").doWrite(ListUtil.empty());
    }
}

测试

public class Test{
    @Data
    @ColumnWidth(20) // 定义列宽
    public static class TestVO {
        @ExcelProperty(value = "*姓名", index = 0)
        private String name;
        @ExcelProperty(value = "*年龄", index = 1)
        private int age;
        @ExcelProperty(value = "学校", index = 2)
        private String school;
    }
    /**
     * 测试导出下拉款
     * @throws IOException
     */
    @Test
    public void testDropDown() throws IOException {
        // 输出流
        OutputStream outputStream = new FileOutputStream(new File("D:\\1.xlsx"));
        HashMap<Integer, String[]> dropDownMap = new HashMap<>();
        // 指定下拉框
        String[] school = {"一中","二中","三中"};
        dropDownMap.put(2,school);
        EasyExcelUtil.writeExcelWithModel(outputStream, TestVO.class, dropDownMap);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
easyexcel 中,可以通过实现 Converter 接口来进行下拉选择的构建。 1. 首先,需要定义下拉选择的选项,可以使用枚举类型或者自定义类来进行定义。例如,定义一个颜色选择的枚举类型: public enum ColorEnum { RED("红色"), GREEN("绿色"), BLUE("蓝色"); private String desc; ColorEnum(String desc) { this.desc = desc; } public String getDesc() { return desc; } public static List<String> getValues() { List<String> values = new ArrayList<>(); for (ColorEnum color : ColorEnum.values()) { values.add(color.getDesc()); } return values; } } 2. 实现 Converter 接口,定义下拉选择的转换规则。例如,定义一个将枚举类型转换为字符串类型的转换器: public class ColorEnumConverter implements Converter<ColorEnum> { @Override public Class<ColorEnum> supportJavaTypeKey() { return ColorEnum.class; } @Override public CellData<String> convertToExcelData(ColorEnum value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return new CellData<>(value.getDesc()); } @Override public ColorEnum convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { String desc = cellData.getStringValue(); for (ColorEnum color : ColorEnum.values()) { if (color.getDesc().equals(desc)) { return color; } } return null; } } 3. 在导出 Excel 时,使用 @ExcelProperty 注解指定字段需要进行下拉选择,并使用 @ContentStyle 注解指定下拉选择的样式: @ExcelProperty(value = "颜色", converter = ColorEnumConverter.class) @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10) private ColorEnum color; 4. 在导入 Excel 时,使用 @ExcelProperty 注解指定字段需要进行下拉选择,并使用 @HeadStyle 注解指定下拉选择的样式: @ExcelProperty(value = "颜色", converter = ColorEnumConverter.class) @HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10) private ColorEnum color; 以上就是在 easyexcel 中构建下拉选择的步骤。需要注意的是,在进行下拉选择时,需要将字段类型设置为支持下拉选择的类型,例如枚举类型或者自定义类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值