Search in sources :

Example 1 with CellWriteHandlerContext

use of com.alibaba.excel.write.handler.context.CellWriteHandlerContext 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 2 with CellWriteHandlerContext

use of com.alibaba.excel.write.handler.context.CellWriteHandlerContext 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 3 with CellWriteHandlerContext

use of com.alibaba.excel.write.handler.context.CellWriteHandlerContext in project easyexcel by alibaba.

the class FillStyleDataTest method fillStyleHandler.

private void fillStyleHandler(File file, File template) throws Exception {
    EasyExcel.write(file, FillStyleData.class).withTemplate(template).sheet().registerWriteHandler(new AbstractVerticalCellStyleStrategy() {

        @Override
        protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
            WriteCellStyle writeCellStyle = new WriteCellStyle();
            WriteFont writeFont = new WriteFont();
            writeCellStyle.setWriteFont(writeFont);
            writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            writeFont.setBold(true);
            if (context.getColumnIndex() == 0) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
            }
            if (context.getColumnIndex() == 1) {
                writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                writeFont.setColor(IndexedColors.DARK_RED.getIndex());
            }
            if (context.getColumnIndex() == 2) {
                writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                writeFont.setColor(IndexedColors.DARK_GREEN.getIndex());
            }
            if (context.getColumnIndex() == 3) {
                writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                writeFont.setColor(IndexedColors.DARK_BLUE.getIndex());
            }
            if (context.getColumnIndex() == 4) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
            }
            if (context.getColumnIndex() == 5) {
                writeCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
                writeFont.setColor(IndexedColors.DARK_TEAL.getIndex());
            }
            return writeCellStyle;
        }

        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            return null;
        }
    }).doFill(data());
}
Also used : AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont)

Example 4 with CellWriteHandlerContext

use of com.alibaba.excel.write.handler.context.CellWriteHandlerContext in project easyexcel by alibaba.

the class FillStyleAnnotatedTest method fillStyleHandler.

private void fillStyleHandler(File file, File template) throws Exception {
    EasyExcel.write(file, FillData.class).withTemplate(template).sheet().registerWriteHandler(new AbstractVerticalCellStyleStrategy() {

        @Override
        protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
            WriteCellStyle writeCellStyle = new WriteCellStyle();
            WriteFont writeFont = new WriteFont();
            writeCellStyle.setWriteFont(writeFont);
            writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            writeFont.setBold(true);
            if (context.getColumnIndex() == 0) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
            }
            if (context.getColumnIndex() == 1) {
                writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                writeFont.setColor(IndexedColors.DARK_RED.getIndex());
            }
            if (context.getColumnIndex() == 2) {
                writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                writeFont.setColor(IndexedColors.DARK_GREEN.getIndex());
            }
            if (context.getColumnIndex() == 3) {
                writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                writeFont.setColor(IndexedColors.DARK_BLUE.getIndex());
            }
            if (context.getColumnIndex() == 4) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
            }
            if (context.getColumnIndex() == 5) {
                writeCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
                writeFont.setColor(IndexedColors.DARK_TEAL.getIndex());
            }
            return writeCellStyle;
        }

        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            return null;
        }
    }).doFill(data());
}
Also used : AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont)

Example 5 with CellWriteHandlerContext

use of com.alibaba.excel.write.handler.context.CellWriteHandlerContext in project easyexcel by alibaba.

the class ExcelWriteAddExecutor method doAddBasicTypeToExcel.

private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int rowIndex, int relativeRowIndex, int dataIndex, int columnIndex) {
    ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), head == null ? null : head.getFieldName());
    CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
    WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
    Cell cell = WorkBookUtil.createCell(row, columnIndex);
    cellWriteHandlerContext.setCell(cell);
    WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
    cellWriteHandlerContext.setOriginalValue(oneRowData.get(dataIndex));
    cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue()));
    converterAndSet(cellWriteHandlerContext);
    WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
}
Also used : CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)9 Head (com.alibaba.excel.metadata.Head)5 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)5 Cell (org.apache.poi.ss.usermodel.Cell)5 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)3 CellWriteHandler (com.alibaba.excel.write.handler.CellWriteHandler)3 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)3 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)3 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)3 Map (java.util.Map)3 HashSet (java.util.HashSet)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 Test (org.junit.Test)2 DemoData (com.alibaba.easyexcel.test.demo.write.DemoData)1 WriteContext (com.alibaba.excel.context.WriteContext)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