|
@@ -0,0 +1,57 @@
|
|
|
+package com.citu.framework.excel.core.convert;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+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.alibaba.excel.util.DateUtils;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeParseException;
|
|
|
+
|
|
|
+import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+
|
|
|
+public class LocalDateTimeStringConverter implements Converter<LocalDateTime> {
|
|
|
+ @Override
|
|
|
+ public Class<?> supportJavaTypeKey() {
|
|
|
+ return LocalDateTime.class;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CellDataTypeEnum supportExcelTypeKey() {
|
|
|
+ return CellDataTypeEnum.STRING;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
|
|
|
+ GlobalConfiguration globalConfiguration) throws ParseException {
|
|
|
+ try {
|
|
|
+ if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
|
|
+
|
|
|
+ return DateUtils.parseLocalDateTime(cellData.getStringValue(), null, globalConfiguration.getLocale());
|
|
|
+ } else {
|
|
|
+ return DateUtils.parseLocalDateTime(cellData.getStringValue(),
|
|
|
+ contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale());
|
|
|
+ }
|
|
|
+ }catch (DateTimeParseException e){
|
|
|
+ throw new RuntimeException("时间序列化错误,请检查时间格式 是否为 yyyy-MM-dd HH:mm:ss 例如: 1995-01-01 11:11:00!",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
|
|
|
+ GlobalConfiguration globalConfiguration) {
|
|
|
+ if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
|
|
+ return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale()));
|
|
|
+ } else {
|
|
|
+ return new WriteCellData<>(
|
|
|
+ DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat(),
|
|
|
+ globalConfiguration.getLocale()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|