Search in sources :

Example 6 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData 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 7 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project RuoYi-Vue-Plus by JavaLionLi.

the class ExcelDictConvert method convertToExcelData.

@Override
public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    if (ObjectUtil.isNull(object)) {
        return new WriteCellData<>("");
    }
    ExcelDictFormat anno = getAnnotation(contentProperty.getField());
    String type = anno.dictType();
    String value = Convert.toStr(object);
    String label;
    if (StringUtils.isBlank(type)) {
        label = ExcelUtil.convertByExp(value, anno.readConverterExp(), anno.separator());
    } else {
        label = SpringUtils.getBean(DictService.class).getDictLabel(type, value, anno.separator());
    }
    return new WriteCellData<>(label);
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) ExcelDictFormat(com.ruoyi.common.annotation.ExcelDictFormat)

Example 8 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project RuoYi-Cloud-Plus by JavaLionLi.

the class ExcelBigNumberConvert method convertToExcelData.

@Override
public WriteCellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    if (ObjectUtil.isNotNull(object)) {
        String str = Convert.toStr(object);
        if (str.length() > 15) {
            return new WriteCellData<>(str);
        }
    }
    WriteCellData<Object> cellData = new WriteCellData<>(new BigDecimal(object));
    cellData.setType(CellDataTypeEnum.NUMBER);
    return cellData;
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) BigDecimal(java.math.BigDecimal)

Example 9 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project RuoYi-Cloud-Plus by JavaLionLi.

the class ExcelDictConvert method convertToExcelData.

@Override
public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    if (ObjectUtil.isNull(object)) {
        return new WriteCellData<>("");
    }
    ExcelDictFormat anno = getAnnotation(contentProperty.getField());
    String type = anno.dictType();
    String value = Convert.toStr(object);
    String label;
    if (StringUtils.isBlank(type)) {
        label = ExcelUtil.convertByExp(value, anno.readConverterExp(), anno.separator());
    } else {
        label = SpringUtils.getBean(DictService.class).getDictLabel(type, value, anno.separator());
    }
    return new WriteCellData<>(label);
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) ExcelDictFormat(com.ruoyi.common.excel.annotation.ExcelDictFormat)

Example 10 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project RuoYi-Vue-Plus by JavaLionLi.

the class ExcelBigNumberConvert method convertToExcelData.

@Override
public WriteCellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    if (ObjectUtil.isNotNull(object)) {
        String str = Convert.toStr(object);
        if (str.length() > 15) {
            return new WriteCellData<>(str);
        }
    }
    WriteCellData<Object> cellData = new WriteCellData<>(new BigDecimal(object));
    cellData.setType(CellDataTypeEnum.NUMBER);
    return cellData;
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) BigDecimal(java.math.BigDecimal)

Aggregations

WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)14 ArrayList (java.util.ArrayList)4 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)3 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)3 BigDecimal (java.math.BigDecimal)3 Test (org.junit.Test)3 FormulaData (com.alibaba.excel.metadata.data.FormulaData)2 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)2 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)2 ExcelDictFormat (com.ruoyi.common.annotation.ExcelDictFormat)2 InputStream (java.io.InputStream)2 Cell (org.apache.poi.ss.usermodel.Cell)2 WriteContext (com.alibaba.excel.context.WriteContext)1 NullableObjectConverter (com.alibaba.excel.converters.NullableObjectConverter)1 WriteConverterContext (com.alibaba.excel.converters.WriteConverterContext)1 CellDataTypeEnum (com.alibaba.excel.enums.CellDataTypeEnum)1 WriteDirectionEnum (com.alibaba.excel.enums.WriteDirectionEnum)1 WriteTemplateAnalysisCellTypeEnum (com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum)1 ExcelGenerateException (com.alibaba.excel.exception.ExcelGenerateException)1 ExcelWriteDataConvertException (com.alibaba.excel.exception.ExcelWriteDataConvertException)1