xiaoyi
5 天以前 481d8165f26fc7e37f0e87f6444ac0340bb563a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cn.iocoder.yudao.framework.excel.core.convert;
 
import cn.idev.excel.converters.Converter;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
 
/**
 * LocalDate 写出为纯文本(yyyy-MM-dd)的转换器
 *
 * <p>默认 FastExcel 把日期写成「日期单元格」,在 Excel 里显示为 46023.0 这类序列号,
 * 使用者看不懂该填什么格式。导入模板类场景注册本转换器后,日期以文本输出,直观可读。</p>
 *
 * <p>注意:仅用于「写出模板」,不要用于导入解析。</p>
 *
 * @author 超级管理员
 */
public class LocalDateStringConverter implements Converter<LocalDate> {
 
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
    @Override
    public Class<LocalDate> supportJavaTypeKey() {
        return LocalDate.class;
    }
 
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }
 
    @Override
    public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new WriteCellData<>(value.format(FORMATTER));
    }
 
}