Showing
3 changed files
with
45 additions
and
5 deletions
1 | +package com.ash.base.excelOpt; | |
2 | + | |
3 | +import com.alibaba.excel.converters.Converter; | |
4 | +import com.alibaba.excel.converters.WriteConverterContext; | |
5 | +import com.alibaba.excel.metadata.GlobalConfiguration; | |
6 | +import com.alibaba.excel.metadata.data.ReadCellData; | |
7 | +import com.alibaba.excel.metadata.data.WriteCellData; | |
8 | +import com.alibaba.excel.metadata.property.ExcelContentProperty; | |
9 | + | |
10 | +import java.text.SimpleDateFormat; | |
11 | +import java.util.Date; | |
12 | + | |
13 | +public class DatetimeConverter2 implements Converter<Date> { | |
14 | + private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd HH:mm:ss"; | |
15 | + | |
16 | + | |
17 | + @Override | |
18 | + public Class<Date> supportJavaTypeKey() { | |
19 | + return Date.class; | |
20 | + } | |
21 | + | |
22 | + | |
23 | + @Override | |
24 | + public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { | |
25 | + String value = cellData.getStringValue(); | |
26 | + SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD); | |
27 | + return sdf.parse(value); | |
28 | + } | |
29 | + | |
30 | + | |
31 | + @Override | |
32 | + public WriteCellData<String> convertToExcelData(WriteConverterContext<Date> context) throws Exception { | |
33 | + Date date = context.getValue(); | |
34 | + if (date == null) { | |
35 | + return null; | |
36 | + } | |
37 | + SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD); | |
38 | + return new WriteCellData<>(sdf.format(date)); | |
39 | + } | |
40 | +} | ... | ... |
... | ... | @@ -5,7 +5,7 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; |
5 | 5 | import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
6 | 6 | import com.alibaba.excel.annotation.write.style.ContentStyle; |
7 | 7 | import com.alibaba.excel.annotation.write.style.HeadRowHeight; |
8 | -import com.ash.base.excelOpt.DatetimeConverter; | |
8 | +import com.ash.base.excelOpt.DatetimeConverter2; | |
9 | 9 | import com.ash.base.excelOpt.ExcelCheck; |
10 | 10 | import lombok.AllArgsConstructor; |
11 | 11 | import lombok.Builder; |
... | ... | @@ -30,8 +30,8 @@ public class WarningInstanceExcelData implements Serializable { |
30 | 30 | @ColumnWidth(25) |
31 | 31 | private String code; |
32 | 32 | |
33 | - @ExcelCheck(canEmpty = false, dateFormatValid = "yyyy/MM/dd h:mm:ss") | |
34 | - @ExcelProperty(value = "报警时间", index = 1) | |
33 | + @ExcelCheck(canEmpty = false) | |
34 | + @ExcelProperty(value = "报警时间", index = 1, converter = DatetimeConverter2.class) | |
35 | 35 | @ContentStyle(dataFormat = 49) |
36 | 36 | @ColumnWidth(25) |
37 | 37 | private Date alarmDate; | ... | ... |
... | ... | @@ -5,7 +5,7 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; |
5 | 5 | import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
6 | 6 | import com.alibaba.excel.annotation.write.style.ContentStyle; |
7 | 7 | import com.alibaba.excel.annotation.write.style.HeadRowHeight; |
8 | -import com.ash.base.excelOpt.DatetimeConverter; | |
8 | +import com.ash.base.excelOpt.DatetimeConverter2; | |
9 | 9 | import com.ash.base.excelOpt.ExcelCheck; |
10 | 10 | import lombok.AllArgsConstructor; |
11 | 11 | import lombok.Builder; |
... | ... | @@ -31,7 +31,7 @@ public class WarningInstanceExportExcelData implements Serializable { |
31 | 31 | private String code; |
32 | 32 | |
33 | 33 | @ExcelCheck(canEmpty = false, dateFormatValid = "yyyy-MM-dd HH:mm:ss") |
34 | - @ExcelProperty(value = "报警时间", index = 1, converter = DatetimeConverter.class) | |
34 | + @ExcelProperty(value = "报警时间", index = 1, converter = DatetimeConverter2.class) | |
35 | 35 | @ContentStyle(dataFormat = 49) |
36 | 36 | @ColumnWidth(25) |
37 | 37 | private Date alarmDate; | ... | ... |