$fuc($data,$user); list($msg,$filename) = $return; if(! $status) return [false, $msg]; $headers = array_column($msg,'value'); Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []); return [true, ['file' => $filename]]; } private function getTableConfig($type = ""){ if(empty($type)) return []; //获取配置文件 $config = "excel." . $type . "Table"; return config($config) ?? []; } private function product($data,$user){ $config_array = $this->getTableConfig($data['type']); if(empty($config_array)) return [false, ['导入配置表头文件不存在','']]; //生成下载文件 $filename = "存货导入模板_" . time() . '.' . 'xlsx'; return [true, [$config_array, $filename]]; } //导入入口 public function importAll($data,$user){ // //不超时 // ini_set('max_execution_time', 0); // //内存设置 // ini_set('memory_limit', -1); // $reader = IOFactory::createReader('Xlsx'); // $reader->setReadDataOnly(true); // 只读取有数据的单元格 // $spreadsheet = $reader->load($data['file']); // dd($spreadsheet); // // 创建一个Reader对象 // $reader = IOFactory::createReader('Xlsx'); // 根据你的文件格式选择合适的reader // //// 加载Excel文件 // $spreadsheet = $reader->load($data['file']); // //// 获取第一个工作表 // $worksheet = $spreadsheet->getActiveSheet(); // //// 获取总行数 // $totalRows = $worksheet->getHighestRow();dd($totalRows); if(empty($data['type'])) return [false,'缺少导入类型,导入失败']; if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败']; if(empty($data['file'])) return [false,'导入文件不能为空']; try { $import = new ImportAll(); //设置导入人id $import->setCrt($user['id']); $import->setUser($user); $import->setType($data['type']); $other = $data; unset($other['file']); $import->setOtherParam($other); //导入 \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']); if($import->getMsg()) { $bool = $import->getIsLongText(); if($bool) { return [0, $import->getMsg()]; }else{ return [false, $import->getMsg()]; } } }catch (\Throwable $exception) { return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')']; } return [true, '']; } public function productImport($array, $user, $other_param){ $upload = $array[0]; list($status, $msg) = $this->compareTableAndReturn($upload, $other_param); if(! $status) return [false, $msg]; $table_config = $msg; // 去除表头 unset($array[0]); if(empty($array)) return [false, '导入数据不能为空']; list($array, $error) = $this->checkProduct($array, $table_config); if(empty($array)) return [false, '导入数据不能为空']; try{ DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false, $e->getMessage() . $e->getLine() . $e->getCode()]; } return [true, '']; } private function checkProduct($array, $table_config){ $error = []; foreach ($array as $key => $value){ $line = '第' . $key . '行'; $rowData = array_filter($value); if (empty($rowData)) { unset($array[$key]); } else{ foreach ($value as $k => $v){ $row = '第' . $k . '列'; $tmp_v = trim($v); if(! isset($table_config[$k])){ $error[] = $line . $row . "数据不存在"; }else{ $table_tmp = $table_config[$k] ?? []; if(! empty($table_tmp['required']) && empty($tmp_v)) $error[] = $line . $table_tmp['value'] . "必填"; if(! empty($table_tmp['unique'])) } $value[$k] = trim($v); } } } } //模板校验 private function compareTableAndReturn($upload, $param){ if(empty($upload)) return [false, '表头不能为空']; $config_array = $this->getTableConfig($param['type']); if(empty($config_array)) return [false, '导入配置表头文件不存在']; foreach ($config_array as $key => $value){ $key_position = $key + 1; if(! isset($upload[$key])) return [false, "第" . $key_position . "列表头缺失"]; $tmp_v = trim($upload[$key]); if($tmp_v != $value['value']) return [false, "第" . $key_position . "列表头与模板不符合,请重新下载模板"]; } return [true, $config_array]; } }