easyexcel icon indicating copy to clipboard operation
easyexcel copied to clipboard

3.0.5版本实现NullableObjectConverter接口替换null值

Open kaciry0927 opened this issue 3 years ago • 1 comments

异常代码

// 这是实现NullableObjectConverter接口的复写方法
    @Override
    public String convertToJavaData(ReadConverterContext<?> context) throws AnalyzerException, ParseException {
        String fieldName = Optional.ofNullable(context).map(ReadConverterContext::getContentProperty).map(ExcelContentProperty::getField).map(Field::getName).orElse("");
        switch (Objects.requireNonNull(context).getReadCellData().getType()) {
            case STRING: {
                    return context.getReadCellData().getStringValue() == null ? StringUtils.EMPTY : context.getReadCellData().getStringValue();
            }
            default:
                return null;
        }
    }

ConverterUtils

private static Object doConvertToJavaObject(ReadCellData<?> cellData, Class<?> clazz,
        ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap, AnalysisContext context,
        Integer rowIndex, Integer columnIndex) {
      // 当Excel值为空的时候,已下代码直接返回了null,并不会执行上面代码块的自定义处理
        boolean canNotConverterEmpty = cellData.getType() == CellDataTypeEnum.EMPTY
            && !(converter instanceof NullableObjectConverter);
        if (canNotConverterEmpty) {
            return null;
        }

    }

异常提示 在3.0.0-beta1版本修复了这个问题,但是经过测试无法实现对null值得替换 建议描述

kaciry0927 avatar Nov 13 '21 04:11 kaciry0927

这个只能在写入的时候使用 无法在读取的时候使用

zhuangjiaju avatar May 11 '22 06:05 zhuangjiaju