public function actionReadNumberExcel() { sleep(2); Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $request = Yii::$app->request; $fileB64 = $request->post('fileB64'); $content = $request->post('content'); $modelGroupId = $request->post('modelGroupId'); //delete all sms model by groupId //根据groupid删除各自短信模板 但是导入之后前台刷新会丢失groupid 会造成数据库数据累积 //表中含有accountid字段可以根据该字段删除所有模板 if ($modelGroupId != -1) { SmsModel::deleteAll(['groupId' => $modelGroupId]); } $file = base64_decode(substr($fileB64, strpos($fileB64, ";base64,") + 8)); $filePath = Yii::getAlias('@runtime') . '/numberExcel' . date('his') . '.csv'; file_put_contents($filePath, $file); //csv 由xlsx转换 第一列为号码 第二列替换param1 第三列param2 $phpReader = new \PHPExcel_Reader_Excel2007(); if (!$phpReader->canRead($filePath)) { $phpReader = new \PHPExcel_Reader_Excel5(); if (!$phpReader->canRead($filePath)) { $phpReader = new \PHPExcel_Reader_CSV(); if (!$phpReader->canRead($filePath)) { unlink($filePath); return ['fileError' => true]; } } } $phpExcel = $phpReader->load($filePath); $sheets = $phpExcel->getAllSheets(); $recordCount = -1; for ($si = 0; $si < sizeof($sheets); $si++) { $sheet = $sheets[$si]; $ingredientFinished = false; $rowCount = $sheet->getHighestRow(); $recordCount = $rowCount; $highestCol = $sheet->getHighestColumn(); // LogUtil::error(date('Y-m-d h:i:s') . ' $rowCount: ' . $rowCount . ' $highestCol: ' . $highestCol); $colCount = ord($highestCol) - 65; $currentGroupId = $this->guid(); // LogUtil::error(date('Y-m-d h:i:s') . ' $getAccountId: ' . $this->getAccountId()); for ($row = 1; $row <= $rowCount; $row++) { $realModel = $content; $number = ''; for ($col = 0; $col <= $colCount; $col++) { $val = $sheet->getCellByColumnAndRow($col, $row)->getValue(); $val = trim((string) $val); if ($val === '') { continue; } if ($col == 0) { $number = $val; } if ($col > 0) { $realModel = str_replace('%param' . $col . '%', $val, $realModel); } } //如果存在空白行则会把模板存入 $SmsModel = new SmsModel(); $SmsModel->groupId = $currentGroupId; $SmsModel->mobile = $number; $SmsModel->content = $realModel; $SmsModel->accountId = $this->getAccountId(); $SmsModel->save(); } } LogUtil::error(date('Y-m-d h:i:s') . ' $modelGroupId: ' . $currentGroupId); return ['recordCount' => $recordCount, 'modelGroupId' => $currentGroupId]; }