delete(); $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); $excel = $reader->load(public_path().'public/uploads/import_comment.xlsx'); // $excel = $reader->load(public_path().'public/uploads/import_comment.xlsx'); // $excel = PHPExcel_IOFactory::load(public_path().'/uploads/import_comment.xlsx'); $nameArr = $excel->getSheetNames(); $sheetCount = $excel->getSheetCount(); $goodsIds = [ // 线上环境 10036, // 2.5kg 10035, // 5kg 10037, // 礼盒装 10041, // 2.5kg 10袋 10042, // 5kg 5袋 10040, // 5kg礼盒 4盒 // 预生产测试 // 10044, // 10043, // 10042, // 10038, // 10039, // 10040, ]; for ($i = 0; $i < $sheetCount; $i++) { $sheet = $excel->getSheet($i); $totalRows = $sheet->getHighestRow(); $totalColumn = $sheet->getHighestColumn(); \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($totalColumn); for ($j = 2; $j <= $totalRows; $j++) { $nickName = trim($this->removeEmojiChar($sheet->getCell('B' . $j)->getValue())); $content = trim($this->removeEmojiChar($sheet->getCell('C' . $j)->getValue())); if (!empty($nickName) && !empty($content)) { // 写入评价 \app\common\model\Comment::create([ 'score' => 5, 'content' => $content, 'user_id' => 0, 'nick_name' => $nickName, 'goods_id' => $goodsIds[$i], 'store_id' => 10001, 'is_public' => 1, 'status' => 1, 'create_time' => 0, 'update_time' => 0, ]); } } } } public function removeEmojiChar($str) { $mbLen = mb_strlen($str); $strArr = []; for ($i = 0; $i < $mbLen; $i++) { $mbSubstr = mb_substr($str, $i, 1, 'utf-8'); if (strlen($mbSubstr) >= 4) { continue; } $strArr[] = $mbSubstr; } return implode('', $strArr); } }