Search in sources :

Example 1 with Head

use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.

the class StyleDataTest method t03AbstractVerticalCellStyleStrategy.

@Test
public void t03AbstractVerticalCellStyleStrategy() {
    AbstractVerticalCellStyleStrategy verticalCellStyleStrategy = new AbstractVerticalCellStyleStrategy() {

        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            WriteCellStyle writeCellStyle = new WriteCellStyle();
            writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            DataFormatData dataFormatData = new DataFormatData();
            dataFormatData.setIndex((short) 0);
            writeCellStyle.setDataFormatData(dataFormatData);
            writeCellStyle.setHidden(false);
            writeCellStyle.setLocked(true);
            writeCellStyle.setQuotePrefix(true);
            writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            writeCellStyle.setWrapped(true);
            writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            writeCellStyle.setRotation((short) 0);
            writeCellStyle.setIndent((short) 10);
            writeCellStyle.setBorderLeft(BorderStyle.THIN);
            writeCellStyle.setBorderRight(BorderStyle.THIN);
            writeCellStyle.setBorderTop(BorderStyle.THIN);
            writeCellStyle.setBorderBottom(BorderStyle.THIN);
            writeCellStyle.setLeftBorderColor(IndexedColors.RED.getIndex());
            writeCellStyle.setRightBorderColor(IndexedColors.RED.getIndex());
            writeCellStyle.setTopBorderColor(IndexedColors.RED.getIndex());
            writeCellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
            writeCellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
            writeCellStyle.setShrinkToFit(Boolean.TRUE);
            if (head.getColumnIndex() == 0) {
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                WriteFont writeFont = new WriteFont();
                writeFont.setItalic(true);
                writeFont.setStrikeout(true);
                writeFont.setTypeOffset(Font.SS_NONE);
                writeFont.setUnderline(Font.U_DOUBLE);
                writeFont.setBold(true);
                writeFont.setCharset((int) Font.DEFAULT_CHARSET);
            } else {
                writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            }
            return writeCellStyle;
        }

        @Override
        protected WriteCellStyle contentCellStyle(Head head) {
            WriteCellStyle writeCellStyle = new WriteCellStyle();
            writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            if (head.getColumnIndex() == 0) {
                writeCellStyle.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
            } else {
                writeCellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
            }
            return writeCellStyle;
        }
    };
    EasyExcel.write(fileVerticalCellStyleStrategy07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet().doWrite(data());
}
Also used : AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) DataFormatData(com.alibaba.excel.metadata.data.DataFormatData) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) Test(org.junit.Test)

Example 2 with Head

use of com.alibaba.excel.metadata.Head 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 Head

use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.

the class AbstractWriteHolder method dealColumnWidth.

private void dealColumnWidth(List<WriteHandler> handlerList) {
    WriteHandler columnWidthStyleStrategy = new AbstractHeadColumnWidthStyleStrategy() {

        @Override
        protected Integer columnWidth(Head head, Integer columnIndex) {
            if (head == null) {
                return null;
            }
            if (head.getColumnWidthProperty() != null) {
                return head.getColumnWidthProperty().getWidth();
            }
            return null;
        }
    };
    handlerList.add(columnWidthStyleStrategy);
}
Also used : AbstractHeadColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy) Head(com.alibaba.excel.metadata.Head) 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)

Example 4 with Head

use of com.alibaba.excel.metadata.Head in project easyexcel by alibaba.

the class ExcelWriteHeadProperty method headCellRangeList.

/**
 * Calculate all cells that need to be merged
 *
 * @return cells that need to be merged
 */
public List<CellRange> headCellRangeList() {
    List<CellRange> cellRangeList = new ArrayList<CellRange>();
    Set<String> alreadyRangeSet = new HashSet<String>();
    List<Head> headList = new ArrayList<Head>(getHeadMap().values());
    for (int i = 0; i < headList.size(); i++) {
        Head head = headList.get(i);
        List<String> headNameList = head.getHeadNameList();
        for (int j = 0; j < headNameList.size(); j++) {
            if (alreadyRangeSet.contains(i + "-" + j)) {
                continue;
            }
            alreadyRangeSet.add(i + "-" + j);
            String headName = headNameList.get(j);
            int lastCol = i;
            int lastRow = j;
            for (int k = i + 1; k < headList.size(); k++) {
                String key = k + "-" + j;
                if (headList.get(k).getHeadNameList().get(j).equals(headName) && !alreadyRangeSet.contains(key)) {
                    alreadyRangeSet.add(key);
                    lastCol = k;
                } else {
                    break;
                }
            }
            Set<String> tempAlreadyRangeSet = new HashSet<>();
            outer: for (int k = j + 1; k < headNameList.size(); k++) {
                for (int l = i; l <= lastCol; l++) {
                    String key = l + "-" + k;
                    if (headList.get(l).getHeadNameList().get(k).equals(headName) && !alreadyRangeSet.contains(key)) {
                        tempAlreadyRangeSet.add(l + "-" + k);
                    } else {
                        break outer;
                    }
                }
                lastRow = k;
                alreadyRangeSet.addAll(tempAlreadyRangeSet);
            }
            if (j == lastRow && i == lastCol) {
                continue;
            }
            cellRangeList.add(new CellRange(j, lastRow, head.getColumnIndex(), headList.get(lastCol).getColumnIndex()));
        }
    }
    return cellRangeList;
}
Also used : Head(com.alibaba.excel.metadata.Head) CellRange(com.alibaba.excel.metadata.CellRange) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 5 with Head

use of com.alibaba.excel.metadata.Head 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)

Aggregations

Head (com.alibaba.excel.metadata.Head)19 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)5 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)5 Map (java.util.Map)5 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)4 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)4 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)3 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)3 Cell (org.apache.poi.ss.usermodel.Cell)3 Sheet (org.apache.poi.ss.usermodel.Sheet)3 BeanMap (org.springframework.cglib.beans.BeanMap)3 CellWriteHandler (com.alibaba.excel.write.handler.CellWriteHandler)2 RowWriteHandler (com.alibaba.excel.write.handler.RowWriteHandler)2 SheetWriteHandler (com.alibaba.excel.write.handler.SheetWriteHandler)2 WorkbookWriteHandler (com.alibaba.excel.write.handler.WorkbookWriteHandler)2 WriteHandler (com.alibaba.excel.write.handler.WriteHandler)2 WriteHolder (com.alibaba.excel.write.metadata.holder.WriteHolder)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 TreeMap (java.util.TreeMap)2