Java源码示例:com.alibaba.excel.metadata.property.ExcelContentProperty

示例1
@Override
public Enum convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    String cellDataStr = cellData.getStringValue();

    EnumFormat annotation = contentProperty.getField().getAnnotation(EnumFormat.class);
    Class enumClazz = annotation.value();
    String[] fromExcel = annotation.fromExcel();
    String[] toJavaEnum = annotation.toJavaEnum();

    Enum anEnum = null;
    if (ArrayUtils.isNotEmpty(fromExcel) && ArrayUtils.isNotEmpty(toJavaEnum)) {
        Assert.isTrue(fromExcel.length == toJavaEnum.length, "fromExcel 与 toJavaEnum 的长度必须相同");
        for (int i = 0; i < fromExcel.length; i++) {
            if (Objects.equals(fromExcel[i], cellDataStr)) {
                anEnum = EnumUtils.getEnum(enumClazz, toJavaEnum[i]);
            }
        }
    } else {
        anEnum = EnumUtils.getEnum(enumClazz, cellDataStr);
    }

    Assert.notNull(anEnum, "枚举值不合法");
    return anEnum;
}
 
示例2
@Override
public CellData convertToExcelData(Enum value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {

    String enumName = value.name();

    EnumFormat annotation = contentProperty.getField().getAnnotation(EnumFormat.class);
    String[] fromExcel = annotation.fromExcel();
    String[] toJavaEnum = annotation.toJavaEnum();

    if (ArrayUtils.isNotEmpty(fromExcel) && ArrayUtils.isNotEmpty(toJavaEnum)) {
        Assert.isTrue(fromExcel.length == toJavaEnum.length, "fromExcel 与 toJavaEnum 的长度必须相同");
        for (int i = 0; i < toJavaEnum.length; i++) {
            if (Objects.equals(toJavaEnum[i], enumName)) {
                return new CellData(fromExcel[i]);
            }
        }
    }
    return new CellData(enumName);
}
 
示例3
/**
 *
 * @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);
    }
}
 
示例4
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    // If there are "DateTimeFormat", read as date
    if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
        return DateUtils.format(
            DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
                contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
            contentProperty.getDateTimeFormatProperty().getFormat());
    }
    // If there are "NumberFormat", read as number
    if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
        return NumberUtils.format(cellData.getNumberValue(), contentProperty);
    }
    // Excel defines formatting
    if (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {
        return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(),
            cellData.getDataFormatString(), globalConfiguration);
    }
    // Default conversion number
    return NumberUtils.format(cellData.getNumberValue(), contentProperty);
}
 
示例5
@Override
public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (value) {
        return new CellData(BigDecimal.ONE);
    }
    return new CellData(BigDecimal.ZERO);
}
 
示例6
@Override
public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return ONE;
    }
    return ZERO;
}
 
示例7
@Override
public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
        return new CellData(DateUtils.format(value, null));
    } else {
        return new CellData(DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat()));
    }
}
 
示例8
@Override
public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
        return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
            globalConfiguration.getUse1904windowing(), null);
    } else {
        return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
            contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);
    }
}
 
示例9
@Override
public CellData convertToExcelData(URL value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) throws IOException {
    InputStream inputStream = null;
    try {
        inputStream = value.openStream();
        byte[] bytes = IoUtils.toByteArray(inputStream);
        return new CellData(bytes);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
 
示例10
@Override
public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    if (integer==0){
        return new CellData(ENABLE);
    }else {
        return new CellData(NOT_ENABLE);
    }
}
 
示例11
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String stringValue = cellData.getStringValue();
    if (DELETE.equals(stringValue)){
        return 1;
    }else {
        return 0;
    }
}
 
示例12
@Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
示例13
@Override
public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    if (integer==0){
        return new CellData(ENABLE);
    }else {
        return new CellData(NOT_ENABLE);
    }
}
 
示例14
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String stringValue = cellData.getStringValue();
    if (DELETE.equals(stringValue)){
        return 1;
    }else {
        return 0;
    }
}
 
示例15
@Override
public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    if (integer==0){
        return new CellData(NOT_DELETE);
    }else {
        return new CellData(DELETE);
    }
}
 
示例16
@Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (BigDecimal.ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
示例17
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String stringValue = cellData.getStringValue();
    if (DELETE.equals(stringValue)){
        return 1;
    }else {
        return 0;
    }
}
 
示例18
@Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
示例19
@Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return BigDecimal.ONE;
    }
    return BigDecimal.ZERO;
}
 
示例20
/**
 * parse
 *
 * @param string
 * @param contentProperty
 * @return
 */
public static BigDecimal parseBigDecimal(String string, ExcelContentProperty contentProperty)
    throws ParseException {
    if (!hasFormat(contentProperty)) {
        return new BigDecimal(string);
    }
    return BigDecimal.valueOf(parse(string, contentProperty).doubleValue());
}
 
示例21
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;
}
 
示例22
/**
 * parse
 *
 * @param string
 * @param contentProperty
 * @return
 * @throws ParseException
 */
private static Number parse(String string, ExcelContentProperty contentProperty) throws ParseException {
    String format = contentProperty.getNumberFormatProperty().getFormat();
    RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
    DecimalFormat decimalFormat = new DecimalFormat(format);
    decimalFormat.setRoundingMode(roundingMode);
    decimalFormat.setParseBigDecimal(true);
    return decimalFormat.parse(string);
}
 
示例23
/**
 * Convert it into a Java object
 *
 * @param cellData
 * @param field
 * @param contentProperty
 * @param converterMap
 * @param globalConfiguration
 * @param rowIndex
 * @param columnIndex
 * @return
 */
public static Object convertToJavaObject(CellData cellData, Field field, ExcelContentProperty contentProperty,
    Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration, Integer rowIndex,
    Integer columnIndex) {
    Class clazz;
    if (field == null) {
        clazz = String.class;
    } else {
        clazz = field.getType();
    }
    if (clazz == CellData.class) {
        Type type = field.getGenericType();
        Class classGeneric;
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType)type;
            classGeneric = (Class)parameterizedType.getActualTypeArguments()[0];
        } else {
            classGeneric = String.class;
        }
        CellData cellDataReturn = new CellData(cellData);
        cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap,
            globalConfiguration, rowIndex, columnIndex));
        return cellDataReturn;
    }
    return doConvertToJavaObject(cellData, clazz, contentProperty, converterMap, globalConfiguration, rowIndex,
        columnIndex);
}
 
示例24
@Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return ONE;
    }
    return ZERO;
}
 
示例25
@Override
public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
        return new CellData(
            BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing())));
    } else {
        return new CellData(BigDecimal.valueOf(
            DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
    }
}
 
示例26
@Override
public CellData convertToExcelData(Byte[] value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    byte[] byteValue = new byte[value.length];
    for (int i = 0; i < value.length; i++) {
        byteValue[i] = value[i];
    }
    return new CellData(byteValue);
}
 
示例27
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());
    }
}
 
示例28
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return ONE;
    }
    return ZERO;
}
 
示例29
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
示例30
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;
}