Skip to content

[Bug] 在版本1.3.0中,配置了全局convert但未生效,本地断点也进不去。 #648

Closed

Description

@liuyier

Search before asking

  • I searched in the issues and found nothing similar.

Fesod version

1.3.0

JDK version

17

Operating system

No response

Steps To Reproduce

这个是一个测试demo 视频
Image

因为这个问题领了几个线上问题,哎

Current Behavior

同上

Expected Behavior

断点能进入且全局配置能生效

Anything else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. added
    bug
    on Oct 17, 2025
  2. delei commented on Oct 17, 2025

    @delei
    Member

    Could you please provide the ExcelTest entity class?

  3. liuyier commented on Oct 17, 2025

    @liuyier
    Author

    请问你能提供一下那个ExcelTest实体类吗?

    @DaTa
    @accessors(chain = true)
    @ExcelIgnoreUnannotated
    @ColumnWidth(20)
    @HeadRowHeight(20)
    @ContentRowHeight(15)
    @headstyle(borderBottom = BorderStyleEnum.DOTTED, borderLeft = BorderStyleEnum.DOTTED, borderRight = BorderStyleEnum.DOTTED, borderTop = BorderStyleEnum.DOTTED,fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 9)
    @HeadFontStyle(fontHeightInPoints = 12)
    @ContentStyle(borderBottom = BorderStyleEnum.DOTTED, borderLeft = BorderStyleEnum.DOTTED, borderRight = BorderStyleEnum.DOTTED, borderTop = BorderStyleEnum.DOTTED,horizontalAlignment = HorizontalAlignmentEnum.CENTER)
    @ContentFontStyle(fontHeightInPoints = 11)
    public class ExcelTest {

    @ExcelProperty({"刘燃", "姓名"})
    @NotBlank(message = "姓名不能为空")
    @Encrypt(type = EncryptTypeEnum.NAME)
    private String name;
    
    @ExcelProperty({"刘燃", "年龄"})
    @Encrypt(type = EncryptTypeEnum.CUSTOMER)
    private String age;
    
    @Encrypt(type = EncryptTypeEnum.PHONE)
    private String phone;
    
    @Encrypt(type = EncryptTypeEnum.ID_CARD)
    private String idCard;
    
    @ExcelProperty({"刘燃1", "性别"})
    private String gender;
    
    @ExcelProperty("错误")
    private String YC_ComponentCode;
    
    @ExcelProperty("分数")
    @DecimalMin("100")
    @NumberFormat("#,###.00")
    private BigDecimal score;
    
    @ExcelProperty("数量")
    private BigDecimal number;
    
    @ExcelProperty("日期")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private LocalDateTime date;
    

    }

    很常规的一个实体,

  4. liuyier commented on Oct 17, 2025

    @liuyier
    Author

    @Encrypt
    是我自定义注解,可以删掉。

  5. delei commented on Oct 17, 2025

    @delei
    Member

    Please give it a try using

    @ExcelProperty(value = "数量",converter = BigDecimalFormatByConfigConverter.class)
    private BigDecimal number;
  6. liuyier commented on Oct 17, 2025

    @liuyier
    Author

    Please give it a try using

    @ExcelProperty(value = "数量",converter = BigDecimalFormatByConfigConverter.class)
    private BigDecimal number;

    这样子可以进入断点并生效。
    但其实我的目的是加入到全局配置中,而不是单个字段加convert属性,那太麻烦了,也不现实。

  7. delei commented on Oct 17, 2025

    @delei
    Member

    Please try to modify the custom Converter, override the supportExcelTypeKey method and return null.

    public class BigDecimalFormatByConfigConverter extends BigDecimalStringConverter {
    
        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return null;
        }
        
        // ...
    }

    ExcelTest entity class

    @ExcelProperty(value = "数量")
    private BigDecimal number;

    FYI, based on my understanding, this may affect all BigDecimal-type properties. Please thoroughly test according to your actual use case.

  8. added
    question
    and removed
    bug
    on Oct 17, 2025
  9. liuyier commented on Oct 20, 2025

    @liuyier
    Author

    请尝试修改这个自定义转换器,重写 supportExcelTypeKey 方法并返回 null。

    public class BigDecimalFormatByConfigConverter extends BigDecimalStringConverter {

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

    }
    ExcelTest 实体类

    @ExcelProperty(value = "数量")
    private BigDecimal number;
    仅供参考,据我了解,这可能会影响到所有使用 BigDecimal 类型的属性。请根据您的实际使用场景进行彻底的测试。

    我刚测试了下,重写supportExcelTypeKey方法后,确实是生效了。

    这是什么原因,我在1.2.0版本上没有重写这个方法可以正常使用。
    但升到了1.3.0还需要重写supportExcelTypeKey方法并返回 return null ?啊这。。

  10. delei commented on Oct 20, 2025

    @delei
    Member

    这是什么原因,我在1.2.0版本上没有重写这个方法可以正常使用。 但升到了1.3.0还需要重写supportExcelTypeKey方法并返回 return null ?啊这。。

    Please refre to issue #170 and PR #322

  11. liuyier commented on Oct 21, 2025

    @liuyier
    Author

    没太明白, 我还是退回1.2.0好了。

  12. bengbengbalabalabeng commented on Oct 30, 2025

    @bengbengbalabalabeng
    Contributor

    问题触发条件

    1. 用户通过 registerConverter() 注册了一个或多个全局自定义的 Converter<?>。
    2. 在需要转换的实体类字段上,未使用 @ExcelProperty(converter = ...) 注解显式指定转换器。
    3. 写入的目标文件格式不是 CSV (ExcelTypeEnum.CSV)。

    初步分析

    问题的根源在于 AbstractExcelWriteExecutor#doConvert 方法中获取全局转换器的逻辑。

    cn.idev.excel.write.metadata.holder.AbstractWriteHolder

    Code
    public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder) {
        // ......
    
        // Set converterMap
        if (parentAbstractWriteHolder == null) {
            setConverterMap(DefaultConverterLoader.loadDefaultWriteConverter());
        } else {
            setConverterMap(new HashMap<>(parentAbstractWriteHolder.getConverterMap()));
        }
        if (writeBasicParameter.getCustomConverterList() != null
                && !writeBasicParameter.getCustomConverterList().isEmpty()) {
            for (Converter<?> converter : writeBasicParameter.getCustomConverterList()) {
                getConverterMap()
                        .put(
                                ConverterKeyBuild.buildKey(
                                        //                                  [Mark-1]
                                        converter.supportJavaTypeKey(), converter.supportExcelTypeKey()),
                                converter);
            }
        }
    }

    Note: [Mark-1]

    cn.idev.excel.write.executor.AbstractExcelWriteExecutor#doConvert

    Code
    private WriteCellData<?> doConvert(CellWriteHandlerContext cellWriteHandlerContext) {
        ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty();
    
        Converter<?> converter = null;
    
        // get converter by @ExcelProperty(converter = ...)
        if (excelContentProperty != null) {
            converter = excelContentProperty.getConverter();
        }
    
        // get converter by Converter Map
        if (converter == null) {
            // csv is converted to string by default
            // [Mark-2]
            if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) {
                cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
            }
            converter = writeContext
                    .currentWriteHolder()
                    .converterMap()
                    .get(ConverterKeyBuild.buildKey(
                            cellWriteHandlerContext.getOriginalFieldClass(),
                            // [Mark-3]
                            cellWriteHandlerContext.getTargetCellDataType()));
        }
        // ......
        if (converter == null) {
            throw new ExcelWriteDataConvertException(
                    cellWriteHandlerContext,
                    "Can not find 'Converter' support class "
                            + cellWriteHandlerContext.getOriginalFieldClass().getSimpleName() + ".");
        }
        // ......
        return cellData;
    }

    Note: [Mark-2]、[Mark-3]

    在 ExcelWriteAddExecutor#add 方法(调用链)中,会先构造一个 CellWriteHandlerContext 对象,但此时并没有对 targetCellDataType 字段进行赋值。随后调用 converterAndSet -> doConvert 时,如果 @ExcelProperty 注解未显式指定 converter,就会进入全局 Converter<?>的获取逻辑。

    然而,当 [Mark-2] 条件不成立时,[Mark-3] 中的 TargetCellDataType 会一直保持为 NULL。这会导致通过 [Mark-1] 添加的 Converter<?> 无法被正确匹配,从而抛出异常。

    讨论

    • 是否需要在 [Mark-1] 添加 excelTypeKey(感觉 CellWriteHandlerContext#targetCellDataType 类型赋值问题不好解)

    • 个人觉得通过 registerConverter() 同时注册多个相同类型的全局 Converter 本身不太合理,应该使用 @ExcelProperty(converter = ...) 显式指定会更好(优先级高)。

  13. reopened this on Oct 30, 2025
  14. delei commented on Oct 30, 2025

    @delei
    Member

    Hi, @bengbengbalabalabeng

    I also believe that this logic needs to be further optimized.

    FYI, based on my understanding, I believe we should continue exploring how to better configure global converters, and should permit a type to have multiple converters.

  15. added
    enhancement
    help wanted
    and removed
    question
    on Oct 30, 2025
  16. bengbengbalabalabeng commented on Oct 30, 2025

    @bengbengbalabalabeng
    Contributor

    Hi, @bengbengbalabalabeng

    I also believe that this logic needs to be further optimized.

    FYI, based on my understanding, I believe we should continue exploring how to better configure global converters, and should permit a type to have multiple converters.

    Such as :

    when the same type has multiple converters, in a NoModel (no @ExcelProperty) setup where several fields of the same type each need different conversion logic, doConvert can just loop through (or use some other approach) to pick out the best-matching converter.

    Thinking about it this way, it actually makes sense.
    My understanding of the source code might be a bit one‑sided, so apologies if I got anything wrong.

  17. JusChui commented on Mar 16, 2026

    @JusChui

    已解决!我在2.0.1-incubating版本仍然遇到这个问题,我编写了文件导出工具类,定义了脱敏注解和org.apache.fesod.sheet.converters.Converter的实现类,通过registerConverter(new XxxxxConverter())注册converter,但还是仅当使用注解@ExcelProperty(value = "xxx", converter = XxxxxConverter.class)时显式指定converter 才能进入自定义处理器,然后重写了处理器,通过实现org.apache.fesod.sheet.write.handler.CellWriteHandler接口重写afterCellDispose方法而非实现org.apache.fesod.sheet.converters.Converter,再通过 FesodSheet.write(response.getOutputStream(), clazz)
    .registerWriteHandler(new XxxxxCellWriteHandler())注册处理器解决问题。

  18. added a commit that references this issue on Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions