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 \ 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 을위한 셀의 형식을 설정하고 사용자 지정 주석 방법을 통해 각 열의 형식을 정밀하게 제어 하여 클래스 를 구현 합니다. 스타일 설정은 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