gaoluyang
2 天以前 a862c36323b8c6b47f8c41f3860ac93411348c4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cn.iocoder.yudao.module.crm.framework.excel.core;
 
import cn.idev.excel.converters.Converter;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
 
/** 是否流入公海的 Excel 转换;非法值必须报错,避免意外开放公海可见度。 */
public class AutoPoolEnabledConvert implements Converter<Boolean> {
 
    @Override
    public Class<?> supportJavaTypeKey() {
        return Boolean.class;
    }
 
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }
 
    @Override
    public Boolean convertToJavaData(ReadCellData<?> cell, ExcelContentProperty property,
                                     GlobalConfiguration configuration) {
        if (cell.getType() == CellDataTypeEnum.EMPTY) {
            return null;
        }
        if (cell.getType() == CellDataTypeEnum.BOOLEAN) {
            return cell.getBooleanValue();
        }
        String value = cell.getType() == CellDataTypeEnum.NUMBER
                ? cell.getNumberValue().stripTrailingZeros().toPlainString() : cell.getStringValue();
        if (value == null || value.isBlank()) {
            return null;
        }
        return switch (value.trim().toLowerCase(java.util.Locale.ROOT)) {
            case "是", "true", "1" -> true;
            case "否", "false", "0" -> false;
            default -> throw new IllegalArgumentException("是否流入公海请填写是或否");
        };
    }
 
    @Override
    public WriteCellData<String> convertToExcelData(Boolean value, ExcelContentProperty property,
                                                    GlobalConfiguration configuration) {
        return new WriteCellData<>(value == null ? "" : value ? "是" : "否");
    }
}