Search in sources :

Example 1 with NullableObjectConverter

use of com.alibaba.excel.converters.NullableObjectConverter in project easyexcel by alibaba.

the class AbstractExcelWriteExecutor method doConvert.

private WriteCellData<?> doConvert(CellWriteHandlerContext cellWriteHandlerContext) {
    ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty();
    Converter<?> converter = null;
    if (excelContentProperty != null) {
        converter = excelContentProperty.getConverter();
    }
    if (converter == null) {
        // csv is converted to string by default
        if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) {
            cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
        }
        converter = writeContext.currentWriteHolder().converterMap().get(ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(), cellWriteHandlerContext.getTargetCellDataType()));
    }
    if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) {
        return new WriteCellData<>(CellDataTypeEnum.EMPTY);
    }
    if (converter == null) {
        throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass().getSimpleName() + ".");
    }
    WriteCellData<?> cellData;
    try {
        cellData = ((Converter<Object>) converter).convertToExcelData(new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty, writeContext));
    } catch (Exception e) {
        throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:" + cellWriteHandlerContext.getRowIndex(), e);
    }
    if (cellData == null || cellData.getType() == null) {
        throw new ExcelWriteDataConvertException(cellWriteHandlerContext, "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return is null or return type is null, at row:" + cellWriteHandlerContext.getRowIndex());
    }
    return cellData;
}
Also used : ExcelWriteDataConvertException(com.alibaba.excel.exception.ExcelWriteDataConvertException) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) WriteConverterContext(com.alibaba.excel.converters.WriteConverterContext) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) NullableObjectConverter(com.alibaba.excel.converters.NullableObjectConverter) ExcelWriteDataConvertException(com.alibaba.excel.exception.ExcelWriteDataConvertException)

Example 2 with NullableObjectConverter

use of com.alibaba.excel.converters.NullableObjectConverter in project easyexcel by alibaba.

the class ConverterUtils method doConvertToJavaObject.

/**
 * @param cellData
 * @param clazz
 * @param contentProperty
 * @param converterMap
 * @param context
 * @param rowIndex
 * @param columnIndex
 * @return
 */
private static Object doConvertToJavaObject(ReadCellData<?> cellData, Class<?> clazz, ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) {
    Converter<?> converter = null;
    if (contentProperty != null) {
        converter = contentProperty.getConverter();
    }
    boolean canNotConverterEmpty = cellData.getType() == CellDataTypeEnum.EMPTY && !(converter instanceof NullableObjectConverter);
    if (canNotConverterEmpty) {
        return null;
    }
    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(new ReadConverterContext<>(cellData, contentProperty, context));
    } catch (Exception e) {
        throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty, "Convert data " + cellData + " to " + clazz + " error ", e);
    }
}
Also used : ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) NullableObjectConverter(com.alibaba.excel.converters.NullableObjectConverter) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException)

Aggregations

NullableObjectConverter (com.alibaba.excel.converters.NullableObjectConverter)2 WriteConverterContext (com.alibaba.excel.converters.WriteConverterContext)1 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)1 ExcelWriteDataConvertException (com.alibaba.excel.exception.ExcelWriteDataConvertException)1 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1