首页 > 其他分享 >EasyExcel动态表头导出(支持多级表头)

EasyExcel动态表头导出(支持多级表头)

时间:2023-09-26 09:58:13浏览次数:34  
标签:String excel EasyExcel 多级 List 表头 fileName import public

EasyExcel动态表头导出(支持多级表头)

在很多业务场景中,都会应用到动态表头的导出,也会涉及到多级表头的导出,如下图所示

image-20230926091557316

通过EasyExcel,我们可以快速实现这一需求,具体代码如下

DynamicHeader

import java.util.List;

/**
 *@Author: <a href="mailto:[email protected]">Fxsen</a>
 *@CreateTime: 2023年09月22日  09:16
 */
public class DynamicHeader {
    /**
     * 要导出的字段名称英文
     */
    private String fieldName;

    /**
     * 要导出的表头名称中文
     */
    private String headName;
    /**
     * 如果是多级表都,插入下级
     */
    private List<DynamicHeader> children;

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getHeadName() {
        return headName;
    }

    public void setHeadName(String headName) {
        this.headName = headName;
    }

    public List<DynamicHeader> getChildren() {
        return children;
    }

    public void setChildren(List<DynamicHeader> children) {
        this.children = children;
    }
}

CustomTitleWriteHandler

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

public class CustomTitleWriteHandler implements SheetWriteHandler {
    /**
     * 标题
     */
    private final String fileName;

    /**
     * 字段个数
     */
    private final Integer count;

    public CustomTitleWriteHandler(Integer count,String fileName) {
        this.fileName = fileName;
        this.count = count;
    }

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

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        // 获取clazz所有的属性
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        Row row1 = sheet.createRow(0);
        row1.setHeight((short) 800);
        Cell cell = row1.createCell(0);
        //设置标题
        cell.setCellValue(fileName);
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 400);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, count - 1);
        setRegionStyle(sheet,region,cellStyle);
        sheet.addMergedRegion(region);
    }
    /**
     * 为合并的单元格设置样式(可根据需要自行调整)
     */
    public static void setRegionStyle(Sheet sheet, CellRangeAddress region, CellStyle cs) {
        for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
            Row row = sheet.getRow(i);
            if (null == row) row = sheet.createRow(i);
            for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
                Cell cell = row.getCell(j);
                if (null == cell) cell = row.createCell(j);
                cell.setCellStyle(cs);
            }
        }
    }
}

CellStyle

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;

public class CellStyle extends AbstractColumnWidthStyleStrategy {

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

}

DynamicExcelUtils

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.wisesoft.core.util.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
 * 动态导出工具
 *@Author: <a href="mailto:[email protected]">Fxsen</a>
 *@CreateTime: 2023年09月22日  09:13
 */
public class DynamicExcelUtils {
    private static final Logger log = LoggerFactory.getLogger(DynamicExcelUtils.class);

    /**
     * 根据模板导出数据 单个sheet
     *
     * @param response     返回对象
     * @param dataList     导出的数据集合
     * @param object       填充对象
     * @param fileName     文件名称
     * @param templateName 模板名称
     * @throws Exception
     */
    public void exportTemplateExcel(HttpServletResponse response, List<?> dataList, Object object,
                                    String fileName, String templateName) throws Exception {
        InputStream inputStream = this.getClass().getResourceAsStream(templateName);
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        ExcelWriter excelWriter = EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).build();
        WriteSheet writeSheet0 = EasyExcelFactory.writerSheet(0).build();
        excelWriter.fill(object, fillConfig, writeSheet0);
        excelWriter.fill(dataList, fillConfig, writeSheet0);
        excelWriter.finish();
    }

    /**
     * 构建输出流
     *
     * @param fileName 文件名称
     * @param response 输出流
     * @return
     * @throws Exception
     */
    private OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        return response.getOutputStream();
    }
    /**
     * 动态表头生成excel
     * @param headers 要生成的表头
     * @param dataList 数据
     * @param response
     * @param fileName 文件名称
     * @param titleName title名称
     * @param <T>
     */
    public static <T> void dynamicExportExcel(List<DynamicHeader> headers, List<T> dataList, HttpServletResponse response, String fileName, String titleName) {
        long startTime = System.currentTimeMillis();
        List<List<T>> allList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)){
            for (T detail : dataList) {
                allList.addAll(dataList(headers, detail));
            }
        }
        List<List<String>> headerList = headers(headers);
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            String name = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + name + ".xlsx");//设置响应头
            EasyExcel.write(outputStream).head(headerList)
                    //表格标题占位
                    .relativeHeadRowIndex(1)
                    //文件样式
                    .registerWriteHandler(new CustomTitleWriteHandler(headerList.size(),titleName))
                    .registerWriteHandler(new CellStyle())
                    .sheet(fileName).doWrite(allList);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("生成动态EXL失败,字段", e);
        }
        long endTime = System.currentTimeMillis();
        log.info("动态导出耗时:{}", endTime - startTime);
    }

    //excel表头
    public static List<List<String>> headers(List<DynamicHeader> excelHeaders) {
        List<List<String>> headers = new ArrayList<>();
        for (DynamicHeader header : excelHeaders) {
            List<DynamicHeader> children = header.getChildren();
            if (CollectionUtils.isNotEmpty(children)){
                for (DynamicHeader child : children) {
                    List<String> head = new ArrayList<>();
                    head.add(header.getHeadName());
                    head.add(child.getHeadName());
                    headers.add(head);
                }
            }else {
                List<String> head = new ArrayList<>();
                head.add(header.getHeadName());
                headers.add(head);
            }
        }
        return headers;
    }

    /**
     * 要导出的字段
     *
     * @param exportFields 表头集合
     * @param obj          数据对象
     * @return 集合
     */
    public static <T> List<List<T>> dataList(List<DynamicHeader> exportFields, T obj) {
        List<List<T>> list = new ArrayList<>();
        List<T> data = new ArrayList<>();
        List<String> propList = new ArrayList<>();
        for (DynamicHeader exportField : exportFields) {
            List<DynamicHeader> children = exportField.getChildren();
            if (CollectionUtils.isNotEmpty(children)){
                propList.addAll(children.stream().map(DynamicHeader::getFieldName).collect(Collectors.toList()));
            }else {
                propList.add(exportField.getFieldName());
            }
        }
        if (obj instanceof Map){
            Map map = (Map) obj;
            for (String prop : propList) {
                map.forEach((k,v)->{
                    if (prop.equals(k)){
                        if (Objects.isNull(v)){
                            v = "";
                        }else if (v instanceof Date){
                            v = DateUtil.format((Date)v,DateUtil.DATETIME_YMD_DASH);
                        }
                        data.add((T)v);
                    }
                });
            }
        }else {
            //先根据反射获取实体类的class对象
            Class<?> objClass = obj.getClass();
            //设置实体类属性的集合
            Field[] fields = ReflectUtil.getFields(objClass);
            for (String prop : propList) {
                //循环实体类对象集合
                for (Field field : fields) {
                    field.setAccessible(true);
                    //判断实体类属性跟特定字段集合名是否一样
                    if (field.getName().equals(prop)) {
                        T object = null;
                        try {
                            object = (T) field.get(obj);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                            log.error("生成动态EXL失败,字段", e);
                        }
                        //获取属性对应的值
                        if(null == object){
                            object = (T) "";
                        }else{
                            if(object instanceof LocalDate){
                                object = (T)((LocalDate)object).format(DateTimeFormatter.ofPattern(DateUtil.DEFAULT_DATETIME_FORMAT));
                            }
                            if(object instanceof LocalDateTime){
                                object = (T) ((LocalDateTime)object).format(DateTimeFormatter.ofPattern(DateUtil.DEFAULT_DATETIME_FORMAT));
                            }
                        }
                        data.add(object);
                    }
                }
            }
        }
        list.add(data);
        return list;
    }
}

测试:

image-20230926091557316

标签:String,excel,EasyExcel,多级,List,表头,fileName,import,public
From: https://www.cnblogs.com/fxsen/p/17729418.html

相关文章

  • el-table表头动态渲染未更新
    问题el-table的表头改为通过获取后端数据动态渲染,发现在请求接口后,表头并未重新渲染。//html<el-table:data="tableData"><el-table-columnv-for="(item,index)intableCol":key="index"><templateslot="header">{{item.colN......
  • 页表,多级页表
    原文但是如果程序分成的页和swap要一一对应的话,这个表存在内存中不就相当于是单页表吗,由此思考当磁盘空间足够时,程序页在磁盘中是连续储存的,所以只需要一个占用的磁盘块的起始地址既可以通过计算查询。当磁盘空间不足时,分表储存,并且存储磁盘块的起始地址和长度,通过遍历计算程......
  • 支持多级别日志消息
    1、项目介绍本项目主要实现一个日志系统,其主要支持以下功能:支持多级别日志消息将日志分为不同等级的日志,有调试级别的日志、提示级别的日志、警告级别的日志、错误级别的日志、致命级别的日志……不同级别的日志应对着不同的场景。可以通过日志级别来实现限制输出的级别:当调试......
  • 《安富莱嵌入式周报》第301期:ThreadX老大离开微软推出PX5 RTOS第5代系统,支持回流焊的
    祝大家开工大吉视频版:https://www.bilibili.com/video/BV1GT411o7zr1、ThreadX老大离开微软,开发的第5代RTOS系统PX5RTOS正式上线最早是看到IAR的一条消息,全面支持PX5RTOS,然后就进一步上他们的官方下载白皮书了解相关消息当看到这两个名字时,很熟悉,这不就是ThreadX的老大BillLamie......
  • C++的异常类型与多级catch匹配
    try-catch的用法:try{//可能抛出异常的语句}catch(exceptionTypevariable){//处理异常的语句}我们还遗留下一个问题,就是catch关键字后边的exceptionTypevariable,这节就来详细分析一下。exceptionType是异常类型,它指明了当前的catch可以处理什么类型的异常;varia......
  • easyexcel导出csv格式怎么分sheet页
    在EasyExcel中,导出CSV格式并分多个Sheet页的方式与导出Excel格式基本相同。以下是使用EasyExcel导出CSV并分Sheet页的示例代码:publicvoidexportToCsvWithMultipleSheets(List<List<String>>data,List<String>sheetNames){try(CSVWritercsvWriter=new......
  • nacos服务多级存储模型
       ......
  • 使用EasyExcel实现无模板、全自定义Excel导出
    1需求背景最近公司需要做一个动态字段的Excel导出,大致的样式如下:实体类如下://部门实体类publicclassDepartment{privateStringcompanyName;privateStringname;privateStringfullName;privateStringleaderName;privateStringbusiness;......
  • 多级缓存-Redis缓存预热
            ......
  • 使用EasyExcel下载文件时,前端获取不到后端返回的文件名,无法下载到本地
    【问题描述】使用EasyExcel下载文件时,前端获取不到后端返回的文件名,无法下载到本地 【原因分析】实际上文件的流后端已经返回了,只是缺少了Content-Disposition属性返回,前端无法获取到文件名;privatestaticOutputStreamgetOutputStream(StringfileName,HttpServletRespon......