Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionImport2()
- {
- $model = new Category();
- $id=Yii::$app->user->identity->id;
- $inputFile= 'upload/import_category.xlsx';
- if ($model->load(Yii::$app->request->post())) {
- $model->save();
- $model->attributes=$_POST['Category'];
- $itu=CUploadedFile::getInstance($model,'file_excel');
- $path=Yii::app()->basepath.'/'.$inputFile.'';
- $itu->saveAs($path);
- try{
- $inputFileType = \PHPExcel_IOFactory::identify($itu);
- $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
- $objPHPExcel = $objReader->load($itu);
- }catch(Exception $e)
- {
- die('Error');
- }
- $sheet = $objPHPExcel->getSheet(0);
- $highestRow = $sheet->getHighestRow();
- $highestColumn = $sheet->getHighestColumn();
- for($row=1; $row<=$highestRow; $row++)
- {
- $rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE);
- if($row==1)
- {
- continue;
- }
- $cat = new Category();
- $cat_id= $rowData[0][0];
- $cat->name= $rowData[0][1];
- $cat->create_time= $rowData[0][2];
- $cat->update_time= $rowData[0][3];
- $cat->save();
- // print_r($cat->getErrors());
- }
- return $this->redirect(['index']);
- }else{
- return $this->render('import', [
- 'model' => $model,
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement