yanghb
2024-12-17 1287337fd0b0c156ec79712f9a600ebeffefe3a6
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.zzg.system.convert.easyExcel;
 
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.zzg.common.exception.GlobalException;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.format.annotation.DateTimeFormat;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
import java.util.TimeZone;
 
public class DateConverter implements Converter<Date> {
 
 
    @Override
    public Class<Date> supportJavaTypeKey() {
        return Date.class;
    }
 
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }
 
    /**
     * 这里读的时候会调用
     *
     * @param cellData            excel数据 (NotNull)
     * @param contentProperty     excel属性 (Nullable)
     * @param globalConfiguration 全局配置 (NotNull)
     * @return 读取到内存中的数据
     */
    @Override
    public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        try {
            if (cellData.getType() == CellDataTypeEnum.NUMBER) {
                // Excel 日期是以数字格式存储的,转换为 Java Date
                return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue());
            }
            DateTimeFormat annotation = contentProperty.getField().getAnnotation(DateTimeFormat.class);
            String format = Objects.nonNull(annotation) ? annotation.pattern() : "yyyy/m/d";
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
            sdf.setTimeZone(TimeZone.getDefault());
            return sdf.parse(cellData.getStringValue());
        } catch (Exception e) {
            throw new GlobalException("日期格式有问题");
        }
    }
 
    /**
     * 写的时候会调用
     *
     * @param value               java value (NotNull)
     * @param contentProperty     excel属性 (Nullable)
     * @param globalConfiguration 全局配置 (NotNull)
     * @return 写出到excel文件的数据
     */
    @Override
    public WriteCellData<Date> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        DateTimeFormat annotation = contentProperty.getField().getAnnotation(DateTimeFormat.class);
        String format = Objects.nonNull(annotation) ? annotation.pattern() : "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.getDefault());
        sdf.setTimeZone(TimeZone.getDefault());
        String result = sdf.format(value);
        return new WriteCellData<>(result);
    }
}