Search in sources :

Example 6 with WriteCellStyle

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

the class FillStyleCellWriteHandler method afterCellDispose.

@Override
public void afterCellDispose(CellWriteHandlerContext context) {
    if (BooleanUtils.isTrue(context.getIgnoreFillStyle())) {
        return;
    }
    WriteCellData<?> cellData = context.getFirstCellData();
    if (cellData == null) {
        return;
    }
    WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();
    CellStyle originCellStyle = cellData.getOriginCellStyle();
    if (writeCellStyle == null && originCellStyle == null) {
        return;
    }
    WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder();
    context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle));
}
Also used : WriteWorkbookHolder(com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) CellStyle(org.apache.poi.ss.usermodel.CellStyle) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle)

Example 7 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle 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 8 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle 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 9 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project charon by harvies.

the class ConverterTest method main.

public static void main(String[] args) {
    List<Head> list = new ArrayList<>();
    Head head = new Head();
    // head.setA("aaa");
    head.setB(Arrays.asList("ccccccccc", "dddddddd", "ccccccccc", "dddddddd", "ccccccccc", "dddddddd", "ccccccccc", "dddddddd"));
    head.setC(Arrays.asList(111111111, 1222222222, 1222222222, 1222222222, 1222222222, 1222222222, 1222222222));
    head.setD(true);
    list.add(head);
    // 头的策略
    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    // 背景设置为红色
    // headWriteCellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    // WriteFont headWriteFont = new WriteFont();
    // headWriteFont.setFontHeightInPoints((short) 20);
    // headWriteCellStyle.setWriteFont(headWriteFont);
    // 内容的策略
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
    // contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // // 背景绿色
    // contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    // WriteFont contentWriteFont = new WriteFont();
    // // 字体大小
    // contentWriteFont.setFontHeightInPoints((short) 20);
    // contentWriteCellStyle.setWriteFont(contentWriteFont);
    // 单元格不溢出
    // contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.FILL);
    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    ExcelWriter excelWriter = EasyExcelFactory.write(FileUtils.getCurrentUserHomePath() + "/Downloads/test_write.xlsx").head(Head.class).registerWriteHandler(horizontalCellStyleStrategy).build();
    WriteSheet writeSheet = new WriteSheet();
    writeSheet.setSheetNo(0);
    writeSheet.setSheetName("工作表1");
    excelWriter.write(list, writeSheet);
    excelWriter.finish();
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) ExcelWriter(com.alibaba.excel.ExcelWriter) ArrayList(java.util.ArrayList) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy)

Example 10 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project study by bage2014.

the class WriteTest method styleWrite.

/**
 * 自定义样式
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 创建一个style策略 并注册
 * <p>
 * 3. 直接写即可
 */
@Test
public void styleWrite() {
    String fileName = TestFileUtil.getPath() + "styleWrite" + System.currentTimeMillis() + ".xlsx";
    // 头的策略
    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    // 背景设置为红色
    headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
    WriteFont headWriteFont = new WriteFont();
    headWriteFont.setFontHeightInPoints((short) 20);
    headWriteCellStyle.setWriteFont(headWriteFont);
    // 内容的策略
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
    contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // 背景绿色
    contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    // 字体大小
    contentWriteFont.setFontHeightInPoints((short) 20);
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板").doWrite(data());
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy) Test(org.junit.Test)

Aggregations

WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)18 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)14 HorizontalCellStyleStrategy (com.alibaba.excel.write.style.HorizontalCellStyleStrategy)10 Test (org.junit.Test)8 Head (com.alibaba.excel.metadata.Head)4 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)4 CellStyle (org.apache.poi.ss.usermodel.CellStyle)4 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)3 ExcelWriter (com.alibaba.excel.ExcelWriter)2 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)2 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)2 ArrayList (java.util.ArrayList)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 DemoData (com.alibaba.easyexcel.test.demo.write.DemoData)1 HeadFontStyle (com.alibaba.excel.annotation.write.style.HeadFontStyle)1 HeadStyle (com.alibaba.excel.annotation.write.style.HeadStyle)1 CommentData (com.alibaba.excel.metadata.data.CommentData)1 DataFormatData (com.alibaba.excel.metadata.data.DataFormatData)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1