Excel动态数据导出快速使用

1,后端创建动态表头是否导出的判断类
/*
    根据前端传过来的数据动态增加表头
 */

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ExcelHeader {
 
    /**
     * 要导出的字段名称
     */
    private String fieldName;
 
    /**
     * 要导出的表头名称
     */
    private String headName;
}

2,

   List excelHeaders = new ArrayList();
        if (Export.getChecked()) {
        //通过前端传来的boolean值判断数据是否导出
            excelHeaders.add(new ExcelHeader(" 要导出的字段名", "要导出的表头名称"));
        }
         ExcelUtil.dynamicExportExcel(excelHeaders(所有导出的表头), list(查出的所有数据), response);
3,导出excel的类
 public static  void dynamicExportExcel(List headers, List dataList, HttpServletResponse response) {
        long startTime = System.currentTimeMillis();
        List> allList = new ArrayList<>();
        for (T detail : dataList) {
            allList.addAll(ExcelUtil.dataList(headers, detail));
        }
        numberConvertToString(headers, allList);
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            EasyExcel.write(outputStream).head(ExcelUtil.headers(headers))
                    //文件样式
                    .registerWriteHandler(new com.ruoyi.common.utils.poi.CellStyle())
                    .registerWriteHandler(new com.ruoyi.common.utils.poi.CellStyle().setStyleStrategy())
                    .sheet("表名").doWrite(allList);
        } catch (IOException e) {
            log.error("生成动态EXL失败,字段", e);
        }
        long endTime = System.currentTimeMillis();
        log.info("动态导出耗时:{}", endTime - startTime);
    }

4,导出excel样式类
public class CellStyle extends AbstractColumnWidthStyleStrategy {

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List> cellDataList, Cell cell, Head head,
                                  Integer relativeRowIndex, Boolean isHead) {
        // 简单设置
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.setColumnWidth(cell.getColumnIndex(), 6500);

    }

    /**
     * 自定义样式(表头、内容的字体、背景、样式)
     *
     * @return HorizontalCellStyleStrategy
     */
    public HorizontalCellStyleStrategy setStyleStrategy() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 设置居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 背景色, 设置为浅灰
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        // 字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 14);
        headWriteCellStyle.setWriteFont(headWriteFont);

        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了FillPatternType所以可以不指定
//         contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
//         contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

        // 背景灰色
//        contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        // 设置 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 设置边框样式
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

        // 头是头的样式 内容是内容的样式
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


}

5,导入依赖
    
            
                com.alibaba
                easyexcel
                3.1.5
            

你可能感兴趣的:(excel,状态模式)