[alibaba/easyexcel]@ExcelProperty指定转换器依然报Can not find 'Converter' support class

2024-05-11 520 views
9

触发Bug的代码

// 字段 省略setter getter
@ExcelProperty(value = "性别", converter = GenderEnumExcelConverter.class)
private GenderEnum gender;

// 转换器
public class GenderEnumExcelConverter implements Converter<GenderEnum> {
    @Override
    public Class supportJavaTypeKey() {
        return GenderEnum.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public GenderEnum convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        for (GenderEnum value : GenderEnum.values()) {
            if (Objects.equals(value.getCnName(), cellData.getStringValue())) return value;
        }
        return null;
    }

    @Override
    public CellData convertToExcelData(GenderEnum value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData(value.getCnName());
    }
}
com.alibaba.excel.exception.ExcelDataConvertException: Can not find 'Converter' support class GenderEnum.
    at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.doConvert(AbstractExcelWriteExecutor.java:111) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.convert(AbstractExcelWriteExecutor.java:96) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.converterAndSet(AbstractExcelWriteExecutor.java:41) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:164) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:72) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:56) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:58) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161) ~[easyexcel-2.2.10.jar:na]
    at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146) ~[easyexcel-2.2.10.jar:na]

回答

2

请检查下是否还有其他字段

6

解决 Map<String, Converter> stringConverterMap = DefaultConverterLoader.loadDefaultWriteConverter(); FastRetailingTypeCover fastRetailingTypeCover = new FastRetailingTypeCover(); stringConverterMap.put(ConverterKeyBuild.buildKey(fastRetailingTypeCover.supportJavaTypeKey()),fastRetailingTypeCover);

5

对Map<String,String>数据进行写操作时,会报com.alibaba.excel.exception.ExcelWriteDataConvertException: Can not find 'Converter' support class Map.错误