We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
easyexcel-4.0.3
导出Excel时,使用registerConverter方法全局添加自定义Converter失效。会出现如下问题:
Excel
registerConverter
Converter
CellDataTypeEnum
null
此时通过debug发现,AbstractExcelWriteExecutor#doConvert方法,参数converterMap中,cellDataTypeEnum 为null
debug
AbstractExcelWriteExecutor#doConvert
converterMap
cellDataTypeEnum
导出时有需要转换java.sql.Timestamp。自定义Converter如下 转换成数字类型
java.sql.Timestamp
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);
The text was updated successfully, but these errors were encountered:
通过debug,初步定位到原因可能是AbstractWriteHolder的构造方法中,调用的buildKey,只是使用了supportJavaTypeKey,没有用到supportExcelTypeKey
AbstractWriteHolder
buildKey
supportJavaTypeKey
supportExcelTypeKey
Sorry, something went wrong.
看fastexcel中的AbstractWriteHolder也是如此。我将fork一下,使用fastexcel来测试案例。如果可以,将修复并提交Pull request
fastexcel
fork
Pull request
delei
Successfully merging a pull request may close this issue.
版本
easyexcel-4.0.3
问题描述
导出
Excel
时,使用registerConverter
方法全局添加自定义Converter
失效。会出现如下问题:CellDataTypeEnum
为null
此时通过
debug
发现,AbstractExcelWriteExecutor#doConvert
方法,参数converterMap
中,cellDataTypeEnum
为null
示例
导出时有需要转换
java.sql.Timestamp
。自定义Converter
如下转换成数字类型
转换成字符串类型
导出时
The text was updated successfully, but these errors were encountered: