getActiveSheet(); // 获取最大行数和列数 $maxRow = $worksheet->getHighestRow(); //$maxColumn = $worksheet->getHighestColumn(); //$saveSpreadsheet = new Spreadsheet(); //$saveActiveWorksheet = $saveSpreadsheet->getActiveSheet(); // 遍历每一行和每一列 //$stuList = []; for ($row = 4; $row <= $maxRow; $row++) { $project = $worksheet->getCell('F' . $row)->getValue(); if(substr($project, 0, 1) == '='){ $project = $worksheet->getCell('E' . $row)->getValue(); } $subProject = $this->getProject($project); $worksheet->setCellValue('G' . $row, $subProject); } //$activeWorksheet = $spreadsheet->getActiveSheet(); //$activeWorksheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save($savePath); exit; } private function getProject($project) { $projects = ['田径','篮球','足球','排球','武术','体操']; $k = mt_rand(0, 5); $tmpProject = $projects[$k]; while($tmpProject == $project){ $k = mt_rand(0, 5); $tmpProject = $projects[$k]; echo "$tmpProject ==== $project\r\n"; } echo "--------------------------------\r\n"; return $tmpProject; } }