easyexcelはデフォルトのヘッダースタイルをキャンセルします

簡略化されたバージョンはデフォルトのヘッダースタイルをキャンセルします。クリックしてジャンプします

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メソッドのみを書き直すことにしました。オーバーライドされたsetHeadCellStyleのヘッダースタイルをカスタマイズする、つまりデフォルトのヘッダースタイルを上書きする限り、initCellStyleメソッドが実行されても問題ありません。

com \ alibaba \ excel \ write \ style \ Horizo​​ntalCellStyleStrategy.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のセルの形式を設定し、カスタム注釈メソッドを介して各列形式を正確に制御するクラスを実装します。スタイル設定は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());
        }
    }
}

注:コンストラクターから渡されたヘッダースタイルでは、背景色と境界線を設定できません

 

 

 

 

おすすめ

転載: blog.csdn.net/sinat_33472737/article/details/103719527
おすすめ