Search in sources :

Example 1 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class Issue2443Test method parseIntegerTest1.

@Test
public void parseIntegerTest1() throws ParseException {
    String string = "1.00";
    ExcelContentProperty contentProperty = null;
    int Int = NumberUtils.parseInteger(string, contentProperty);
    assertEquals(1, Int);
}
Also used : ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) Test(org.junit.Test)

Example 2 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class AbstractWriteHolder method dealStyle.

private void dealStyle(List<WriteHandler> handlerList) {
    WriteHandler styleStrategy = new AbstractVerticalCellStyleStrategy() {

        @Override
        public int order() {
            return OrderConstant.ANNOTATION_DEFINE_STYLE;
        }

        @Override
        protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
            Head head = context.getHeadData();
            if (head == null) {
                return null;
            }
            return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
        }

        @Override
        protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
            ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
            return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(), excelContentProperty.getContentFontProperty());
        }
    };
    handlerList.add(styleStrategy);
}
Also used : AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) WorkbookWriteHandler(com.alibaba.excel.write.handler.WorkbookWriteHandler) RowWriteHandler(com.alibaba.excel.write.handler.RowWriteHandler) CellWriteHandler(com.alibaba.excel.write.handler.CellWriteHandler) SheetWriteHandler(com.alibaba.excel.write.handler.SheetWriteHandler) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty)

Example 3 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class ExcelWriteFillExecutor method doFill.

private void doFill(List<AnalysisCell> analysisCellList, Object oneRowData, FillConfig fillConfig, Integer relativeRowIndex) {
    if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) {
        return;
    }
    Map dataMap;
    if (oneRowData instanceof Map) {
        dataMap = (Map) oneRowData;
    } else {
        dataMap = BeanMapUtils.create(oneRowData);
    }
    Set<String> dataKeySet = new HashSet<>(dataMap.keySet());
    RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, null, relativeRowIndex, Boolean.FALSE);
    for (AnalysisCell analysisCell : analysisCellList) {
        CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), relativeRowIndex, Boolean.FALSE, ExcelContentProperty.EMPTY);
        if (analysisCell.getOnlyOneVariable()) {
            String variable = analysisCell.getVariableList().get(0);
            if (!dataKeySet.contains(variable)) {
                continue;
            }
            Object value = dataMap.get(variable);
            ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
            cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
            createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext);
            cellWriteHandlerContext.setOriginalValue(value);
            cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
            converterAndSet(cellWriteHandlerContext);
            WriteCellData<?> cellData = cellWriteHandlerContext.getFirstCellData();
            // Restyle
            if (fillConfig.getAutoStyle()) {
                Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag)).map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)).ifPresent(cellData::setOriginCellStyle);
            }
        } else {
            StringBuilder cellValueBuild = new StringBuilder();
            int index = 0;
            List<WriteCellData<?>> cellDataList = new ArrayList<>();
            cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY);
            cellWriteHandlerContext.setIgnoreFillStyle(Boolean.TRUE);
            createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext);
            Cell cell = cellWriteHandlerContext.getCell();
            for (String variable : analysisCell.getVariableList()) {
                cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
                if (!dataKeySet.contains(variable)) {
                    continue;
                }
                Object value = dataMap.get(variable);
                ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
                cellWriteHandlerContext.setOriginalValue(value);
                cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
                cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
                cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
                WriteCellData<?> cellData = convert(cellWriteHandlerContext);
                cellDataList.add(cellData);
                CellDataTypeEnum type = cellData.getType();
                if (type != null) {
                    switch(type) {
                        case STRING:
                            cellValueBuild.append(cellData.getStringValue());
                            break;
                        case BOOLEAN:
                            cellValueBuild.append(cellData.getBooleanValue());
                            break;
                        case NUMBER:
                            cellValueBuild.append(cellData.getNumberValue());
                            break;
                        default:
                            break;
                    }
                }
            }
            cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
            cell.setCellValue(cellValueBuild.toString());
            cellWriteHandlerContext.setCellDataList(cellDataList);
            if (CollectionUtils.isNotEmpty(cellDataList)) {
                cellWriteHandlerContext.setFirstCellData(cellDataList.get(0));
            }
            // Restyle
            if (fillConfig.getAutoStyle()) {
                Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag)).map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)).ifPresent(cell::setCellStyle);
            }
        }
        WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
    }
    // In the case of the fill line may be called many times
    if (rowWriteHandlerContext.getRow() != null) {
        WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) Setter(lombok.Setter) CellDataTypeEnum(com.alibaba.excel.enums.CellDataTypeEnum) Getter(lombok.Getter) BeanMapUtils(com.alibaba.excel.util.BeanMapUtils) ClassUtils(com.alibaba.excel.util.ClassUtils) HashMap(java.util.HashMap) WriteDirectionEnum(com.alibaba.excel.enums.WriteDirectionEnum) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) HashSet(java.util.HashSet) ListUtils(com.alibaba.excel.util.ListUtils) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) WriteTemplateAnalysisCellTypeEnum(com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) StringUtils(com.alibaba.excel.util.StringUtils) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell) FillWrapper(com.alibaba.excel.write.metadata.fill.FillWrapper) Sheet(org.apache.poi.ss.usermodel.Sheet) Iterator(java.util.Iterator) WriteHandlerUtils(com.alibaba.excel.util.WriteHandlerUtils) Collection(java.util.Collection) Set(java.util.Set) EqualsAndHashCode(lombok.EqualsAndHashCode) Objects(java.util.Objects) List(java.util.List) WriteContext(com.alibaba.excel.context.WriteContext) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException) FieldUtils(com.alibaba.excel.util.FieldUtils) MapUtils(com.alibaba.excel.util.MapUtils) RowWriteHandlerContext(com.alibaba.excel.write.handler.context.RowWriteHandlerContext) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) PoiUtils(org.apache.poi.hssf.usermodel.PoiUtils) Optional(java.util.Optional) Row(org.apache.poi.ss.usermodel.Row) AllArgsConstructor(lombok.AllArgsConstructor) CellStyle(org.apache.poi.ss.usermodel.CellStyle) WriteSheetHolder(com.alibaba.excel.write.metadata.holder.WriteSheetHolder) CellDataTypeEnum(com.alibaba.excel.enums.CellDataTypeEnum) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) ArrayList(java.util.ArrayList) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) RowWriteHandlerContext(com.alibaba.excel.write.handler.context.RowWriteHandlerContext) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) HashMap(java.util.HashMap) Map(java.util.Map) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) Cell(org.apache.poi.ss.usermodel.Cell) HashSet(java.util.HashSet)

Example 4 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty 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 5 with ExcelContentProperty

use of com.alibaba.excel.metadata.property.ExcelContentProperty in project easyexcel by alibaba.

the class ClassUtils method getExcelContentProperty.

private static ExcelContentProperty getExcelContentProperty(Class<?> clazz, Class<?> headClass, String fieldName) {
    return CONTENT_CACHE.computeIfAbsent(buildKey(clazz, headClass, fieldName), key -> {
        ExcelContentProperty excelContentProperty = Optional.ofNullable(declaredFieldContentMap(clazz)).map(map -> map.get(fieldName)).orElse(null);
        ExcelContentProperty headExcelContentProperty = Optional.ofNullable(declaredFieldContentMap(headClass)).map(map -> map.get(fieldName)).orElse(null);
        ExcelContentProperty combineExcelContentProperty = new ExcelContentProperty();
        combineExcelContentProperty(combineExcelContentProperty, headExcelContentProperty);
        if (clazz != headClass) {
            combineExcelContentProperty(combineExcelContentProperty, excelContentProperty);
        }
        return combineExcelContentProperty;
    });
}
Also used : BeanMap(org.springframework.cglib.beans.BeanMap) Setter(lombok.Setter) Getter(lombok.Getter) DateTimeFormatProperty(com.alibaba.excel.metadata.property.DateTimeFormatProperty) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) HashSet(java.util.HashSet) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException) Map(java.util.Map) Converter(com.alibaba.excel.converters.Converter) ExcelIgnore(com.alibaba.excel.annotation.ExcelIgnore) FontProperty(com.alibaba.excel.metadata.property.FontProperty) NumberFormat(com.alibaba.excel.annotation.format.NumberFormat) LinkedHashSet(java.util.LinkedHashSet) DateTimeFormat(com.alibaba.excel.annotation.format.DateTimeFormat) ContentFontStyle(com.alibaba.excel.annotation.write.style.ContentFontStyle) NumberFormatProperty(com.alibaba.excel.metadata.property.NumberFormatProperty) WriteHolder(com.alibaba.excel.write.metadata.holder.WriteHolder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Field(java.lang.reflect.Field) EqualsAndHashCode(lombok.EqualsAndHashCode) StyleProperty(com.alibaba.excel.metadata.property.StyleProperty) ExcelIgnoreUnannotated(com.alibaba.excel.annotation.ExcelIgnoreUnannotated) List(java.util.List) Holder(com.alibaba.excel.metadata.Holder) TreeMap(java.util.TreeMap) Modifier(java.lang.reflect.Modifier) ContentStyle(com.alibaba.excel.annotation.write.style.ContentStyle) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) AutoConverter(com.alibaba.excel.converters.AutoConverter) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty)

Aggregations

ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)10 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)5 Map (java.util.Map)4 Head (com.alibaba.excel.metadata.Head)3 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Cell (org.apache.poi.ss.usermodel.Cell)3 ExcelProperty (com.alibaba.excel.annotation.ExcelProperty)2 ContentFontStyle (com.alibaba.excel.annotation.write.style.ContentFontStyle)2 ContentStyle (com.alibaba.excel.annotation.write.style.ContentStyle)2 ExcelCommonException (com.alibaba.excel.exception.ExcelCommonException)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 AllArgsConstructor (lombok.AllArgsConstructor)2 EqualsAndHashCode (lombok.EqualsAndHashCode)2 Getter (lombok.Getter)2 Setter (lombok.Setter)2