easyexcel 默认导出的excel表头样式如上图,边框和背景色都设置了默认样式,源码如下:
com\alibaba\excel\util\StyleUtil.class
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.setWrapText(true);
newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
newCellStyle.setAlignment(HorizontalAlignment.CENTER);
newCellStyle.setLocked(true);
newCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
newCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
newCellStyle.setBorderTop(BorderStyle.THIN);
newCellStyle.setBorderBottom(BorderStyle.THIN);
newCellStyle.setBorderLeft(BorderStyle.THIN);
newCellStyle.setBorderRight(BorderStyle.THIN);
return newCellStyle;
}
因为设置了背景色,所以无论怎么设置其他背景色和边框,都无法和内容样式保持一致。因为下方代码:
com\alibaba\excel\util\StyleUtil.class
if (writeCellStyle.getFillBackgroundColor() != null) {
cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
}
if (writeCellStyle.getFillForegroundColor() != null) {
cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
}
只要writeCellStyle.getFillBackgroundColor() != null或writeCellStyle.getFillForegroundColor() != null,边框就不会是默认的样式。即使把背景色设置为默认的IndexedColors.AUTOMATIC,边框全设置为BorderStyle.NONE也不行,这个时候没有边框。所以不能执行设置任何背景色的代码。
easyexcel中初始化表头样式和设置表头样式方法如下,initCellStyle会执行默认设置表头方法。如果要初始化initCellStyle方法会发现内容的样式初始化也在这个方法中,重写的话比较麻烦,所以决定只重写setHeadCellStyle方法。即使执行了initCellStyle方法也无妨,只要在重写的setHeadCellStyle中自定义表头样式即可,即覆盖掉默认表头样式。
com\alibaba\excel\write\style\HorizontalCellStyleStrategy.class
@Override
protected void initCellStyle(Workbook workbook) {
if (headWriteCellStyle != null) {
headCellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
}
if (contentWriteCellStyleList != null && !contentWriteCellStyleList.isEmpty()) {
contentCellStyleList = new ArrayList<CellStyle>();
for (WriteCellStyle writeCellStyle : contentWriteCellStyleList) {
contentCellStyleList.add(StyleUtil.buildContentCellStyle(workbook, writeCellStyle));
}
}
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (headCellStyle == null) {
return;
}
cell.setCellStyle(headCellStyle);
}
重写方法如下,ExcelStyleAnnotationCellWriteHandler为easyexcel 给单元格设置格式,通过自定义注解方式实现对每一列格式的精确控制中的类。样式的设置copy了com\alibaba\excel\util\StyleUtil.class中的方法,只是去掉了ishead参数。
@Slf4j
@Data
public class ExcelStyleAnnotationAndCancelDefaultHeadStyleCellWriteHandler extends ExcelStyleAnnotationCellWriteHandler {
private WriteCellStyle headWriteCellStyleSelf;
private CellStyle headCellStyleSelf;
private Class c;
public ExcelStyleAnnotationAndCancelDefaultHeadStyleCellWriteHandler(Class c, WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
super(c,headWriteCellStyle, contentWriteCellStyle);
this.headWriteCellStyleSelf = headWriteCellStyle;
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
Workbook workbook = cell.getSheet().getWorkbook();
headCellStyleSelf = buildHeadCellStyle(workbook, headWriteCellStyleSelf);
if (headCellStyleSelf == null) {
return;
}
cell.setCellStyle(headCellStyleSelf);
}
/**
* Build head cell style
*/
private static CellStyle buildHeadCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
CellStyle cellStyle = workbook.createCellStyle();
if (writeCellStyle == null) {
return cellStyle;
}
buildCellStyle(workbook, cellStyle, writeCellStyle);
return cellStyle;
}
private static void buildCellStyle(Workbook workbook, CellStyle cellStyle, WriteCellStyle writeCellStyle) {
buildFont(workbook, cellStyle, writeCellStyle.getWriteFont());
if (writeCellStyle.getDataFormat() != null) {
cellStyle.setDataFormat(writeCellStyle.getDataFormat());
}
if (writeCellStyle.getHidden() != null) {
cellStyle.setHidden(writeCellStyle.getHidden());
}
if (writeCellStyle.getLocked() != null) {
cellStyle.setLocked(writeCellStyle.getLocked());
}
if (writeCellStyle.getQuotePrefix() != null) {
cellStyle.setQuotePrefixed(writeCellStyle.getQuotePrefix());
}
if (writeCellStyle.getHorizontalAlignment() != null) {
cellStyle.setAlignment(writeCellStyle.getHorizontalAlignment());
}
if (writeCellStyle.getWrapped() != null) {
cellStyle.setWrapText(writeCellStyle.getWrapped());
}
if (writeCellStyle.getVerticalAlignment() != null) {
cellStyle.setVerticalAlignment(writeCellStyle.getVerticalAlignment());
}
if (writeCellStyle.getRotation() != null) {
cellStyle.setRotation(writeCellStyle.getRotation());
}
if (writeCellStyle.getIndent() != null) {
cellStyle.setIndention(writeCellStyle.getIndent());
}
if (writeCellStyle.getBorderLeft() != null) {
cellStyle.setBorderLeft(writeCellStyle.getBorderLeft());
}
if (writeCellStyle.getBorderRight() != null) {
cellStyle.setBorderRight(writeCellStyle.getBorderRight());
}
if (writeCellStyle.getBorderTop() != null) {
cellStyle.setBorderTop(writeCellStyle.getBorderTop());
}
if (writeCellStyle.getBorderBottom() != null) {
cellStyle.setBorderBottom(writeCellStyle.getBorderBottom());
}
if (writeCellStyle.getLeftBorderColor() != null) {
cellStyle.setLeftBorderColor(writeCellStyle.getLeftBorderColor());
}
if (writeCellStyle.getRightBorderColor() != null) {
cellStyle.setRightBorderColor(writeCellStyle.getRightBorderColor());
}
if (writeCellStyle.getTopBorderColor() != null) {
cellStyle.setTopBorderColor(writeCellStyle.getTopBorderColor());
}
if (writeCellStyle.getBottomBorderColor() != null) {
cellStyle.setBottomBorderColor(writeCellStyle.getBottomBorderColor());
}
if (writeCellStyle.getFillPatternType() != null) {
cellStyle.setFillPattern(writeCellStyle.getFillPatternType());
}
if (writeCellStyle.getFillBackgroundColor() != null) {
cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
}
if (writeCellStyle.getFillForegroundColor() != null) {
cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
}
if (writeCellStyle.getShrinkToFit() != null) {
cellStyle.setShrinkToFit(writeCellStyle.getShrinkToFit());
}
}
private static void buildFont(Workbook workbook, CellStyle cellStyle, WriteFont writeFont) {
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
cellStyle.setFont(font);
if (writeFont == null) {
return;
}
if (writeFont.getFontName() != null) {
font.setFontName(writeFont.getFontName());
}
if (writeFont.getFontHeightInPoints() != null) {
font.setFontHeightInPoints(writeFont.getFontHeightInPoints());
}
if (writeFont.getItalic() != null) {
font.setItalic(writeFont.getItalic());
}
if (writeFont.getStrikeout() != null) {
font.setStrikeout(writeFont.getStrikeout());
}
if (writeFont.getColor() != null) {
font.setColor(writeFont.getColor());
}
if (writeFont.getTypeOffset() != null) {
font.setTypeOffset(writeFont.getTypeOffset());
}
if (writeFont.getUnderline() != null) {
font.setUnderline(writeFont.getUnderline());
}
if (writeFont.getCharset() != null) {
font.setCharSet(writeFont.getCharset());
}
if (writeFont.getBold() != null) {
font.setBold(writeFont.getBold());
}
}
}
注意:构造函数传入的表头样式不能设置背景色和边框
来源:CSDN
作者:sinat_33472737
链接:https://blog.csdn.net/sinat_33472737/article/details/103719527