easyexcel cancels the default header style

Simplified version cancels the default header style, click to jump

The Excel header style exported by default by easyexcel is as shown in the figure above. The default styles are set for the border and background colors. The source code is as follows:

 

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;
}

Because the background color is set, no matter how you set other background colors and borders, they cannot be consistent with the content style. Because of the code below:

 

com\alibaba\excel\util\StyleUtil.class

if (writeCellStyle.getFillBackgroundColor() != null) {
    cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
}
if (writeCellStyle.getFillForegroundColor() != null) {
    cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
}

As long as writeCellStyle.getFillBackgroundColor() != null or writeCellStyle.getFillForegroundColor() != null, the border will not be the default style. Even if the background color is set to the default IndexedColors.AUTOMATIC, the borders are all set to BorderStyle.NONE, and there is no border at this time. Therefore, the code that sets any background color cannot be executed.

The methods of initializing the header style and setting the header style in easyexcel are as follows, initCellStyle will execute the default setting header method. If you want to initialize the initCellStyle method, you will find that the style initialization of the content is also in this method. It is more troublesome to rewrite, so I decided to rewrite only the setHeadCellStyle method. It does not matter even if the initCellStyle method is executed, as long as you customize the header style in the overridden setHeadCellStyle, that is, overwrite the default header style.

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);
}

 

The rewriting method is as follows, ExcelStyleAnnotationCellWriteHandler sets the format of the cell for easyexcel, and implements the class in the precise control of the format of each column through a custom annotation method . The style setting is copied from the method in com\alibaba\excel\util\StyleUtil.class, but the ishead parameter is removed.

@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());
        }
    }
}

Note: The header style passed in by the constructor cannot set the background color and border

 

 

 

 

Guess you like

Origin blog.csdn.net/sinat_33472737/article/details/103719527