《Spring Cloud Alibaba實戰》系列-Easy Excel 導出excel自定義頭部樣式

前言

最近測試測試時,提了一個需求,導入模板頭部的必填項需要標紅;因爲我用的動態導出模板,不像之前公司那樣,直接定義好一個excel模板,然後提供下載,那樣直接修改excel模板就可以了。但是動態模板需要在導出時設置下頭部樣式實現。再經歷了各種嘗試後,本文坐下記錄。
如果需要導入導出功能的,請參考前面的一篇《Easy Excel實現Excel的導入導出》

核心策略類

策略類中的屬性可以根據需求自己定義,我這裏定了頭部必填需要的幾個屬性。

/**
 * 單元自定義樣式
 *
 * @author gourd.hu
 */
@Slf4j
public class CustomCellStyleStrategy extends AbstractCellStyleStrategy {

    /**
     * 操作行
     */
    private List<Integer> columnIndexes;
    /**
     * 操作列
     */
    private List<Integer> rowIndexes;
    /**
     * 顏色
     */
    private Short colorIndex;


    public CustomCellStyleStrategy(List<Integer> rowIndexes, List<Integer> columnIndexes, Short colorIndex) {
        this.rowIndexes = rowIndexes;
        this.columnIndexes = columnIndexes;
        this.colorIndex = colorIndex;
    }

    @Override
    protected void initCellStyle(Workbook workbook) {

    }

    /**
     * 自定義頭部樣式
     *
     * @param cell
     * @param head
     * @param integer
     */
    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer integer) {
        // 獲取workbook
        Workbook workbook = cell.getSheet().getWorkbook();
        // 獲取樣式實例
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 獲取字體實例
        WriteFont headWriteFont = new WriteFont();
        // 設置字體樣式
        headWriteFont.setFontName("宋體");
        // 設置字體大小
        headWriteFont.setFontHeightInPoints((short)14);
        // 邊框
        headWriteFont.setBold(true);
        // 設置背景顏色爲灰色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        if (CollectionUtils.isNotEmpty(columnIndexes) && columnIndexes.contains(cell.getColumnIndex())
                && CollectionUtils.isNotEmpty(rowIndexes) && rowIndexes.contains(cell.getRowIndex())
                && colorIndex != null) {
            // 設置指定單元格字體自定義顏色
            headWriteFont.setColor(colorIndex);
        }
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 獲取樣式實例
        CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
        // 單元格設置樣式
        cell.setCellStyle(cellStyle);
    }

    /**
     * 自定義內容樣式
     *
     * @param cell
     * @param head
     * @param integer
     */
    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer integer) {

    }
}

使用

		// 必填頭部標紅
        List<Integer> columnIndexes = Arrays.asList(0,1,2);
        List<Integer> rowIndexes = Arrays.asList(0,1);
		CustomCellStyleStrategy customCellStyleStrategy = new CustomCellStyleStrategy(rowIndexes,columnIndexes, IndexedColors.RED.getIndex());
        EasyExcelUtil.writeSingleExcel("單sheet導出","用戶",userPOList,UserPO.class,customCellStyleStrategy);
    

工具類方法

/**
     * 導出文件
     * 導出模板時,tList傳一個空list即可
     * @param tList 數據集
     * @param tClass 數據類型
     * @param <T>
     * @throws IOException
     */
    public static <T> void writeSingleExcel(String fileName, String sheetName, List<T> tList, Class tClass, CustomCellStyleStrategy customCellStyleStrategy) throws IOException{
        HttpServletResponse response = RequestHolder.getResponse();
        try (ServletOutputStream outputStream = response.getOutputStream()){
            setResponse(fileName, response);
            EasyExcel.write(outputStream, tClass).autoCloseStream(Boolean.TRUE)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .registerWriteHandler(new CustomCellWriteHandler())
                    .registerWriteHandler(customCellStyleStrategy)
                    .sheet(sheetName)
                    .doWrite(tList);
        } catch (Exception e) {
            errorWrite(response, e);
        }
    }

效果

在這裏插入圖片描述

代碼均已上傳至本人的開源項目
cloud-plus:https://blog.csdn.net/HXNLYW/article/details/104635673

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章