Advertisement
Guest User

import

a guest
Jan 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. public function actionImport2()
  2.     {
  3.         $model = new Category();
  4.         $id=Yii::$app->user->identity->id;
  5.         $inputFile= 'upload/import_category.xlsx';
  6.  
  7.         if ($model->load(Yii::$app->request->post())) {
  8.             $model->save();
  9.            
  10.            
  11.             $model->attributes=$_POST['Category'];
  12.             $itu=CUploadedFile::getInstance($model,'file_excel');
  13.             $path=Yii::app()->basepath.'/'.$inputFile.'';
  14.             $itu->saveAs($path);
  15.            
  16.             try{
  17.             $inputFileType = \PHPExcel_IOFactory::identify($itu);
  18.             $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
  19.             $objPHPExcel = $objReader->load($itu);
  20.             }catch(Exception $e)
  21.             {
  22.                 die('Error');
  23.             }
  24.             $sheet = $objPHPExcel->getSheet(0);
  25.             $highestRow = $sheet->getHighestRow();
  26.             $highestColumn = $sheet->getHighestColumn();
  27.             for($row=1; $row<=$highestRow; $row++)
  28.             {
  29.                 $rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE);
  30.                 if($row==1)
  31.                 {
  32.                     continue;
  33.                 }
  34.                 $cat = new Category();
  35.                 $cat_id= $rowData[0][0];
  36.                 $cat->name= $rowData[0][1];
  37.                 $cat->create_time= $rowData[0][2];
  38.                 $cat->update_time= $rowData[0][3];
  39.                 $cat->save();
  40.                 // print_r($cat->getErrors());
  41.  
  42.             }
  43.                
  44.            
  45.             return $this->redirect(['index']); 
  46.         }else{
  47.             return $this->render('import', [
  48.                 'model' => $model,
  49.             ]);
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement