diff --git a/app/Exports/CommonExport.php b/app/Exports/CommonExport.php index 7f06973..053dd22 100755 --- a/app/Exports/CommonExport.php +++ b/app/Exports/CommonExport.php @@ -77,7 +77,13 @@ class CommonExport implements FromCollection, WithStyles, WithColumnWidths, With } $index = 1; + // 第一遍:处理除了历史课程之外的所有字段 foreach ($this->fields as $field => $label) { + if (str_contains($field, 'history_courses')) { + // 跳过历史课程,稍后处理 + continue; + } + if (str_contains($field, 'users') && !str_contains($field, 'project_users')) { $this->hasUsersField = true; // 展开学员信息为多列 @@ -92,18 +98,24 @@ class CommonExport implements FromCollection, WithStyles, WithColumnWidths, With $this->expandedFields[$subField] = $subLabel; $index++; } - } elseif (str_contains($field, 'history_courses')) { + } else { + $this->expandedFields[$field] = $label; + $index++; + } + } + + // 第二遍:处理历史课程字段,放在最后 + foreach ($this->fields as $field => $label) { + if (str_contains($field, 'history_courses')) { $this->hasHistoryCoursesField = true; // 展开历史课程信息为多列 foreach (self::HISTORY_COURSES_SUB_COLUMNS as $subField => $subLabel) { $this->expandedFields[$subField] = $subLabel; $index++; } - } else { - $this->expandedFields[$field] = $label; - $index++; } } + $this->totalColumns = count($this->expandedFields); }