"Convert data com.alibaba.excel.metadata.data.ReadCellData@ca896aaa to class java.time.LocalDateTime error ",
package com.csbaic.edatope.app.excel;
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 org.apache.poi.ss.usermodel.DateUtil;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class LocalDateTimeConverter implements Converter<LocalDateTime> {
@Override
public Class<LocalDateTime> supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
if (null == cellData) {
return null;
}
LocalDateTime result = null;
if (cellData.getType() == CellDataTypeEnum.NUMBER) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
Date date = DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing(), null);
result = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
} else {
Date date = DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);
result = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
if (cellData.getType() == CellDataTypeEnum.STRING) {
String value = cellData.getStringValue();
if (value.contains("-")) {
try {
result = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
/* result= LocalDateTime.parse(cellData.getStringValue());*/
} catch (Exception e) {
e.printStackTrace();
}
} else if (value.contains("/")) {
try {
result = LocalDateTime.parse(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(value));
} catch (Exception e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
@ExcelProperty(value = "填表日期",converter = LocalDateTimeConverter.class)
private LocalDateTime recordingDate;
- @ExcelProperty(value = “填表日期”)的value字段需要与excel中表头文字一致