getActiveSheet(); $rows = $sheet->toArray(null, true, true, true); if (empty($rows)) { throw new BusinessException('Excel 内容为空'); } // 取第一行表头 $headerRow = array_shift($rows); // 实例化映射器 $mapper = new FbLeadHeaderMapper(); // ① 如果有未知表头,会在这里抛出异常并终止处理 $colToField = $mapper->buildColumnFieldMap($headerRow); // ② 开始处理数据行 $insertData = []; $now = time(); foreach ($rows as $row) { $item = []; foreach ($colToField as $colIndex => $field) { $item[$field] = $row[$colIndex] ?? null; } // 补充你需要的其他字段 $item['source_file'] = basename($filePath); $item['created_at'] = $now; $item['updated_at'] = $now; $insertData[] = $item; } // ③ 批量入库(根据你的实际需求做去重等逻辑) if ($insertData) { Db::table('fb_leads')->insert($insertData); } } }