Java Code Examples for com.alibaba.excel.metadata.CellData#getType()

The following examples show how to use com.alibaba.excel.metadata.CellData#getType() . 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: ConverterUtils.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param cellData
 * @param clazz
 * @param contentProperty
 * @param converterMap
 * @param globalConfiguration
 * @param rowIndex
 * @param columnIndex
 * @return
 */
private static Object doConvertToJavaObject(CellData cellData, Class clazz, ExcelContentProperty contentProperty,
    Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration, Integer rowIndex,
    Integer columnIndex) {
    Converter converter = null;
    if (contentProperty != null) {
        converter = contentProperty.getConverter();
    }
    if (converter == null) {
        converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType()));
    }
    if (converter == null) {
        throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty,
            "Converter not found, convert " + cellData.getType() + " to " + clazz.getName());
    }
    try {
        return converter.convertToJavaData(cellData, contentProperty, globalConfiguration);
    } catch (Exception e) {
        throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty,
            "Convert data " + cellData + " to " + clazz + " error ", e);
    }
}
 
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: 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 5
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 6
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 7
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 8
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 9
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;
}