easyexcel表头特定列变颜色

2,036 阅读1分钟

需求说明

下标单数的表头背景为白色,下标双数的表头背景是红色

代码实现

样式定义方式:

public static AbstractVerticalCellStyleStrategy formatTemplate(){
    return new AbstractVerticalCellStyleStrategy() {
    
        /**表头样式*/
        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            if (head.getColumnIndex().intValue()%2==0){
                headWriteCellStyle.setFillForegroundColor(IndexedColors.ROSE.getIndex());
            }else {
                headWriteCellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
            }
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 10);
            headWriteCellStyle.setWriteFont(headWriteFont);
            return headWriteCellStyle;
        }

        /**内容样式*/
        @Override
        protected WriteCellStyle contentCellStyle(Head head) {
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            return contentWriteCellStyle;
        }
    };
}

使用方式:

ExcelWriter excelWriter = EasyExcel.write(os).registerWriteHandler(ExcelSetter.formatTemplate().build();
excelWriter.finish();

细节部分省略,参考官方文档:www.yuque.com/easyexcel/d…