The simplified version of easyexcel cancels the default header style, and supports callbacks to achieve differentiation of different header styles

In the  article of easyexcel cancel the default header style , there is a setting based on the format of the annotation cell content. The code below only rewrites the header style and does not include cell content formatting.

One solution , the code below is still based on easyexcel's WriteCellStyle, the ExcelStyleAnnotationAndCancelDefaultHeadStyleCellWriteHandler#buildHeadCellStyle method can be found in easyexcel cancel the default header style . The headStyleConsumer callback can be used to make different settings for the format of the header content of different cells. ThreeConsumer didn't put the relativeRowIndex into it, just because there are already three parameters, these three parameters and relativeRowIndex should be encapsulated into one class. For example, HeadConsumer below can replace ThreeConsumer.

public class CancelDefaultHeadStyleCellWriteHandler extends AbstractCellStyleStrategy {
    private WriteCellStyle headWriteCellStyleSelf;
    private ThreeConsumer<CellStyle,Cell,Head> headStyleConsumer;
    public CancelDefaultHeadStyleCellWriteHandler(WriteCellStyle headWriteCellStyle,ThreeConsumer<CellStyle,Cell,Head> headStyleConsumer) {
        this.headWriteCellStyleSelf = headWriteCellStyle;
        this.headStyleConsumer = headStyleConsumer;
    }
    @Override
    protected void initCellStyle(Workbook workbook) {
        //NO-OP
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        Workbook workbook = cell.getSheet().getWorkbook();
        CellStyle headCellStyleSelf = ExcelStyleAnnotationAndCancelDefaultHeadStyleCellWriteHandler.buildHeadCellStyle(workbook, headWriteCellStyleSelf);
        if (headCellStyleSelf == null) {
            return;
        }
        if (headStyleConsumer != null) {
            headStyleConsumer.accept(headCellStyleSelf,cell,head);
        }
        cell.setCellStyle(headCellStyleSelf);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        //NO-OP
    }
}
@FunctionalInterface
public interface ThreeConsumer<T, U, V>  {
    void accept(T t, U u,V v);
}
public interface HeadConsumer<T extends HeadConsumer.HeadConsumerHelper> {
    void accept(T t);
    @Data
    public static class HeadConsumerHelper{
        private Workbook workbook;
        private Cell cell;
        private Head head;
        private Integer relativeRowIndex;
        private CellStyle headCellStyle;
    }
}

 ThreeConsumer example: Set different styles according to the header content. Of course, if there is a relativeRowIndex, you can also set different row header styles according to your needs. ( Based on the writing of WriteTable, the relativeRowIndex of different tables start from 0 )

    public static void headStyleConsumer(CellStyle cellStyle, Cell cell, Head head) {
        String name = cell.getStringCellValue().trim();
        if ("AA".equals(name) || "BB".equals(name)) {
            Font font = cell.getSheet().getWorkbook().createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short)11);
            cellStyle.setFont(font);
            cell.getRow().setHeight((short)1000);
        }else {
            cellStyle.setBorderTop(BorderStyle.NONE);
            cellStyle.setBorderRight(BorderStyle.NONE);
            cell.getRow().setHeight((short)600);
        }
    }

The second solution is to abandon the easyexcel-based WriteCellStyle and rewrite the setHeadCellStyle method, as shown in the following code. The HeadConsumer callback is used to set the style of the header, and the differentiated style of the header can be set in the callback.

public class CancelDefaultHeadStyleCellWriteHandler extends AbstractCellStyleStrategy {
    private HeadConsumer<HeadConsumer.HeadConsumerHelper> headStyleConsumer;
    public CancelDefaultHeadStyleCellWriteHandler(HeadConsumer<HeadConsumer.HeadConsumerHelper> headStyleConsumer) {
        this.headStyleConsumer = headStyleConsumer;
    }
    @Override
    protected void initCellStyle(Workbook workbook) {
        //NO-OP
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        Workbook workbook = cell.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        HeadConsumer.HeadConsumerHelper helper = new HeadConsumer.HeadConsumerHelper();
        helper.setHeadCellStyle(cellStyle);
        helper.setHead(head);
        helper.setCell(cell);
        helper.setRelativeRowIndex(relativeRowIndex);
        helper.setWorkbook(workbook);
        headStyleConsumer.accept(helper);
        cell.setCellStyle(cellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        //NO-OP
    }
}

 

Guess you like

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