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);
|
}
|
}
|