關於EasyExcel 的一些生成模板,導入導出的使用心得(優化版)


這里就簡單說了,直接貼代碼

@HeadFontStyle(color =2)  維護表頭第2行的注解
/**
 * @author: kuangql@fadada.com
 * @date: 2020/11/27 19:53
 * @description: TODO
 */
@Data
public class DemoExportEntity {

    public static final String bigTitle= "填寫須知: \n" +
            "1.第1、2行為固定結構,不可更改;以下示例行,導入前請先刪除\n" +
            "2.請嚴格按照填寫規則輸入數據,不合規的數據無法成功導入 \n" +
            "3.測試換行 \n";


    /**
     *
     * org.apache.poi.ss.usermodel.IndexedColors 這個類有所有的顏色枚舉值    2是紅色
     * @ HeadFontStyle  注解默認是宋體,黑色,加粗
     *
     */
    @ExcelProperty(value = {"姓名(必填)"}, index = 0)
    @ColumnWidth(30)
    @HeadFontStyle(color =2)
    private String userName;

    @ExcelProperty(value = {"性別(必填)"}, index = 1)
    @ColumnWidth(20)
    private String userSexName;

    @ExcelProperty(value = {"手機號碼(必填)"}, index = 2)
    @ColumnWidth(30)
    private String userMobile;

    @ExcelProperty(value = {"出生年月(必填)"}, index = 3)
    @ColumnWidth(30)
    private String userBirthday;

    @ExcelProperty(value = {"工作單位(必填)"}, index = 4)
    @ColumnWidth(20)
    private String deptName;

    @ExcelProperty(value = {"職務(必填)"}, index = 5)
    @ColumnWidth(20)
    private String unitPosition;

    @ExcelProperty(value = {"干部類別(必填)"}, index = 6)
    @ColumnWidth(20)
    private String leaderTypeName;

    @ExcelProperty(value = {"用戶狀態(必填)"}, index = 7)
    @ColumnWidth(20)
    private String userStatusName;




    /**
     * 每個模板的首行高度, 換行數目+2 乘以400
     */
    public   static int getHeadHeight(){
        return  (StringUtils.getCharCounts(bigTitle,"\n")+2)*400;
    }
}

  

 

 

維護第一行表頭樣式

/**
 *
 *
 * 創建模板
 * @author: kuangql@fadada.com
 * @date: 2020/11/30 13:48
 * @description: TODO
 */
public class CreateTemplateWriteHandler implements SheetWriteHandler {


    /**
     * 第一行內容
     */
    private String firstTitle;


    /**
     * 實體模板類的行高
     */
    private int height;


    /**
     * 實體類 最大的列坐標 從0開始算
     */
    private int  lastCellIndex;



    public CreateTemplateWriteHandler(String firstTitle, int height, int cellCounts) {
        this.firstTitle = firstTitle;
        this.height = height;
        this.lastCellIndex = cellCounts;

    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        Row row1 = sheet.createRow(0);
        row1.setHeight((short) height);
        //字體樣式
        Font font = workbook.createFont();
        font.setColor((short)2);
        Cell cell = row1.createCell(0);

        //單元格樣式
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setFont(font);
        cellStyle.setWrapText(true);
        cell.setCellStyle(cellStyle);

        //設置單元格內容
        cell.setCellValue(firstTitle);


        //合並單元格  --> 起始行, 終止行   ,起始列,終止列
        sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, lastCellIndex));

    }

  

/**
     * 根據實體類生成模板
     *
     * @param response
     * @param fileName      下 載的文件名,
     * @param sheetName     sheet名
     * @param model         實體類
     * @param height        第一行行高
     * @param title         第一行表頭內容
     * @param lastCellIndex 列數
     */
    public static void createTemplate(HttpServletResponse response, String fileName,
                                      String sheetName,
                                      Class<?> model, int height, String title, int lastCellIndex) {

        EasyExcel.write(getOutputStream(fileName, response, ExcelTypeEnum.XLSX), model).
                excelType(ExcelTypeEnum.XLSX).sheet(sheetName)
                .registerWriteHandler(new CreateTemplateWriteHandler(title, height, lastCellIndex))
                .head(model)
                .useDefaultStyle(true).relativeHeadRowIndex(1)
                .doWrite(null);

    }

  

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM