com.alibaba.excel.enums.CellDataTypeEnum Java Examples

The following examples show how to use com.alibaba.excel.enums.CellDataTypeEnum. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CellValueTagHandler.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
@Override
protected void setStringValue(XlsxReadContext xlsxReadContext) {
    // Have to go "sharedStrings.xml" and get it
    CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData();
    switch (tempCellData.getType()) {
        case STRING:
            String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache()
                .get(Integer.valueOf(tempCellData.getStringValue()));
            if (stringValue != null && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
                stringValue = stringValue.trim();
            }
            tempCellData.setStringValue(stringValue);
            break;
        case DIRECT_STRING:
            tempCellData.setType(CellDataTypeEnum.STRING);
            break;
        default:
    }
}
 
Example #2
Source File: CellData.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the object does not appear null
 */
public void checkEmpty() {
    if (type == null) {
        type = CellDataTypeEnum.EMPTY;
    }
    switch (type) {
        case STRING:
        case ERROR:
            if (StringUtils.isEmpty(stringValue)) {
                type = CellDataTypeEnum.EMPTY;
            }
            return;
        case NUMBER:
            if (numberValue == null) {
                type = CellDataTypeEnum.EMPTY;
            }
            return;
        case BOOLEAN:
            if (booleanValue == null) {
                type = CellDataTypeEnum.EMPTY;
            }
            return;
        default:
    }
}
 
Example #3
Source File: LongestMatchColumnWidthStyleStrategy.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) {
    if (isHead) {
        return cell.getStringCellValue().getBytes().length;
    }
    CellData cellData = cellDataList.get(0);
    CellDataTypeEnum type = cellData.getType();
    if (type == null) {
        return -1;
    }
    switch (type) {
        case STRING:
            return cellData.getStringValue().getBytes().length;
        case BOOLEAN:
            return cellData.getBooleanValue().toString().getBytes().length;
        case NUMBER:
            return cellData.getNumberValue().toString().getBytes().length;
        default:
            return -1;
    }
}
 
Example #4
Source File: CellDataDataTest.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
private List<CellDataData> data() throws Exception {
    List<CellDataData> list = new ArrayList<CellDataData>();
    CellDataData cellDataData = new CellDataData();
    cellDataData.setDate(new CellData<Date>(DateUtils.parseDate("2020-01-01 01:01:01")));
    CellData<Integer> integer1 = new CellData<Integer>();
    integer1.setType(CellDataTypeEnum.NUMBER);
    integer1.setNumberValue(BigDecimal.valueOf(2L));
    cellDataData.setInteger1(integer1);
    cellDataData.setInteger2(2);
    CellData formulaValue = new CellData();
    formulaValue.setFormula(Boolean.TRUE);
    formulaValue.setFormulaValue("B2+C2");
    cellDataData.setFormulaValue(formulaValue);
    list.add(cellDataData);
    return list;
}
 
Example #5
Source File: CellData.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
public CellData(BigDecimal numberValue) {
    if (numberValue == null) {
        throw new IllegalArgumentException("DoubleValue can not be null");
    }
    this.type = CellDataTypeEnum.NUMBER;
    this.numberValue = numberValue;
    this.formula = Boolean.FALSE;
}
 
Example #6
Source File: ModelBuildEventListener.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private Object buildUserModel(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder,
    AnalysisContext context) {
    ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty();
    Object resultModel;
    try {
        resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
    } catch (Exception e) {
        throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0,
            new CellData(CellDataTypeEnum.EMPTY), null,
            "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
    }
    Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
    Map<String, Object> map = new HashMap<String, Object>(headMap.size() * 4 / 3 + 1);
    Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Integer index = entry.getKey();
        if (!cellDataMap.containsKey(index)) {
            continue;
        }
        CellData cellData = cellDataMap.get(index);
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            continue;
        }
        ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
        Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(),
            excelContentProperty, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration(),
            context.readRowHolder().getRowIndex(), index);
        if (value != null) {
            map.put(excelContentProperty.getField().getName(), value);
        }
    }
    BeanMap.create(resultModel).putAll(map);
    return resultModel;
}
 
Example #7
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) {
    if (value == null) {
        return new CellData(CellDataTypeEnum.EMPTY);
    }
    if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
        value = ((String)value).trim();
    }
    CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty);
    if (cellData.getFormula() != null && cellData.getFormula()) {
        cell.setCellFormula(cellData.getFormulaValue());
    }
    if (cellData.getType() == null) {
        cellData.setType(CellDataTypeEnum.EMPTY);
    }
    WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
    switch (cellData.getType()) {
        case STRING:
            cell.setCellValue(cellData.getStringValue());
            return cellData;
        case BOOLEAN:
            cell.setCellValue(cellData.getBooleanValue());
            return cellData;
        case NUMBER:
            cell.setCellValue(cellData.getNumberValue().doubleValue());
            return cellData;
        case IMAGE:
            setImageValue(cellData, cell);
            return cellData;
        case EMPTY:
            return cellData;
        default:
            throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData,
                excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType()
                    + "at row:" + cell.getRow().getRowNum());
    }
}
 
Example #8
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty) {
    if (value == null) {
        return new CellData(CellDataTypeEnum.EMPTY);
    }
    // This means that the user has defined the data.
    if (value instanceof CellData) {
        CellData cellDataValue = (CellData)value;
        if (cellDataValue.getType() != null) {
            return cellDataValue;
        } else {
            if (cellDataValue.getData() == null) {
                cellDataValue.setType(CellDataTypeEnum.EMPTY);
                return cellDataValue;
            }
        }
        CellData cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), cell,
            cellDataValue.getData(), excelContentProperty);
        // The formula information is subject to user input
        if (cellDataValue.getFormula() != null) {
            cellDataReturn.setFormula(cellDataValue.getFormula());
            cellDataReturn.setFormulaValue(cellDataValue.getFormulaValue());
        }
        return cellDataReturn;
    }
    return doConvert(currentWriteHolder, clazz, cell, value, excelContentProperty);
}
 
Example #9
Source File: CellData.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
public CellData(CellDataTypeEnum type, String stringValue) {
    if (type != CellDataTypeEnum.STRING && type != CellDataTypeEnum.ERROR) {
        throw new IllegalArgumentException("Only support CellDataTypeEnum.STRING and  CellDataTypeEnum.ERROR");
    }
    if (stringValue == null) {
        throw new IllegalArgumentException("StringValue can not be null");
    }
    this.type = type;
    this.stringValue = stringValue;
    this.formula = Boolean.FALSE;
}
 
Example #10
Source File: CellData.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
public CellData(byte[] imageValue) {
    if (imageValue == null) {
        throw new IllegalArgumentException("ImageValue can not be null");
    }
    this.type = CellDataTypeEnum.IMAGE;
    this.imageValue = imageValue;
    this.formula = Boolean.FALSE;
}
 
Example #11
Source File: CellData.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
public CellData(Boolean booleanValue) {
    if (booleanValue == null) {
        throw new IllegalArgumentException("BooleanValue can not be null");
    }
    this.type = CellDataTypeEnum.BOOLEAN;
    this.booleanValue = booleanValue;
    this.formula = Boolean.FALSE;
}
 
Example #12
Source File: CellData.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
public CellData(CellDataTypeEnum type) {
    if (type == null) {
        throw new IllegalArgumentException("Type can not be null");
    }
    this.type = type;
    this.formula = Boolean.FALSE;
}
 
Example #13
Source File: AbstractCellValueTagHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    CellData tempCellData = xlsxReadSheetHolder.getTempCellData();
    StringBuilder tempData = xlsxReadSheetHolder.getTempData();
    CellDataTypeEnum oldType = tempCellData.getType();
    switch (oldType) {
        case DIRECT_STRING:
        case STRING:
        case ERROR:
            tempCellData.setStringValue(tempData.toString());
            break;
        case BOOLEAN:
            tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString()));
            break;
        case NUMBER:
        case EMPTY:
            tempCellData.setType(CellDataTypeEnum.NUMBER);
            tempCellData.setNumberValue(new BigDecimal(tempData.toString()));
            break;
        default:
            throw new IllegalStateException("Cannot set values now");
    }

    // set string value
    setStringValue(xlsxReadContext);

    if (tempCellData.getStringValue() != null
        && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
        tempCellData.setStringValue(tempCellData.getStringValue());
    }

    tempCellData.checkEmpty();
    xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData);
}
 
Example #14
Source File: CellTagHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    xlsxReadSheetHolder.setColumnIndex(PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_R),
        xlsxReadSheetHolder.getColumnIndex()));

    // t="s" ,it's means String
    // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
    // t="inlineStr" ,it's means String
    // t="b" ,it's means Boolean
    // t="e" ,it's means Error
    // t="n" ,it's means Number
    // t is null ,it's means Empty or Number
    CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_T));
    xlsxReadSheetHolder.setTempCellData(new CellData(type));
    xlsxReadSheetHolder.setTempData(new StringBuilder());

    // Put in data transformation information
    String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S);
    Integer dateFormatIndexInteger;
    if (StringUtils.isEmpty(dateFormatIndex)) {
        dateFormatIndexInteger = DEFAULT_FORMAT_INDEX;
    } else {
        dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
    }
    XSSFCellStyle xssfCellStyle =
        xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger);
    int dataFormat = xssfCellStyle.getDataFormat();
    xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat);
    xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
        xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
}
 
Example #15
Source File: ConverterUtils.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * Convert it into a String map
 *
 * @param cellDataMap
 * @param context
 * @return
 */
public static Map<Integer, String> convertToStringMap(Map<Integer, CellData> cellDataMap, AnalysisContext context) {
    Map<Integer, String> stringMap = new HashMap<Integer, String>(cellDataMap.size() * 4 / 3 + 1);
    ReadHolder currentReadHolder = context.currentReadHolder();
    int index = 0;
    for (Map.Entry<Integer, CellData> entry : cellDataMap.entrySet()) {
        Integer key = entry.getKey();
        CellData cellData = entry.getValue();
        while (index < key) {
            stringMap.put(index, null);
            index++;
        }
        index++;
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            stringMap.put(key, null);
            continue;
        }
        Converter converter =
            currentReadHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType()));
        if (converter == null) {
            throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), key, cellData, null,
                "Converter not found, convert " + cellData.getType() + " to String");
        }
        try {
            stringMap.put(key,
                (String)(converter.convertToJavaData(cellData, null, currentReadHolder.globalConfiguration())));
        } catch (Exception e) {
            throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), key, cellData, null,
                "Convert data " + cellData + " to String error ", e);
        }
    }
    return stringMap;
}
 
Example #16
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private CellData doConvert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty) {
    Converter converter = null;
    if (excelContentProperty != null) {
        converter = excelContentProperty.getConverter();
    }
    if (converter == null) {
        converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
    }
    if (converter == null) {
        throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
            new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
            "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
    }
    CellData cellData;
    try {
        cellData =
            converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration());
    } catch (Exception e) {
        throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
            new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
            "Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(), e);
    }
    if (cellData == null || cellData.getType() == null) {
        throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
            new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
            "Convert data:" + value + " return null,at row:" + cell.getRow().getRowNum());
    }
    return cellData;
}
 
Example #17
Source File: ConverterKeyBuild.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public static String buildKey(Class clazz, CellDataTypeEnum cellDataTypeEnum) {
    return buildKey(clazz) + "-" + cellDataTypeEnum.toString();
}
 
Example #18
Source File: UrlImageConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.IMAGE;
}
 
Example #19
Source File: BooleanNumberConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.NUMBER;
}
 
Example #20
Source File: BooleanBooleanConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.BOOLEAN;
}
 
Example #21
Source File: ShortStringConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
}
 
Example #22
Source File: ShortBooleanConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.BOOLEAN;
}
 
Example #23
Source File: BooleanStringConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
}
 
Example #24
Source File: ShortNumberConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.NUMBER;
}
 
Example #25
Source File: DateStringConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
}
 
Example #26
Source File: DateNumberConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.NUMBER;
}
 
Example #27
Source File: ByteArrayImageConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.IMAGE;
}
 
Example #28
Source File: ByteNumberConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.NUMBER;
}
 
Example #29
Source File: BoxingByteArrayImageConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.IMAGE;
}
 
Example #30
Source File: InputStreamImageConverter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.IMAGE;
}