Repository navigation
Closed
Labels
Milestone
Description
版本
easyexcel-4.0.3
问题描述
导出Excel时,使用registerConverter方法全局添加自定义Converter失效。会出现如下问题:
- 添加同一类型的,只能成功一个
- 添加的
CellDataTypeEnum为null - 报错
此时通过debug发现,AbstractExcelWriteExecutor#doConvert方法,参数converterMap中,cellDataTypeEnum 为null

示例
导出时有需要转换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
delei commentedon Dec 25, 2024
通过

debug,初步定位到原因可能是AbstractWriteHolder的构造方法中,调用的buildKey,只是使用了supportJavaTypeKey,没有用到supportExcelTypeKeydelei commentedon Dec 25, 2024
看
fastexcel中的AbstractWriteHolder也是如此。我将fork一下,使用fastexcel来测试案例。如果可以,将修复并提交Pull requestdelei commentedon Apr 14, 2025
@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.
psxjoy commentedon Apr 14, 2025
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:)
delei commentedon Apr 14, 2025
需要我重新提交
PR吗?psxjoy commentedon Apr 14, 2025
delei commentedon Apr 14, 2025
@psxjoy 已重新提交
PR,该issue在实际使用中,暂时用反射的临时方案处理