RuoYi ExcelUtil 工具类深度优化:ruoyi框架execl导出从单父子列表到多父子列表的全方位升级实践(最后附完整源码)

90 阅读23分钟

 

在企业级开发中,Excel 导出功能常需处理复杂的主子列表结构(如订单信息 + 商品明细 + 物流记录),而 RuoYi 框架原生 ExcelUtil 仅支持单个子列表,且存在表头合并异常、列宽失效等痛点。本文基于修改后的 ExcelUtil 代码,对比原版深入解析核心优化点,为复杂 Excel 导出场景提供完整解决方案。

一、优化背景:原版工具类核心痛点

在处理多子列表导出场景时,原版 ExcelUtil 暴露的问题日益明显,主要集中在:

1.仅通过 subFields(单个子列表字段)和 subMethod(单个子列表 getter 方法)存储子列表信息,无法支持多个子列表(如 “学生信息 + 班级信息等多个子列表”)。

​编辑

2.标题合并范围不准确无法覆盖 “主表字段列 + 所有子列表字段列”,导致标题显示不全。

3.未为基础字段和子列表字段单独设置列宽。

二、核心优化点对比分析

修改后的 ExcelUtil 围绕 “多子列表支持” 和 “细节体验优化” 两大方向,对原版代码进行了 7 处关键修改,以下为具体对比:

1. 数据结构重构:新增多子列表信息封装(核心架构升级)

原版问题:

仅通过两个独立变量存储单个子列表信息,多子列表场景下会被覆盖:

// 原版:仅支持单个子列表
private Method subMethod; // 单个子列表的getter方法
private List<Field> subFields; // 单个子列表的字段集合

修改后实现:

新增 SubListInfo 内部类封装单个子列表的完整配置,通过 subListInfos 集合存储所有子列表,支持任意数量子列表:

// 新增:多子列表信息存储集合
private List<SubListInfo> subListInfos = new ArrayList<>();

// 子列表信息封装类(单个子列表的完整配置)
private static class SubListInfo {
    Field parentField;    // 主表中对应的子列表字段(如 List<ReserveSourceVo>)
    Excel parentExcel;    // 主表字段上的@Excel注解(控制父表头显示)
    Method subMethod;     // 子列表的getter方法
    List<Field> subFields;// 子表中的导出字段(带@Excel注解)
    int subFieldCount;    // 子表字段数量(用于表头合并计算)
}

优化价值

  • 每个子列表独立封装,避免字段混淆;
  • 建立 “主表字段 - 子表字段 - 表头配置” 的关联关系,为后续表头合并、数据填充提供基础。

2. 子列表初始化逻辑:从 “单字段赋值” 到 “多列表批量添加”

原版问题

addField 方法中,仅将单个子列表的字段赋值给 subFields,多子列表场景下后一个子列表会覆盖前一个:

// 原版:单列表初始化逻辑
if (Collection.class.isAssignableFrom(field.getType())) {
    subMethod = getSubMethod(field.getName(), clazz);
    ParameterizedType pt = (ParameterizedType) field.getGenericType();
    Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
    this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
}

修改后实现

遍历所有主表字段,若为集合类型(子列表),则创建 SubListInfo 对象并加入 subListInfos 集合,支持批量初始化多子列表:

// 修改后:多子列表批量初始化
if (Collection.class.isAssignableFrom(field.getType())) {
    SubListInfo subListInfo = new SubListInfo();
    subListInfo.parentField = field;       // 关联主表字段
    subListInfo.parentExcel = attr;         // 关联主表字段注解
    subListInfo.subMethod = getSubMethod(field.getName(), clazz); // 子列表getter方法
    // 获取子表导出字段
    ParameterizedType pt = (ParameterizedType) field.getGenericType();
    Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
    subListInfo.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
    subListInfo.subFieldCount = subListInfo.subFields.size(); // 记录子表字段数量
    subListInfos.add(subListInfo); // 加入多子列表集合
}

优化价值

  • 自动识别主表中所有子列表字段,无需手动配置;
  • 为每个子列表保存独立的字段数量、表头注解等信息,解决多子列表冲突问题。

3. 标题合并范围计算:修复多子列表列数统计错误

原版问题

计算标题合并的最后一列时,仅依赖 subFields.size() 统计单个子列表列数,多子列表场景下合并范围偏小:

// 原版:单列表列数计算
if (isSubList()) {
    titleLastCol = titleLastCol + subFields.size() - 1;
}

修改后实现

遍历 subListInfos 集合,累加所有子列表的字段数量,确保标题合并范围覆盖所有列:

// 修改后:多子列表列数计算
if (isSubList()) {
    // 累加所有子列表的字段数量
    for (SubListInfo subListInfo : subListInfos) {
        titleLastCol += subListInfo.subFieldCount - 1;
    }
}

优化价值

  • 标题合并范围准确覆盖 “主表字段列 + 所有子列表字段列”,避免标题显示不全。

4. 表头创建逻辑:双层表头合并与列宽生效(视觉体验优化)

原版问题

  • 基础字段文字放在子表头行,合并后文字空白(POI 仅保留合并区域第一个单元格内容);
  • 未为基础字段和子列表字段单独设置列宽,@Excel(width) 注解失效;
  • 子表头行未处理基础字段单元格,导致表头显示混乱。

修改后实现

重构 createSubHead 方法,分 “父表头行” 和 “子表头行” 两步处理,核心优化点如下:

  1. 父表头行:基础字段文字放在父表头行(合并区域第一个单元格),子列表父表头合并对应子列;
  2. 子表头行:基础字段单元格留空,子列表字段显示子表表头文字;
  3. 列宽生效:分别为基础字段和子列表字段应用 @Excel(width) 注解,无配置时使用默认宽(15);
  4. 跨行合并:通过 CellRangeAddress(parentRowNum, childRowNum, column, column) 实现基础字段跨双层表头合并。

关键代码片段:

// 父表头行:处理基础字段(文字放在父表头)
Cell cell = parentHeaderRow.createCell(column);
cell.setCellValue(attr.name()); // 基础字段文字放在合并区域第一个单元格
if (attr.width() > 0) {
    sheet.setColumnWidth(column, (int) (attr.width() * 256)); // 应用width注解
} else {
    sheet.setColumnWidth(column, (int) ((15 + 0.72) * 256)); // 默认宽
}

// 子表头行:基础字段单元格留空 + 跨行合并
Cell cell = childHeaderRow.createCell(column);
cell.setCellValue(""); // 子表头行留空
sheet.addMergedRegion(new CellRangeAddress(
        parentRowNum,  // 父表头行(合并起始行)
        childRowNum,   // 子表头行(合并结束行)
        column, column
));

优化价值

  • 子列表列宽按注解配置生效,表格布局清晰美观。

5. 子列表判断逻辑:从 “单列表” 到 “多列表” 适配

原版问题

isSubList 方法仅判断 subFields 是否非空,多子列表场景下判断不准确:

// 原版:单列表判断
public boolean isSubList() {
    return StringUtils.isNotNull(subFields) && subFields.size() > 0;
}

修改后实现

基于 subListInfos 集合是否非空判断,适配多子列表场景:

// 修改后:多列表判断
public boolean isSubList() {
    return !subListInfos.isEmpty();
}

// 新增:判断是否有非空子列表
public boolean isSubListValue(T vo) {
    if (subListInfos.isEmpty()) return false;
    // 遍历所有子列表,有一个非空则返回true
    for (SubListInfo subListInfo : subListInfos) {
        Collection<?> subList = getTargetValue(vo, subListInfo.parentField, subListInfo.parentExcel);
        if (subList != null && !subList.isEmpty()) return true;
    }
    return false;
}

优化价值

  • 子列表判断逻辑更精准,多子列表场景下不会误判;
  • 新增 isSubListValue 方法,支持判断是否存在非空子列表。

三、优化后功能验证:多子列表导出实战

以 “储备信息(主表)+ 企业信息(子表 1)+ 排污数据(子表 2)” 为例,验证优化后功能:

1. 实体类注解配置

// 主表实体
public class ReserveMainVo {
    // 基础字段(跨双层表头合并)
    @Excel(name = "序号", cellType = ColumnType.NUMERIC, width = 10, needMerge = true, sort = 0)
    private Integer serialNumber;

    // 子列表1:企业信息(父表头显示“企业信息”)
    @Excel(name = "企业信息", needMerge = true, sort = 1)
    private List<ReserveEnterpriseVo> enterpriseList;

    // 子列表2:排污数据(父表头显示“排污数据”)
    @Excel(name = "排污数据", needMerge = true, sort = 2)
    private List<ReservePollutantVo> pollutantList;
}

// 子表1:企业信息
public class ReserveEnterpriseVo {
    @Excel(name = "企业名称", width = 30, sort = 0)
    private String enterpriseName;
    @Excel(name = "统一信用码", width = 20, sort = 1)
    private String creditCode;
}

// 子表2:排污数据
public class ReservePollutantVo {
    @Excel(name = "化学需氧量", width = 15, sort = 0)
    private BigDecimal cod;
    @Excel(name = "氨氮", width = 15, sort = 1)
    private BigDecimal ammoniaNitrogen;
}

2. 导出效果

​编辑

  • 表头:“序号” 跨双层表头合并,“企业信息” 合并 “企业名称 + 统一信用码” 列,“排污数据” 合并 “化学需氧量 + 氨氮” 列;
  • 列宽:“序号” 宽 10、“企业名称” 宽 30,按注解配置生效;
  • 数据:主表字段(序号)合并所有子列表行,无空白行和空指针;
  • 多子列表:企业信息和排污数据两个子列表并行显示,数据无冲突。

四、总结:优化后的三大核心价值

  1. 架构升级:从 “单列表” 到 “多列表” 的底层架构重构,支持任意数量子列表,覆盖复杂业务场景;
  2. 体验优化:优化合并、列宽等细节问题,导出表格更专业美观;
  3. 向下兼容:保留原版 API 调用方式,现有单列表导出功能无需修改即可正常使用。

五、附完整代码

public class ExcelUtil<T>
{
    private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);

    public static final String FORMULA_REGEX_STR = "=|-|\+|@";

    public static final String[] FORMULA_STR = { "=", "-", "+", "@" };

    /**
     * 用于dictType属性数据存储,避免重复查缓存
     */
    public Map<String, String> sysDictMap = new HashMap<String, String>();

    /**
     * Excel sheet最大行数,默认65536
     */
    public static final int sheetSize = 65536;

    /**
     * 工作表名称
     */
    private String sheetName;

    /**
     * 导出类型(EXPORT:导出数据;IMPORT:导入模板)
     */
    private Type type;

    /**
     * 工作薄对象
     */
    private Workbook wb;

    /**
     * 工作表对象
     */
    private Sheet sheet;

    /**
     * 样式列表
     */
    private Map<String, CellStyle> styles;

    /**
     * 导入导出数据列表
     */
    private List<T> list;

    /**
     * 注解列表
     */
    private List<Object[]> fields;

    /**
     * 当前行号
     */
    private int rownum;

    /**
     * 标题
     */
    private String title;

    /**
     * 最大高度
     */
    private short maxHeight;

    /**
     * 合并后最后行数
     */
    private int subMergedLastRowNum = 0;

    /**
     * 合并后开始行数
     */
    private int subMergedFirstRowNum = 1;

    /**
     * 对象的子列表方法
     */
    private Method subMethod;

    /**
     * 对象的子列表属性
     */
    private List<Field> subFields;

    /**
     * 统计列表
     */
    private Map<Integer, Double> statistics = new HashMap<Integer, Double>();

    /**
     * 数字格式
     */
    private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
    // -------------------------- 新增:多子列表信息存储 --------------------------
    /** 子列表信息集合(支持多个子列表) */
    private List<SubListInfo> subListInfos = new ArrayList<>();

    /** 子列表信息封装类 */
    private static class SubListInfo {
        Field parentField; // 父类中的子列表字段(如List<ReserveSourceVo>)
        Excel parentExcel; // 父类字段上的@Excel注解
        Method subMethod;  // 子列表的getter方法
        List<Field> subFields; // 子VO中的字段(带@Excel注解)
        int subFieldCount; // 子VO的字段数量(用于表头合并)
    }

    /**
     * 实体对象
     */
    public Class<T> clazz;

    /**
     * 需要显示列属性
     */
    public String[] includeFields;

    /**
     * 需要排除列属性
     */
    public String[] excludeFields;

    public ExcelUtil(Class<T> clazz)
    {
        this.clazz = clazz;
    }

    /**
     * 仅在Excel中显示列属性
     *
     * @param fields 列属性名 示例[单个"name"/多个"id","name"]
     */
    public void showColumn(String... fields)
    {
        this.includeFields = fields;
    }

    /**
     * 隐藏Excel中列属性
     *
     * @param fields 列属性名 示例[单个"name"/多个"id","name"]
     */
    public void hideColumn(String... fields)
    {
        this.excludeFields = fields;
    }

    public void init(List<T> list, String sheetName, String title, Type type)
    {
        if (list == null)
        {
            list = new ArrayList<T>();
        }
        this.list = list;
        this.sheetName = sheetName;
        this.type = type;
        this.title = title;
        createExcelField();
        createWorkbook();
        createTitle();
        createSubHead();
    }

    /**
     * 创建excel第一行标题
     */
    public void createTitle()
    {
        if (StringUtils.isNotEmpty(title))
        {
            int titleLastCol = this.fields.size() - 1;
            // 修复子列表列数计算逻辑(原逻辑依赖subFields,改为从subListInfos计算)
            if (isSubList())
            {
                // 累加所有子列表的字段数量
                for (SubListInfo subListInfo : subListInfos) {
                    titleLastCol += subListInfo.subFieldCount - 1;
                }
            }
            Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0);
            titleRow.setHeightInPoints(30);
            Cell titleCell = titleRow.createCell(0);
            titleCell.setCellStyle(styles.get("title"));
            titleCell.setCellValue(title);
            sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, titleLastCol));
        }
    }

    /**
     * 创建对象的子列表名称
     */
    public void createSubHead()
    {
        if (!subListInfos.isEmpty()) {
            // 记录基础字段(如序号)的列索引,用于合并
            Map<String, Integer> baseFieldColumns = new HashMap<>();

            // 1. 创建父表头行(第一层,合并区域的第一个单元格)
            Row parentHeaderRow = sheet.createRow(rownum);
            int parentRowNum = rownum; // 父表头行号
            int column = 0;

            for (Object[] objects : fields) {
                Field field = (Field) objects[0];
                Excel attr = (Excel) objects[1];
                boolean isSubListField = false;

                // 处理子列表字段(父表头显示子列表名称)
                for (SubListInfo subListInfo : subListInfos) {
                    if (subListInfo.parentField == field) {
                        Cell cell = parentHeaderRow.createCell(column);
                        cell.setCellValue(subListInfo.parentExcel.name()); // 子列表父表头文字
                        cell.setCellStyle(styles.get(String.format("header_%s_%s",
                                subListInfo.parentExcel.headerColor(),
                                subListInfo.parentExcel.headerBackgroundColor())));
                        // 合并子列表父表头的列(同一行内)
                        if (subListInfo.subFieldCount > 1) {
                            sheet.addMergedRegion(new CellRangeAddress(
                                    parentRowNum, parentRowNum,
                                    column, column + subListInfo.subFieldCount - 1
                            ));
                        }
                        column += subListInfo.subFieldCount;
                        isSubListField = true;
                        break;
                    }
                }

                // 处理基础字段(如序号、储备时间)
                if (!isSubListField) {
                    // 关键修复1:文字放在父表头行(合并区域的第一个单元格)
                    Cell cell = parentHeaderRow.createCell(column);
                    cell.setCellValue(attr.name()); // 基础字段文字放在父表头
                    cell.setCellStyle(styles.get(String.format("header_%s_%s",
                            attr.headerColor(), attr.headerBackgroundColor())));
                    if (attr.width() > 0) {
                        sheet.setColumnWidth(column, (int) (attr.width() * 256));
                    } else {
                        // 提供默认宽度,避免未设置时过窄
                        sheet.setColumnWidth(column, (int) ((15 + 0.72) * 256));
                    }
                    // 记录列索引,用于后续跨行合并
                    baseFieldColumns.put(attr.name(), column);
                    column++;
                }
            }

            // 2. 创建子表头行(第二层)
            rownum++; // 行号移动到子表头行
            Row childHeaderRow = sheet.createRow(rownum);
            int childRowNum = rownum;
            column = 0;

            for (Object[] objects : fields) {
                Field field = (Field) objects[0];
                Excel attr = (Excel) objects[1];
                boolean isSubListField = false;

                // 处理子列表的子表头(显示子VO字段名称)
                for (SubListInfo subListInfo : subListInfos) {
                    if (subListInfo.parentField == field) {
                        for (Field subField : subListInfo.subFields) {
                            Excel subExcel = subField.getAnnotation(Excel.class);
                            Cell cell = childHeaderRow.createCell(column);
                            cell.setCellValue(subExcel.name()); // 子表头文字
                            cell.setCellStyle(styles.get(String.format("header_%s_%s",
                                    subExcel.headerColor(), subExcel.headerBackgroundColor())));
                            // 关键修复3:子列表字段同样应用width属性
                            if (subExcel.width() > 0) {
                                sheet.setColumnWidth(column, (int) (subExcel.width() * 256));
                            }
                            column++;
                        }
                        isSubListField = true;
                        break;
                    }
                }

                // 处理基础字段的子表头行
                if (!isSubListField) {
                    // 关键修复2:子表头行的基础字段单元格留空(文字已在父表头行)
                    Cell cell = childHeaderRow.createCell(column);
                    cell.setCellValue(""); // 子表头行留空
                    cell.setCellStyle(styles.get(String.format("header_%s_%s",
                            attr.headerColor(), attr.headerBackgroundColor())));
                    if (attr.width() > 0) {
                        sheet.setColumnWidth(column, (int) (attr.width() * 256));
                    }
                    // 执行跨行合并(父表头行 + 子表头行)
                    if (baseFieldColumns.containsKey(attr.name())) {
                        int colIndex = baseFieldColumns.get(attr.name());
                        // 检查是否已合并,避免重复
                        boolean isMerged = false;
                        for (CellRangeAddress region : sheet.getMergedRegions()) {
                            if (region.getFirstRow() == parentRowNum &&
                                    region.getLastRow() == childRowNum &&
                                    region.getFirstColumn() == colIndex &&
                                    region.getLastColumn() == colIndex) {
                                isMerged = true;
                                break;
                            }
                        }
                        if (!isMerged) {
                            sheet.addMergedRegion(new CellRangeAddress(
                                    parentRowNum,  // 合并区域的第一个单元格(父表头行)
                                    childRowNum,   // 合并区域的最后一个单元格(子表头行)
                                    colIndex,
                                    colIndex
                            ));
                        }
                    }
                    column++;
                }
            }
            rownum++; // 移动到数据行
        }
    }

    /**
     * 对excel表单默认第一个索引名转换成list
     * 
     * @param is 输入流
     * @return 转换后集合
     */
    public List<T> importExcel(InputStream is)
    {
        return importExcel(is, 0);
    }

    /**
     * 对excel表单默认第一个索引名转换成list
     * 
     * @param is 输入流
     * @param titleNum 标题占用行数
     * @return 转换后集合
     */
    public List<T> importExcel(InputStream is, int titleNum)
    {
        List<T> list = null;
        try
        {
            list = importExcel(StringUtils.EMPTY, is, titleNum);
        }
        catch (Exception e)
        {
            log.error("导入Excel异常{}", e.getMessage());
            throw new UtilException(e.getMessage());
        }
        finally
        {
            IOUtils.closeQuietly(is);
        }
        return list;
    }

    /**
     * 对excel表单指定表格索引名转换成list
     * 
     * @param sheetName 表格索引名
     * @param titleNum 标题占用行数
     * @param is 输入流
     * @return 转换后集合
     */
    public List<T> importExcel(String sheetName, InputStream is, int titleNum) throws Exception
    {
        this.type = Type.IMPORT;
        this.wb = WorkbookFactory.create(is);
        List<T> list = new ArrayList<T>();
        // 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
        Sheet sheet = StringUtils.isNotEmpty(sheetName) ? wb.getSheet(sheetName) : wb.getSheetAt(0);
        if (sheet == null)
        {
            throw new IOException("文件sheet不存在");
        }
        boolean isXSSFWorkbook = !(wb instanceof HSSFWorkbook);
        Map<String, PictureData> pictures;
        if (isXSSFWorkbook)
        {
            pictures = getSheetPictures07((XSSFSheet) sheet, (XSSFWorkbook) wb);
        }
        else
        {
            pictures = getSheetPictures03((HSSFSheet) sheet, (HSSFWorkbook) wb);
        }
        // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
        int rows = sheet.getLastRowNum();
        if (rows > 0)
        {
            // 定义一个map用于存放excel列的序号和field.
            Map<String, Integer> cellMap = new HashMap<String, Integer>();
            // 获取表头
            Row heard = sheet.getRow(titleNum);
            for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
            {
                Cell cell = heard.getCell(i);
                if (StringUtils.isNotNull(cell))
                {
                    String value = this.getCellValue(heard, i).toString();
                    cellMap.put(value, i);
                }
                else
                {
                    cellMap.put(null, i);
                }
            }
            // 有数据时才处理 得到类的所有field.
            List<Object[]> fields = this.getFields();
            Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>();
            for (Object[] objects : fields)
            {
                Excel attr = (Excel) objects[1];
                Integer column = cellMap.get(attr.name());
                if (column != null)
                {
                    fieldsMap.put(column, objects);
                }
            }
            for (int i = titleNum + 1; i <= rows; i++)
            {
                // 从第2行开始取数据,默认第一行是表头.
                Row row = sheet.getRow(i);
                // 判断当前行是否是空行
                if (isRowEmpty(row))
                {
                    continue;
                }
                T entity = null;
                for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet())
                {
                    Object val = this.getCellValue(row, entry.getKey());

                    // 如果不存在实例则新建.
                    entity = (entity == null ? clazz.newInstance() : entity);
                    // 从map中得到对应列的field.
                    Field field = (Field) entry.getValue()[0];
                    Excel attr = (Excel) entry.getValue()[1];
                    // 取得类型,并根据对象类型设置值.
                    Class<?> fieldType = field.getType();
                    if (String.class == fieldType)
                    {
                        String s = Convert.toStr(val);
                        if (StringUtils.endsWith(s, ".0"))
                        {
                            val = StringUtils.substringBefore(s, ".0");
                        }
                        else
                        {
                            String dateFormat = field.getAnnotation(Excel.class).dateFormat();
                            if (StringUtils.isNotEmpty(dateFormat))
                            {
                                val = parseDateToStr(dateFormat, val);
                            }
                            else
                            {
                                val = Convert.toStr(val);
                            }
                        }
                    }
                    else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
                    {
                        val = Convert.toInt(val);
                    }
                    else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
                    {
                        val = Convert.toLong(val);
                    }
                    else if (Double.TYPE == fieldType || Double.class == fieldType)
                    {
                        val = Convert.toDouble(val);
                    }
                    else if (Float.TYPE == fieldType || Float.class == fieldType)
                    {
                        val = Convert.toFloat(val);
                    }
                    else if (BigDecimal.class == fieldType)
                    {
                        val = Convert.toBigDecimal(val);
                    }
                    else if (Date.class == fieldType)
                    {
                        if (val instanceof String)
                        {
                            val = DateUtils.parseDate(val);
                        }
                        else if (val instanceof Double)
                        {
                            val = DateUtil.getJavaDate((Double) val);
                        }
                    }
                    else if (Boolean.TYPE == fieldType || Boolean.class == fieldType)
                    {
                        val = Convert.toBool(val, false);
                    }
                    if (StringUtils.isNotNull(fieldType))
                    {
                        String propertyName = field.getName();
                        if (StringUtils.isNotEmpty(attr.targetAttr()))
                        {
                            propertyName = field.getName() + "." + attr.targetAttr();
                        }
                        if (StringUtils.isNotEmpty(attr.readConverterExp()))
                        {
                            val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
                        }
                        else if (StringUtils.isNotEmpty(attr.dictType()))
                        {
                            if (!sysDictMap.containsKey(attr.dictType() + val))
                            {
                                String dictValue = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
                                sysDictMap.put(attr.dictType() + val, dictValue);
                            }
                            val = sysDictMap.get(attr.dictType() + val);
                        }
                        else if (!attr.handler().equals(ExcelHandlerAdapter.class))
                        {
                            val = dataFormatHandlerAdapter(val, attr, null);
                        }
                        else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures))
                        {
                            PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
                            if (image == null)
                            {
                                val = "";
                            }
                            else
                            {
                                byte[] data = image.getData();
                                val = FileUtils.writeImportBytes(data);
                            }
                        }
                        ReflectUtils.invokeSetter(entity, propertyName, val);
                    }
                }
                list.add(entity);
            }
        }
        return list;
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param list 导出数据集合
     * @param sheetName 工作表的名称
     * @return 结果
     */
    public AjaxResult exportExcel(List<T> list, String sheetName)
    {
        return exportExcel(list, sheetName, StringUtils.EMPTY);
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param list 导出数据集合
     * @param sheetName 工作表的名称
     * @param title 标题
     * @return 结果
     */
    public AjaxResult exportExcel(List<T> list, String sheetName, String title)
    {
        this.init(list, sheetName, title, Type.EXPORT);
        return exportExcel();
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param response 返回数据
     * @param list 导出数据集合
     * @param sheetName 工作表的名称
     * @return 结果
     */
    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)
    {
        exportExcel(response, list, sheetName, StringUtils.EMPTY);
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param response 返回数据
     * @param list 导出数据集合
     * @param sheetName 工作表的名称
     * @param title 标题
     * @return 结果
     */
    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
    {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        this.init(list, sheetName, title, Type.EXPORT);
        exportExcel(response);
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param sheetName 工作表的名称
     * @return 结果
     */
    public AjaxResult importTemplateExcel(String sheetName)
    {
        return importTemplateExcel(sheetName, StringUtils.EMPTY);
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param sheetName 工作表的名称
     * @param title 标题
     * @return 结果
     */
    public AjaxResult importTemplateExcel(String sheetName, String title)
    {
        this.init(null, sheetName, title, Type.IMPORT);
        return exportExcel();
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param sheetName 工作表的名称
     * @return 结果
     */
    public void importTemplateExcel(HttpServletResponse response, String sheetName)
    {
        importTemplateExcel(response, sheetName, StringUtils.EMPTY);
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @param sheetName 工作表的名称
     * @param title 标题
     * @return 结果
     */
    public void importTemplateExcel(HttpServletResponse response, String sheetName, String title)
    {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        this.init(null, sheetName, title, Type.IMPORT);
        exportExcel(response);
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @return 结果
     */
    public void exportExcel(HttpServletResponse response)
    {
        try
        {
            writeSheet();
            wb.write(response.getOutputStream());
        }
        catch (Exception e)
        {
            log.error("导出Excel异常{}", e.getMessage());
        }
        finally
        {
            IOUtils.closeQuietly(wb);
        }
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     * 
     * @return 结果
     */
    public AjaxResult exportExcel()
    {
        OutputStream out = null;
        try
        {
            writeSheet();
            String filename = encodingFilename(sheetName);
            out = new FileOutputStream(getAbsoluteFile(filename));
            wb.write(out);
            return AjaxResult.success(filename);
        }
        catch (Exception e)
        {
            log.error("导出Excel异常{}", e.getMessage());
            throw new UtilException("导出Excel失败,请联系网站管理员!");
        }
        finally
        {
            IOUtils.closeQuietly(wb);
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 创建写入数据到Sheet
     */
    public void writeSheet()
    {
        int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize));
        for (int index = 0; index < sheetNo; index++) {
            createSheet(sheetNo, index);

            // 表头创建逻辑已在createSubHead中完成,此处直接处理数据
            if (Type.EXPORT.equals(type)) {
                fillExcelData(index, null);
                addStatisticsRow();
            }
        }
    }

    /**
     * 填充excel数据
     * 
     * @param index 序号
     * @param row 单元格行
     */
    @SuppressWarnings("unchecked")
    public void fillExcelData(int index, Row row)
    {
        int startNo = index * sheetSize;
        int endNo = Math.min(startNo + sheetSize, list.size());
        // 关键修复2:数据行从当前rownum开始,不额外加1
        int currentRowNum = rownum;

        for (int i = startNo; i < endNo; i++) {
            T vo = list.get(i);
            int column = 0;
            int maxSubListSize = getMaxSubListSize(vo);
            int mergeStartRow = currentRowNum; // 当前主记录的起始行
            int mergeEndRow = currentRowNum + maxSubListSize - 1; // 当前主记录的结束行

            // 提前创建所有需要用到的行(避免getRow返回null)
            ensureSubRows(sheet, mergeStartRow, mergeEndRow);

            for (Object[] os : fields) {
                Field field = (Field) os[0];
                Excel excel = (Excel) os[1];
                boolean isSubListField = false;

                // 处理每个子列表的数据
                for (SubListInfo subListInfo : subListInfos) {
                    if (subListInfo.parentField == field) {
                        try {
                            Collection<?> subList = (Collection<?>) getTargetValue(vo, field, excel);
                            if (subList == null) subList = new ArrayList<>();

                            int subIndex = 0;
                            for (Object subVo : subList) {
                                Row subRow = sheet.getRow(mergeStartRow + subIndex);
                                int subColumn = column;
                                for (Field subField : subListInfo.subFields) {
                                    Excel subExcel = subField.getAnnotation(Excel.class);
                                    addCell(subExcel, subRow, (T) subVo, subField, subColumn++);
                                }
                                subIndex++;
                            }
                            column += subListInfo.subFieldCount;
                        } catch (Exception e) {
                            log.error("填充子列表数据失败", e);
                        }
                        isSubListField = true;
                        break;
                    }
                }

                // 处理非子列表字段(修复合并范围)
                if (!isSubListField) {
                    // 关键修复1:使用mergeStartRow行填充数据(合并区域的第一行)
                    Row mainRow = sheet.getRow(mergeStartRow);
                    addCell(excel, mainRow, vo, field, column);

                    // 关键修复2:合并范围使用mergeStartRow和mergeEndRow(正确覆盖当前主记录所有行)
                    if (maxSubListSize > 1 && excel.needMerge()) {
                        // 避免重复合并
                        boolean regionExists = false;
                        List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
                        for (CellRangeAddress region : mergedRegions) {
                            if (region.getFirstRow() == mergeStartRow
                                    && region.getLastRow() == mergeEndRow
                                    && region.getFirstColumn() == column
                                    && region.getLastColumn() == column) {
                                regionExists = true;
                                break;
                            }
                        }
                        if (!regionExists) {
                            sheet.addMergedRegion(new CellRangeAddress(
                                    mergeStartRow,   // 正确的起始行
                                    mergeEndRow,     // 正确的结束行
                                    column,
                                    column
                            ));
                        }
                    }
                    column++;
                }
            }
            currentRowNum += maxSubListSize;
        }
    }
    /**
     * 确保从startRow到endRow的所有行都已创建(避免getRow返回null)
     */
    private void ensureSubRows(Sheet sheet, int startRow, int endRow) {
        if (startRow > endRow) {
            return; // 无效范围直接返回
        }
        // 从起始行到结束行,逐个检查并创建行
        for (int i = startRow; i <= endRow; i++) {
            if (sheet.getRow(i) == null) {
                sheet.createRow(i); // 若行为null则创建
            }
        }
    }
    // -------------------------- 新增:获取所有子列表的最大行数(用于合并单元格) --------------------------
    private int getMaxSubListSize(T vo) {
        int maxSize = 1;
        for (SubListInfo subListInfo : subListInfos) {
            try {
                Collection<?> subList = (Collection<?>) getTargetValue(vo, subListInfo.parentField, subListInfo.parentExcel);
                if (subList != null) {
                    maxSize = Math.max(maxSize, subList.size());
                }
            } catch (Exception e) {
                log.error("获取子列表大小失败", e);
            }
        }
        return maxSize;
    }

    /**
     * 获取子列表最大数
     */
    private int getCurrentMaxSubListSize(T vo)
    {
        int maxSubListSize = 1;
        for (Object[] os : fields)
        {
            Field field = (Field) os[0];
            if (Collection.class.isAssignableFrom(field.getType()))
            {
                try
                {
                    Collection<?> subList = (Collection<?>) getTargetValue(vo, field, (Excel) os[1]);
                    if (subList != null && !subList.isEmpty())
                    {
                        maxSubListSize = Math.max(maxSubListSize, subList.size());
                    }
                }
                catch (Exception e)
                {
                    log.error("获取集合大小失败", e);
                }
            }
        }
        return maxSubListSize;
    }

    /**
     * 创建表格样式
     * 
     * @param wb 工作薄对象
     * @return 样式列表
     */
    private Map<String, CellStyle> createStyles(Workbook wb)
    {
        // 写入各条记录,每条记录对应excel表中的一行
        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
        CellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        Font titleFont = wb.createFont();
        titleFont.setFontName("Arial");
        titleFont.setFontHeightInPoints((short) 16);
        titleFont.setBold(true);
        style.setFont(titleFont);
        DataFormat dataFormat = wb.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("@"));
        styles.put("title", style);

        style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        Font dataFont = wb.createFont();
        dataFont.setFontName("Arial");
        dataFont.setFontHeightInPoints((short) 10);
        style.setFont(dataFont);
        styles.put("data", style);

        style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        Font totalFont = wb.createFont();
        totalFont.setFontName("Arial");
        totalFont.setFontHeightInPoints((short) 10);
        style.setFont(totalFont);
        styles.put("total", style);

        styles.putAll(annotationHeaderStyles(wb, styles));

        styles.putAll(annotationDataStyles(wb));

        return styles;
    }

    /**
     * 根据Excel注解创建表格头样式
     * 
     * @param wb 工作薄对象
     * @return 自定义样式列表
     */
    private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, CellStyle> styles)
    {
        Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
        for (Object[] os : fields)
        {
            Excel excel = (Excel) os[1];
            String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor());
            if (!headerStyles.containsKey(key))
            {
                CellStyle style = wb.createCellStyle();
                style.cloneStyleFrom(styles.get("data"));
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                style.setFillForegroundColor(excel.headerBackgroundColor().index);
                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                Font headerFont = wb.createFont();
                headerFont.setFontName("Arial");
                headerFont.setFontHeightInPoints((short) 10);
                headerFont.setBold(true);
                headerFont.setColor(excel.headerColor().index);
                style.setFont(headerFont);
                // 设置表格头单元格文本形式
                DataFormat dataFormat = wb.createDataFormat();
                style.setDataFormat(dataFormat.getFormat("@"));
                headerStyles.put(key, style);
            }
        }
        return headerStyles;
    }

    /**
     * 根据Excel注解创建表格列样式
     * 
     * @param wb 工作薄对象
     * @return 自定义样式列表
     */
    private Map<String, CellStyle> annotationDataStyles(Workbook wb)
    {
        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
        for (Object[] os : fields)
        {
            Field field = (Field) os[0];
            Excel excel = (Excel) os[1];
            if (Collection.class.isAssignableFrom(field.getType()))
            {
                ParameterizedType pt = (ParameterizedType) field.getGenericType();
                Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
                List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
                for (Field subField : subFields)
                {
                    Excel subExcel = subField.getAnnotation(Excel.class);
                    annotationDataStyles(styles, subField, subExcel);
                }
            }
            else
            {
                annotationDataStyles(styles, field, excel);
            }
        }
        return styles;
    }

    /**
     * 根据Excel注解创建表格列样式
     * 
     * @param styles 自定义样式列表
     * @param field  属性列信息
     * @param excel  注解信息
     */
    public void annotationDataStyles(Map<String, CellStyle> styles, Field field, Excel excel)
    {
        String key = StringUtils.format("data_{}_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType(), excel.wrapText());
        if (!styles.containsKey(key))
        {
            CellStyle style = wb.createCellStyle();
            style.setAlignment(excel.align());
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            style.setBorderRight(BorderStyle.THIN);
            style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            style.setBorderLeft(BorderStyle.THIN);
            style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            style.setBorderTop(BorderStyle.THIN);
            style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            style.setBorderBottom(BorderStyle.THIN);
            style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            style.setFillForegroundColor(excel.backgroundColor().getIndex());
            style.setWrapText(excel.wrapText());
            Font dataFont = wb.createFont();
            dataFont.setFontName("Arial");
            dataFont.setFontHeightInPoints((short) 10);
            dataFont.setColor(excel.color().index);
            style.setFont(dataFont);
            if (ColumnType.TEXT == excel.cellType())
            {
                DataFormat dataFormat = wb.createDataFormat();
                style.setDataFormat(dataFormat.getFormat("@"));
            }
            styles.put(key, style);
        }
    }

    /**
     * 创建单元格
     */
    public Cell createHeadCell(Excel attr, Row row, int column)
    {
        Cell cell = row.createCell(column);
        cell.setCellValue(attr.name());
        setDataValidation(attr, row, column);
        cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
        return cell;
    }

    /**
     * 设置单元格信息
     * 
     * @param value 单元格值
     * @param attr 注解相关
     * @param cell 单元格信息
     */
    public void setCellVo(Object value, Excel attr, Cell cell)
    {
        if (ColumnType.STRING == attr.cellType() || ColumnType.TEXT == attr.cellType())
        {
            String cellValue = Convert.toStr(value);
            // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
            if (StringUtils.startsWithAny(cellValue, FORMULA_STR))
            {
                cellValue = RegExUtils.replaceFirst(cellValue, FORMULA_REGEX_STR, "\t$0");
            }
            if (value instanceof Collection && StringUtils.equals("[]", cellValue))
            {
                cellValue = StringUtils.EMPTY;
            }
            cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix());
        }
        else if (ColumnType.NUMERIC == attr.cellType())
        {
            if (StringUtils.isNotNull(value))
            {
                cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
            }
        }
        else if (ColumnType.IMAGE == attr.cellType())
        {
            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1);
            String imagePath = Convert.toStr(value);
            if (StringUtils.isNotEmpty(imagePath))
            {
                byte[] data = ImageUtils.getImage(imagePath);
                getDrawingPatriarch(cell.getSheet()).createPicture(anchor,
                        cell.getSheet().getWorkbook().addPicture(data, getImageType(data)));
            }
        }
    }

    /**
     * 获取画布
     */
    public static Drawing<?> getDrawingPatriarch(Sheet sheet)
    {
        if (sheet.getDrawingPatriarch() == null)
        {
            sheet.createDrawingPatriarch();
        }
        return sheet.getDrawingPatriarch();
    }

    /**
     * 获取图片类型,设置图片插入类型
     */
    public int getImageType(byte[] value)
    {
        String type = FileTypeUtils.getFileExtendName(value);
        if ("JPG".equalsIgnoreCase(type))
        {
            return Workbook.PICTURE_TYPE_JPEG;
        }
        else if ("PNG".equalsIgnoreCase(type))
        {
            return Workbook.PICTURE_TYPE_PNG;
        }
        return Workbook.PICTURE_TYPE_JPEG;
    }

    /**
     * 创建表格样式
     */
    public void setDataValidation(Excel attr, Row row, int column)
    {
        if (attr.name().indexOf("注:") >= 0)
        {
            sheet.setColumnWidth(column, 6000);
        }
        else
        {
            // 设置列宽
            sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
        }
        if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0 || attr.comboReadDict())
        {
            String[] comboArray = attr.combo();
            if (attr.comboReadDict())
            {
                if (!sysDictMap.containsKey("combo_" + attr.dictType()))
                {
                    String labels = DictUtils.getDictLabels(attr.dictType());
                    sysDictMap.put("combo_" + attr.dictType(), labels);
                }
                String val = sysDictMap.get("combo_" + attr.dictType());
                comboArray = StringUtils.split(val, DictUtils.SEPARATOR);
            }
            if (comboArray.length > 15 || StringUtils.join(comboArray).length() > 255)
            {
                // 如果下拉数大于15或字符串长度大于255,则使用一个新sheet存储,避免生成的模板下拉值获取不到
                setXSSFValidationWithHidden(sheet, comboArray, attr.prompt(), 1, 100, column, column);
            }
            else
            {
                // 提示信息或只能选择不能输入的列内容.
                setPromptOrValidation(sheet, comboArray, attr.prompt(), 1, 100, column, column);
            }
        }
    }

    /**
     * 添加单元格
     */
    public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
    {
        Cell cell = null;
        try {
            // 确保行对象不为null
            if (row == null) {
                int rowNum = (sheet.getLastRowNum() >= 0) ? sheet.getLastRowNum() + 1 : 0;
                row = sheet.createRow(rowNum);
            }

            row.setHeight(maxHeight);
            if (attr.isExport())
            {
                cell = row.createCell(column);
                // 关键修复3:删除旧单列表合并逻辑(isSubListValue、subMergedLastRowNum等)
                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText())));

                // 以下为原有数据填充逻辑,保持不变...
                Object value = getTargetValue(vo, field, attr);
                String dateFormat = attr.dateFormat();
                String readConverterExp = attr.readConverterExp();
                String separator = attr.separator();
                String dictType = attr.dictType();
                if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
                {
                    cell.setCellValue(parseDateToStr(dateFormat, value));
                }
                else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
                {
                    cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
                }
                else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value))
                {
                    if (!sysDictMap.containsKey(dictType + value))
                    {
                        String lable = convertDictByExp(Convert.toStr(value), dictType, separator);
                        sysDictMap.put(dictType + value, lable);
                    }
                    cell.setCellValue(sysDictMap.get(dictType + value));
                }
                else if (value instanceof BigDecimal && -1 != attr.scale())
                {
                    cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).doubleValue());
                }
                else if (!attr.handler().equals(ExcelHandlerAdapter.class))
                {
                    cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
                }
                else
                {
                    setCellVo(value, attr, cell);
                }
                addStatisticsData(column, Convert.toStr(value), attr);
            }
        }
        catch (Exception e)
        {
            log.error("导出Excel失败{}", e);
        }
        return cell;
    }

    /**
     * 设置 POI XSSFSheet 单元格提示或选择框
     * 
     * @param sheet 表单
     * @param textlist 下拉框显示的内容
     * @param promptContent 提示内容
     * @param firstRow 开始行
     * @param endRow 结束行
     * @param firstCol 开始列
     * @param endCol 结束列
     */
    public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
            int firstCol, int endCol)
    {
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
        CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
        DataValidation dataValidation = helper.createValidation(constraint, regions);
        if (StringUtils.isNotEmpty(promptContent))
        {
            // 如果设置了提示信息则鼠标放上去提示
            dataValidation.createPromptBox("", promptContent);
            dataValidation.setShowPromptBox(true);
        }
        // 处理Excel兼容性问题
        if (dataValidation instanceof XSSFDataValidation)
        {
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        }
        else
        {
            dataValidation.setSuppressDropDownArrow(false);
        }
        sheet.addValidationData(dataValidation);
    }

    /**
     * 设置某些列的值只能输入预制的数据,显示下拉框(兼容超出一定数量的下拉框).
     * 
     * @param sheet 要设置的sheet.
     * @param textlist 下拉框显示的内容
     * @param promptContent 提示内容
     * @param firstRow 开始行
     * @param endRow 结束行
     * @param firstCol 开始列
     * @param endCol 结束列
     */
    public void setXSSFValidationWithHidden(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, int firstCol, int endCol)
    {
        String hideSheetName = "combo_" + firstCol + "_" + endCol;
        Sheet hideSheet = wb.createSheet(hideSheetName); // 用于存储 下拉菜单数据
        for (int i = 0; i < textlist.length; i++)
        {
            hideSheet.createRow(i).createCell(0).setCellValue(textlist[i]);
        }
        // 创建名称,可被其他单元格引用
        Name name = wb.createName();
        name.setNameName(hideSheetName + "_data");
        name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textlist.length);
        DataValidationHelper helper = sheet.getDataValidationHelper();
        // 加载下拉列表内容
        DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data");
        // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
        // 数据有效性对象
        DataValidation dataValidation = helper.createValidation(constraint, regions);
        if (StringUtils.isNotEmpty(promptContent))
        {
            // 如果设置了提示信息则鼠标放上去提示
            dataValidation.createPromptBox("", promptContent);
            dataValidation.setShowPromptBox(true);
        }
        // 处理Excel兼容性问题
        if (dataValidation instanceof XSSFDataValidation)
        {
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        }
        else
        {
            dataValidation.setSuppressDropDownArrow(false);
        }

        sheet.addValidationData(dataValidation);
        // 设置hiddenSheet隐藏
        wb.setSheetHidden(wb.getSheetIndex(hideSheet), true);
    }

    /**
     * 解析导出值 0=男,1=女,2=未知
     * 
     * @param propertyValue 参数值
     * @param converterExp 翻译注解
     * @param separator 分隔符
     * @return 解析后值
     */
    public static String convertByExp(String propertyValue, String converterExp, String separator)
    {
        StringBuilder propertyString = new StringBuilder();
        String[] convertSource = converterExp.split(",");
        for (String item : convertSource)
        {
            String[] itemArray = item.split("=");
            if (StringUtils.containsAny(propertyValue, separator))
            {
                for (String value : propertyValue.split(separator))
                {
                    if (itemArray[0].equals(value))
                    {
                        propertyString.append(itemArray[1] + separator);
                        break;
                    }
                }
            }
            else
            {
                if (itemArray[0].equals(propertyValue))
                {
                    return itemArray[1];
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

    /**
     * 反向解析值 男=0,女=1,未知=2
     * 
     * @param propertyValue 参数值
     * @param converterExp 翻译注解
     * @param separator 分隔符
     * @return 解析后值
     */
    public static String reverseByExp(String propertyValue, String converterExp, String separator)
    {
        StringBuilder propertyString = new StringBuilder();
        String[] convertSource = converterExp.split(",");
        for (String item : convertSource)
        {
            String[] itemArray = item.split("=");
            if (StringUtils.containsAny(propertyValue, separator))
            {
                for (String value : propertyValue.split(separator))
                {
                    if (itemArray[1].equals(value))
                    {
                        propertyString.append(itemArray[0] + separator);
                        break;
                    }
                }
            }
            else
            {
                if (itemArray[1].equals(propertyValue))
                {
                    return itemArray[0];
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

    /**
     * 解析字典值
     * 
     * @param dictValue 字典值
     * @param dictType 字典类型
     * @param separator 分隔符
     * @return 字典标签
     */
    public static String convertDictByExp(String dictValue, String dictType, String separator)
    {
        return DictUtils.getDictLabel(dictType, dictValue, separator);
    }

    /**
     * 反向解析值字典值
     * 
     * @param dictLabel 字典标签
     * @param dictType 字典类型
     * @param separator 分隔符
     * @return 字典值
     */
    public static String reverseDictByExp(String dictLabel, String dictType, String separator)
    {
        return DictUtils.getDictValue(dictType, dictLabel, separator);
    }

    /**
     * 数据处理器
     * 
     * @param value 数据值
     * @param excel 数据注解
     * @return
     */
    public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
    {
        try
        {
            Object instance = excel.handler().newInstance();
            Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class });
            value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
        }
        catch (Exception e)
        {
            log.error("不能格式化数据 " + excel.handler(), e.getMessage());
        }
        return Convert.toStr(value);
    }

    /**
     * 合计统计信息
     */
    private void addStatisticsData(Integer index, String text, Excel entity)
    {
        if (entity != null && entity.isStatistics())
        {
            Double temp = 0D;
            if (!statistics.containsKey(index))
            {
                statistics.put(index, temp);
            }
            try
            {
                temp = Double.valueOf(text);
            }
            catch (NumberFormatException e)
            {
            }
            statistics.put(index, statistics.get(index) + temp);
        }
    }

    /**
     * 创建统计行
     */
    public void addStatisticsRow()
    {
        if (statistics.size() > 0)
        {
            Row row = sheet.createRow(sheet.getLastRowNum() + 1);
            Set<Integer> keys = statistics.keySet();
            Cell cell = row.createCell(0);
            cell.setCellStyle(styles.get("total"));
            cell.setCellValue("合计");

            for (Integer key : keys)
            {
                cell = row.createCell(key);
                cell.setCellStyle(styles.get("total"));
                cell.setCellValue(DOUBLE_FORMAT.format(statistics.get(key)));
            }
            statistics.clear();
        }
    }

    /**
     * 编码文件名
     */
    public String encodingFilename(String filename)
    {
        filename = UUID.randomUUID() + "_" + filename + ".xlsx";
        return filename;
    }

    /**
     * 获取下载路径
     * 
     * @param filename 文件名称
     */
    public String getAbsoluteFile(String filename)
    {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    /**
     * 获取bean中的属性值
     * 
     * @param vo 实体对象
     * @param field 字段
     * @param excel 注解
     * @return 最终的属性值
     * @throws Exception
     */
    private Object getTargetValue(T vo, Field field, Excel excel) throws Exception
    {
        field.setAccessible(true);
        Object o = field.get(vo);
        if (StringUtils.isNotEmpty(excel.targetAttr()))
        {
            String target = excel.targetAttr();
            if (target.contains("."))
            {
                String[] targets = target.split("[.]");
                for (String name : targets)
                {
                    o = getValue(o, name);
                }
            }
            else
            {
                o = getValue(o, target);
            }
        }
        return o;
    }

    /**
     * 以类的属性的get方法方法形式获取值
     * 
     * @param o
     * @param name
     * @return value
     * @throws Exception
     */
    private Object getValue(Object o, String name) throws Exception
    {
        if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name))
        {
            Class<?> clazz = o.getClass();
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            o = field.get(o);
        }
        return o;
    }

    /**
     * 得到所有定义字段
     */
    private void createExcelField()
    {
        this.fields = getFields();
        this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
        this.maxHeight = getRowHeight();
    }

    /**
     * 获取字段注解信息
     */
    public List<Object[]> getFields()
    {
        List<Object[]> fields = new ArrayList<Object[]>();
        List<Field> tempFields = new ArrayList<>();
        tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
        tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
        if (StringUtils.isNotEmpty(includeFields))
        {
            for (Field field : tempFields)
            {
                if (ArrayUtils.contains(this.includeFields, field.getName()) || field.isAnnotationPresent(Excels.class))
                {
                    addField(fields, field);
                }
            }
        }
        else if (StringUtils.isNotEmpty(excludeFields))
        {
            for (Field field : tempFields)
            {
                if (!ArrayUtils.contains(this.excludeFields, field.getName()))
                {
                    addField(fields, field);
                }
            }
        }
        else
        {
            for (Field field : tempFields)
            {
                addField(fields, field);
            }
        }
        return fields;
    }

    /**
     * 添加字段信息
     */
    public void addField(List<Object[]> fields, Field field)
    {
        // 单注解处理
        if (field.isAnnotationPresent(Excel.class)) {
            Excel attr = field.getAnnotation(Excel.class);
            if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
                fields.add(new Object[]{field, attr});
            }

            // 关键修改:如果是集合类型,添加到子列表信息集合(支持多个)
            if (Collection.class.isAssignableFrom(field.getType())) {
                SubListInfo subListInfo = new SubListInfo();
                subListInfo.parentField = field;
                subListInfo.parentExcel = attr;
                subListInfo.subMethod = getSubMethod(field.getName(), clazz);

                // 获取子VO的字段信息
                ParameterizedType pt = (ParameterizedType) field.getGenericType();
                Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
                subListInfo.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
                subListInfo.subFieldCount = subListInfo.subFields.size();

                subListInfos.add(subListInfo); // 加入集合,支持多个子列表
            }
        }

        // 多注解
        if (field.isAnnotationPresent(Excels.class))
        {
            Excels attrs = field.getAnnotation(Excels.class);
            Excel[] excels = attrs.value();
            for (Excel attr : excels)
            {
                if (StringUtils.isNotEmpty(includeFields))
                {
                    if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr())
                            && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
                    {
                        fields.add(new Object[] { field, attr });
                    }
                }
                else
                {
                    if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
                            && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
                    {
                        fields.add(new Object[] { field, attr });
                    }
                }
            }
        }
    }

    /**
     * 根据注解获取最大行高
     */
    public short getRowHeight()
    {
        double maxHeight = 0;
        for (Object[] os : this.fields)
        {
            Excel excel = (Excel) os[1];
            maxHeight = Math.max(maxHeight, excel.height());
        }
        return (short) (maxHeight * 20);
    }

    /**
     * 创建一个工作簿
     */
    public void createWorkbook()
    {
        this.wb = new SXSSFWorkbook(500);
        this.sheet = wb.createSheet();
        wb.setSheetName(0, sheetName);
        this.styles = createStyles(wb);
    }

    /**
     * 创建工作表
     * 
     * @param sheetNo sheet数量
     * @param index 序号
     */
    public void createSheet(int sheetNo, int index)
    {
        // 设置工作表的名称.
        if (sheetNo > 1 && index > 0)
        {
            this.sheet = wb.createSheet();
            this.createTitle();
            wb.setSheetName(index, sheetName + index);
        }
    }

    /**
     * 获取单元格值
     * 
     * @param row 获取的行
     * @param column 获取单元格列号
     * @return 单元格值
     */
    public Object getCellValue(Row row, int column)
    {
        if (row == null)
        {
            return row;
        }
        Object val = "";
        try
        {
            Cell cell = row.getCell(column);
            if (StringUtils.isNotNull(cell))
            {
                if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
                {
                    val = cell.getNumericCellValue();
                    if (DateUtil.isCellDateFormatted(cell))
                    {
                        val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换
                    }
                    else
                    {
                        if ((Double) val % 1 != 0)
                        {
                            val = new BigDecimal(val.toString());
                        }
                        else
                        {
                            val = new DecimalFormat("0").format(val);
                        }
                    }
                }
                else if (cell.getCellType() == CellType.STRING)
                {
                    val = cell.getStringCellValue();
                }
                else if (cell.getCellType() == CellType.BOOLEAN)
                {
                    val = cell.getBooleanCellValue();
                }
                else if (cell.getCellType() == CellType.ERROR)
                {
                    val = cell.getErrorCellValue();
                }

            }
        }
        catch (Exception e)
        {
            return val;
        }
        return val;
    }

    /**
     * 判断是否是空行
     * 
     * @param row 判断的行
     * @return
     */
    private boolean isRowEmpty(Row row)
    {
        if (row == null)
        {
            return true;
        }
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++)
        {
            Cell cell = row.getCell(i);
            if (cell != null && cell.getCellType() != CellType.BLANK)
            {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取Excel2003图片
     *
     * @param sheet 当前sheet对象
     * @param workbook 工作簿对象
     * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
     */
    public static Map<String, PictureData> getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook)
    {
        Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
        List<HSSFPictureData> pictures = workbook.getAllPictures();
        if (!pictures.isEmpty())
        {
            for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren())
            {
                HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
                if (shape instanceof HSSFPicture)
                {
                    HSSFPicture pic = (HSSFPicture) shape;
                    int pictureIndex = pic.getPictureIndex() - 1;
                    HSSFPictureData picData = pictures.get(pictureIndex);
                    String picIndex = anchor.getRow1() + "_" + anchor.getCol1();
                    sheetIndexPicMap.put(picIndex, picData);
                }
            }
            return sheetIndexPicMap;
        }
        else
        {
            return sheetIndexPicMap;
        }
    }

    /**
     * 获取Excel2007图片
     *
     * @param sheet 当前sheet对象
     * @param workbook 工作簿对象
     * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
     */
    public static Map<String, PictureData> getSheetPictures07(XSSFSheet sheet, XSSFWorkbook workbook)
    {
        Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
        for (POIXMLDocumentPart dr : sheet.getRelations())
        {
            if (dr instanceof XSSFDrawing)
            {
                XSSFDrawing drawing = (XSSFDrawing) dr;
                List<XSSFShape> shapes = drawing.getShapes();
                for (XSSFShape shape : shapes)
                {
                    if (shape instanceof XSSFPicture)
                    {
                        XSSFPicture pic = (XSSFPicture) shape;
                        XSSFClientAnchor anchor = pic.getPreferredSize();
                        CTMarker ctMarker = anchor.getFrom();
                        String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
                        sheetIndexPicMap.put(picIndex, pic.getPictureData());
                    }
                }
            }
        }
        return sheetIndexPicMap;
    }

    /**
     * 格式化不同类型的日期对象
     * 
     * @param dateFormat 日期格式
     * @param val 被格式化的日期对象
     * @return 格式化后的日期字符
     */
    public String parseDateToStr(String dateFormat, Object val)
    {
        if (val == null)
        {
            return "";
        }
        String str;
        if (val instanceof Date)
        {
            str = DateUtils.parseDateToStr(dateFormat, (Date) val);
        }
        else if (val instanceof LocalDateTime)
        {
            str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDateTime) val));
        }
        else if (val instanceof LocalDate)
        {
            str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDate) val));
        }
        else
        {
            str = val.toString();
        }
        return str;
    }

    /**
     * 是否有对象的子列表
     */
    public boolean isSubList()
    {
        return !subListInfos.isEmpty();
    }

    /**
     * 是否有对象的子列表,集合不为空
     */
    public boolean isSubListValue(T vo)
    {
        if (subListInfos.isEmpty()) {
            return false;
        }
        // 遍历所有子列表,只要有一个子列表非空则返回true
        for (SubListInfo subListInfo : subListInfos) {
            try {
                Collection<?> subList = (Collection<?>) getTargetValue(vo, subListInfo.parentField, subListInfo.parentExcel);
                if (subList != null && !subList.isEmpty()) {
                    return true;
                }
            } catch (Exception e) {
                log.error("获取子列表值失败", e);
            }
        }
        return false;
    }

    /**
     * 获取集合的值
     */
    public Collection<?> getListCellValue(Object obj)
    {
        // 多子列表场景下,如需获取第一个子列表数据,从subListInfos中获取
        if (!subListInfos.isEmpty()) {
            SubListInfo firstSubList = subListInfos.get(0);
            try {
                return (Collection<?>) getTargetValue((T) obj, firstSubList.parentField, firstSubList.parentExcel);
            } catch (Exception e) {
                log.error("获取子列表值失败", e);
            }
        }
        return new ArrayList<Object>();
    }

    /**
     * 获取对象的子列表方法
     * 
     * @param name 名称
     * @param pojoClass 类对象
     * @return 子列表方法
     */
    public Method getSubMethod(String name, Class<?> pojoClass)
    {
        StringBuffer getMethodName = new StringBuffer("get");
        getMethodName.append(name.substring(0, 1).toUpperCase());
        getMethodName.append(name.substring(1));
        Method method = null;
        try
        {
            method = pojoClass.getMethod(getMethodName.toString(), new Class[] {});
        }
        catch (Exception e)
        {
            log.error("获取对象异常{}", e.getMessage());
        }
        return method;
    }
}

e