diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 36c0beb..08ee21e 100755 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -817,16 +817,25 @@ class UserController extends BaseController $tempFile = $file->getRealPath(); $dataArray = (new FastExcel)->import($tempFile)->toArray(); - // 从 Excel 第一行获取表头 - $headers = []; - if (!empty($dataArray)) { - $headers = array_keys($dataArray[0]); - } - // 数据过滤,只能导入数据表有的字段 $tableName = $this->model->getTable(); $rowTableFieldByComment = (new CustomFormField)->getRowTableFieldsByComment($tableName); + // 获取 Excel 文件中的表头(从第一行数据中提取) + $excelHeaders = []; + if (!empty($dataArray)) { + $firstRow = reset($dataArray); + $excelHeaders = array_keys($firstRow); + } + + // 只保留 Excel 中存在的字段的表头信息 + $filteredHeaders = []; + foreach ($rowTableFieldByComment as $fieldName => $comment) { + if (in_array($comment, $excelHeaders)) { + $filteredHeaders[$fieldName] = $comment; + } + } + $list = []; $unmatchedUsers = []; // 记录未匹配到的用户 foreach ($dataArray as $key => $value) { @@ -891,7 +900,7 @@ class UserController extends BaseController // 构建返回结果 $result = [ - 'headers' => $headers, // 表头信息,直接使用 Excel 文件中的表头 + 'headers' => $filteredHeaders, // 表头信息,只包含 Excel 中存在的字段,键为字段名,值为中文注释 'list' => $list, 'matched_count' => count($list) - count($unmatchedUsers), 'unmatched_count' => count($unmatchedUsers),