EasyExcel中修改表头颜色、自定义表头颜色字体等

EasyExcel中修改表头颜色、自定义表头颜色字体等
需求描述:
EasyExcel的表头的默认颜色以及字体设置和excel中的不一致,产品要求修改。

EasyExcel版本为2.2.8。

默认设置参考:
com.alibaba.excel.util.StyleUtil这个类

前后效果图:

原先:

修改后:

 

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

public class CustomCellWriteHandler extends AbstractCellStyleStrategy implements CellWriteHandler {

    private XSSFCellStyle headCellStyle;

    @Override
    protected void initCellStyle(Workbook workbook) {
        headCellStyle = (XSSFCellStyle) StyleUtil.buildHeadCellStyle(workbook, null);
        headCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(231, 230, 230)));
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 14);
        font.setBold(true);
        headCellStyle.setFont(font);
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        cell.setCellStyle(headCellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {

    }

}

 

效果已经达到预期,各位同学可以在这个基础上实现一些自定义需求。欢迎指正!

Java | 极客之音

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/iaoyou1/article/details/130500146

posted on 2025-05-12 15:21  天军  阅读(823)  评论(0)    收藏  举报

导航