|
|
@ -9,9 +9,9 @@ import com.alibaba.excel.metadata.GlobalConfiguration;
|
|
|
|
import com.alibaba.excel.metadata.data.ReadCellData;
|
|
|
|
import com.alibaba.excel.metadata.data.ReadCellData;
|
|
|
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
|
|
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
|
|
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
|
|
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|
|
|
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|
|
|
import org.dromara.common.excel.annotation.ExcelEnumFormat;
|
|
|
|
import org.dromara.common.excel.annotation.ExcelEnumFormat;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
@ -37,14 +37,26 @@ public class ExcelEnumConvert implements Converter<Object> {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
|
|
|
public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
|
|
|
Object codeValue = cellData.getData();
|
|
|
|
cellData.checkEmpty();
|
|
|
|
|
|
|
|
// Excel中填入的是枚举中指定的描述
|
|
|
|
|
|
|
|
Object textValue = switch (cellData.getType()) {
|
|
|
|
|
|
|
|
case STRING, DIRECT_STRING, RICH_TEXT_STRING -> cellData.getStringValue();
|
|
|
|
|
|
|
|
case NUMBER -> cellData.getNumberValue();
|
|
|
|
|
|
|
|
case BOOLEAN -> cellData.getBooleanValue();
|
|
|
|
|
|
|
|
default -> throw new IllegalArgumentException("单元格类型异常!");
|
|
|
|
|
|
|
|
};
|
|
|
|
// 如果是空值
|
|
|
|
// 如果是空值
|
|
|
|
if (ObjectUtil.isNull(codeValue)) {
|
|
|
|
if (ObjectUtil.isNull(textValue)) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Map<Object, String> enumValueMap = beforeConvert(contentProperty);
|
|
|
|
Map<Object, String> enumCodeToTextMap = beforeConvert(contentProperty);
|
|
|
|
String textValue = enumValueMap.get(codeValue);
|
|
|
|
// 从Java输出至Excel是code转text
|
|
|
|
return Convert.convert(contentProperty.getField().getType(), textValue);
|
|
|
|
// 因此从Excel转Java应该将text与code对调
|
|
|
|
|
|
|
|
Map<Object, Object> enumTextToCodeMap = new HashMap<>();
|
|
|
|
|
|
|
|
enumCodeToTextMap.forEach((key, value) -> enumTextToCodeMap.put(value, key));
|
|
|
|
|
|
|
|
// 应该从text -> code中查找
|
|
|
|
|
|
|
|
Object codeValue = enumTextToCodeMap.get(textValue);
|
|
|
|
|
|
|
|
return Convert.convert(contentProperty.getField().getType(), codeValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|