Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用registerConverter不生效 #170

Open
delei opened this issue Dec 25, 2024 · 2 comments · May be fixed by #171
Open

使用registerConverter不生效 #170

delei opened this issue Dec 25, 2024 · 2 comments · May be fixed by #171
Assignees
Labels
help wanted Extra attention is needed pending verification This problem needs to be confirmed

Comments

@delei
Copy link

delei commented Dec 25, 2024

版本

easyexcel-4.0.3

问题描述

导出Excel时,使用registerConverter方法全局添加自定义Converter失效。会出现如下问题:

  • 添加同一类型的,只能成功一个
  • 添加的 CellDataTypeEnumnull
  • 报错
image

此时通过debug发现,AbstractExcelWriteExecutor#doConvert方法,参数converterMap中,cellDataTypeEnum null
image

示例

导出时有需要转换java.sql.Timestamp。自定义Converter如下
转换成数字类型

public class TimestampNumberConverter implements Converter<Timestamp> {
    @Override
    public Class<Timestamp> supportJavaTypeKey() {
        return Timestamp.class;
    }

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

    @Override
    public WriteCellData<?> convertToExcelData(Timestamp value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
            return new WriteCellData<>(
                    BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing())));
        } else {
            return new WriteCellData<>(
                    BigDecimal.valueOf(DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
        }
    }
}

转换成字符串类型

public class TimestampStringConverter implements Converter<Timestamp> {
    @Override
    public Class<Timestamp> supportJavaTypeKey() {
        return Timestamp.class;
    }

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

    @Override
    public WriteCellData<?> convertToExcelData(Timestamp value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        WriteCellData<String> cellData = new WriteCellData<>();
        String cellValue;
        if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
            cellValue = DateUtils.format(value.toLocalDateTime(), null, globalConfiguration.getLocale());
        } else {
            cellValue = DateUtils.format(value.toLocalDateTime(), contentProperty.getDateTimeFormatProperty().getFormat(),
                    globalConfiguration.getLocale());
        }
        cellData.setType(CellDataTypeEnum.STRING);
        cellData.setStringValue(cellValue);
        cellData.setData(cellValue);
        return cellData;
    }
}

导出时

EasyExcel.write(path).registerWriteHandler(horizontalCellStyleStrategy())
                .excelType(ExcelTypeEnum.CSV)
                .registerConverter(new TimestampNumberConverter())
                .registerConverter(new TimestampStringConverter())
                .autoCloseStream(Boolean.TRUE)
                .sheet(sheetName)
                .doWrite(dataList);
@delei
Copy link
Author

delei commented Dec 25, 2024

通过debug,初步定位到原因可能是AbstractWriteHolder的构造方法中,调用的buildKey,只是使用了supportJavaTypeKey,没有用到supportExcelTypeKey
image

@delei
Copy link
Author

delei commented Dec 25, 2024

fastexcel中的AbstractWriteHolder也是如此。我将fork一下,使用fastexcel来测试案例。如果可以,将修复并提交Pull request

@psxjoy psxjoy added help wanted Extra attention is needed pending verification This problem needs to be confirmed labels Dec 25, 2024
@delei delei linked a pull request Dec 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed pending verification This problem needs to be confirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants