easyExcel根據ExcelDTO生成excel並導出自定義樣式


@Override
    public String exportExcelModel(HttpServletResponse response) {
        OutputStream outputStream = ExcelUtils.getResponseOutputStream(response, "導入模板");
        ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(outputStream).registerWriteHandler(new CustomCellWriteHandler()).excelType(ExcelTypeEnum.XLSX);
        ExcelWriter excelWriter = excelWriterBuilder.build();

        ExcelUtils.writeOnly(excelWriter, importAssignExcelDemo(), OneExcelDTO.class, 1, "模板數據");
        ExcelUtils.writeOnly(excelWriter, describeDemo(), TwoExcelDTO.class, 2, "詳細說明");
        try {
            ExcelUtils.finishWriter(outputStream, excelWriter);
        } catch (Exception e) {
            System.out.println("導出異常!!!");
            e.printStackTrace();
        }
        return "導出指定模板結束";
    }
/**
* 生成excel文件指定sheet頁
*
* @param excelWriter
* @param data
* @param clazz
* @param sheetNo
* @param sheetName
* @param <T>
*/

public static <T> void writeOnly(ExcelWriter excelWriter, List<T> data, Class clazz,Integer sheetNo,String sheetName){
        ExcelWriterSheetBuilder excelWriterSheetBuilder;
        WriteSheet writeSheet = new WriteSheet();
        excelWriterSheetBuilder = new ExcelWriterSheetBuilder(excelWriter);
        excelWriterSheetBuilder.sheetNo(sheetNo);
        excelWriterSheetBuilder.sheetName(sheetName);
        writeSheet.setSheetNo(sheetNo);
        writeSheet.setSheetName(sheetName);
        writeSheet.setClazz(clazz);
        excelWriter.write(data,writeSheet);
    }
public static void finishWriter(OutputStream outputStream,ExcelWriter excelWriter) throws IOException { outputStream.flush(); excelWriter.finish(); outputStream.close(); System.out.println("結束!!!"); }

  

自定義樣式監聽類:
繼承抽象策略

public class CustomCellWriteHandler extends AbstractCellStyleStrategy {

private WriteCellStyle headWriteCellStyle;
private List<WriteCellStyle> contentWriteCellStyleList;
private CellStyle headCellStyle;
private List<CellStyle> contentCellStyleList;

public CustomCellWriteHandler(WriteCellStyle headWriteCellStyle, List<WriteCellStyle> contentWriteCellStyleList) {
this.headWriteCellStyle = headWriteCellStyle;
this.contentWriteCellStyleList = contentWriteCellStyleList;
}

public CustomCellWriteHandler(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
this.headWriteCellStyle = headWriteCellStyle;
this.contentWriteCellStyleList = new ArrayList();
this.contentWriteCellStyleList.add(contentWriteCellStyle);
}
@Override
protected void initCellStyle(Workbook workbook) {
if (this.headWriteCellStyle != null) {
this.headCellStyle = StyleUtil.buildHeadCellStyle(workbook, this.headWriteCellStyle);
}

if (this.contentWriteCellStyleList != null && !this.contentWriteCellStyleList.isEmpty()) {
this.contentCellStyleList = new ArrayList();
Iterator var2 = this.contentWriteCellStyleList.iterator();

while(var2.hasNext()) {
WriteCellStyle writeCellStyle = (WriteCellStyle)var2.next();
this.contentCellStyleList.add(StyleUtil.buildContentCellStyle(workbook, writeCellStyle));
}
}

}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (this.headCellStyle != null) {
cell.setCellStyle(this.headCellStyle);
}
}
@Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (this.contentCellStyleList != null && !this.contentCellStyleList.isEmpty()) {

// 這里可以對cell進行任何操作
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle headStyle = getContentStyle(workbook, cell);
cell.setCellStyle(headStyle);

// cell.setCellStyle((CellStyle)this.contentCellStyleList.get(relativeRowIndex % this.contentCellStyleList.size()));
}
}



/*
* 表內容樣式
*/
public static CellStyle getContentStyle(Workbook workbook, Cell cell) {
// 設置字體
Font font = workbook.createFont();
//設置字體大小
font.setFontHeightInPoints((short) 11);
//字體加粗
font.setBold(false);
//斜體
font.setItalic(true);
//設置字體名字
font.setFontName("宋體");
//設置樣式;
CellStyle style = workbook.createCellStyle();
if(cell.getRowIndex() == 1){
//第一行示例數據 紅色 宋體 斜體
font.setColor(IndexedColors.RED.getIndex());//字體顏色=紅色
}
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(true);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HorizontalAlignment.CENTER);
//數據規范信息描述,加粗、靠左,通過內容指定內容格式
if(cell.getColumnIndex()==0 && cell.getStringCellValue().contains("導入數據規范說明")){
font.setBold(true);
style.setAlignment(HorizontalAlignment.LEFT);
}
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
}

  

 

參考鏈接地址:

https://blog.csdn.net/m0_46109609/article/details/109774365
https://www.cnblogs.com/Hizy/p/11825886.html


免責聲明!

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



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