Skip to content

使用registerConverter不生效 #170

Closed

Description

@delei

版本

easyexcel-4.0.3

问题描述

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

  • 添加同一类型的,只能成功一个
  • 添加的 CellDataTypeEnum 为 null
  • 报错
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);

Activity

  1. delei commented on Dec 25, 2024

    @delei
    MemberAuthor

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

  2. delei commented on Dec 25, 2024

    @delei
    MemberAuthor

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

  3. added
    help wanted
    pending
    on Dec 25, 2024
  4. added this to the 1.1.0 milestone on Jan 4, 2025
  5. reopened this on Apr 14, 2025
  6. delei commented on Apr 14, 2025

    @delei
    MemberAuthor

    @psxjoy
    在最新的 release 1.2.0 中,我看该 PR 也还未合并是吗?
    In the latest release 1.2.0, this PR has not been merged? If there's anything I can do, please let me know.

  7. psxjoy commented on Apr 14, 2025

    @psxjoy
    Member

    @psxjoy
    在最新的 release 1.2.0 中,我看该 PR 也还未合并是吗?
    In the latest release 1.2.0, this PR has not been merged? If there's anything I can do, please let me know.

    Hi, I added some comments which you have not modified. That made me close that PR.
    Feel free reopen when you think all is fine:)

  8. delei commented on Apr 14, 2025

    @delei
    MemberAuthor

    需要我重新提交PR吗?

  9. psxjoy commented on Apr 14, 2025

    @psxjoy
    Member
  10. delei commented on Apr 14, 2025

    @delei
    MemberAuthor

    @psxjoy 已重新提交PR,该issue在实际使用中,暂时用反射的临时方案处理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help wantedpending

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions