Search in sources :

Example 1 with AbstractVerticalCellStyleStrategy

use of com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy 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 AbstractVerticalCellStyleStrategy

use of com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy 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 AbstractVerticalCellStyleStrategy

use of com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy 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 AbstractVerticalCellStyleStrategy

use of com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy 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 AbstractVerticalCellStyleStrategy

use of com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy in project easyexcel by alibaba.

the class StyleDataTest method t04AbstractVerticalCellStyleStrategy02.

@Test
public void t04AbstractVerticalCellStyleStrategy02() {
    final StyleProperty styleProperty = StyleProperty.build(StyleData.class.getAnnotation(HeadStyle.class));
    final FontProperty fontProperty = FontProperty.build(StyleData.class.getAnnotation(HeadFontStyle.class));
    AbstractVerticalCellStyleStrategy verticalCellStyleStrategy = new AbstractVerticalCellStyleStrategy() {

        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            WriteCellStyle writeCellStyle = WriteCellStyle.build(styleProperty, fontProperty);
            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(fileVerticalCellStyleStrategy207, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet().doWrite(data());
}
Also used : StyleProperty(com.alibaba.excel.metadata.property.StyleProperty) FontProperty(com.alibaba.excel.metadata.property.FontProperty) AbstractVerticalCellStyleStrategy(com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy) Head(com.alibaba.excel.metadata.Head) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) HeadFontStyle(com.alibaba.excel.annotation.write.style.HeadFontStyle) HeadStyle(com.alibaba.excel.annotation.write.style.HeadStyle) Test(org.junit.Test)

Aggregations

Head (com.alibaba.excel.metadata.Head)5 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)5 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)4 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)4 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)3 Test (org.junit.Test)2 HeadFontStyle (com.alibaba.excel.annotation.write.style.HeadFontStyle)1 HeadStyle (com.alibaba.excel.annotation.write.style.HeadStyle)1 DataFormatData (com.alibaba.excel.metadata.data.DataFormatData)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 FontProperty (com.alibaba.excel.metadata.property.FontProperty)1 StyleProperty (com.alibaba.excel.metadata.property.StyleProperty)1 CellWriteHandler (com.alibaba.excel.write.handler.CellWriteHandler)1 RowWriteHandler (com.alibaba.excel.write.handler.RowWriteHandler)1 SheetWriteHandler (com.alibaba.excel.write.handler.SheetWriteHandler)1 WorkbookWriteHandler (com.alibaba.excel.write.handler.WorkbookWriteHandler)1 WriteHandler (com.alibaba.excel.write.handler.WriteHandler)1