templates/example.controller.php
public function actionImport()
    {
        $field = [
            'fileImport' => 'File Import',
        ];
        
        $modelImport = DynamicModel::validateData($field, [
            [['fileImport'], 'required'],
            [['fileImport'], 'file', 'extensions'=>'xls,xlsx','maxSize'=>1024*1024],
        ]);

        if (Yii::$app->request->post()) {
            $modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport, 'fileImport');
            if ($modelImport->fileImport && $modelImport->validate()) {                                
                $inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName );
                $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
                $objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
                $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
                $baseRow = 2;
                while(!empty($sheetData[$baseRow]['A'])){
                    $model = new Mahasiswa();
                    $model->nama = (string)$sheetData[$baseRow]['B'];
                    $model->nim = (string)$sheetData[$baseRow]['C'];
                    $model->save(); 
                    //die(print_r($model->errors));
                    $baseRow++;
                }
                Yii::$app->getSession()->setFlash('success', 'Success');
            }
            else{
                Yii::$app->getSession()->setFlash('error', 'Error');
            }
        }
        
        return $this->redirect(['index']);
    }
public function actionImport()
    {
        $field = [
            'fileImport' => 'File Import',
        ];
        
        $modelImport = DynamicModel::validateData($field, [
            [['fileImport'], 'required'],
            [['fileImport'], 'file', 'extensions'=>'xls,xlsx','maxSize'=>1024*1024],
        ]);

        if (Yii::$app->request->post()) {
            $modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport, 'fileImport');
            if ($modelImport->fileImport && $modelImport->validate()) {                                
                $inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName );
                $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
                $objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
                $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
                $baseRow = 2;
                while(!empty($sheetData[$baseRow]['A'])){
                    $model = new Mahasiswa();
                    $model->nama = (string)$sheetData[$baseRow]['B'];
                    $model->nim = (string)$sheetData[$baseRow]['C'];
                    $model->save(); 
                    //die(print_r($model->errors));
                    $baseRow++;
                }
                Yii::$app->getSession()->setFlash('success', 'Success');
            }
            else{
                Yii::$app->getSession()->setFlash('error', 'Error');
            }
        }
        
        return $this->redirect(['index']);
    }
public function actionImport ( ) { $ field = [ 'fileImport' => 'File Import' , ] ; $ modelImport = DynamicModel :: validateData ( $ field , [ [ [ 'fileImport' ] , 'required' ] , [ [ 'fileImport' ] , 'file' , 'extensions' => 'xls,xlsx' , 'maxSize' => 1024 * 1024 ] , ] ) ; if ( Yii :: $ app -> request -> post ( ) ) { $ modelImport -> fileImport = \ yii \ web \ UploadedFile :: getInstance ( $ modelImport , 'fileImport' ) ; if ( $ modelImport -> fileImport && $ modelImport -> validate ( ) ) { $ inputFileType = \ PHPExcel_IOFactory :: identify ( $ modelImport -> fileImport -> tempName ) ; $ objReader = \ PHPExcel_IOFactory :: createReader ( $ inputFileType ) ; $ objPHPExcel = $ objReader -> load ( $ modelImport -> fileImport -> tempName ) ; $ sheetData = $ objPHPExcel -> getActiveSheet ( ) -> toArray ( null , true , true , true ) ; $ baseRow = 2 ; while ( ! empty ( $ sheetData [ $ baseRow ] [ 'A' ] ) ) { $ model = new Mahasiswa ( ) ; $ model -> nama = ( string ) $ sheetData [ $ baseRow ] [ 'B' ] ; $ model -> nim = ( string ) $ sheetData [ $ baseRow ] [ 'C' ] ; $ model -> save ( ) ; //die(print_r($model->errors)); $ baseRow ++ ; } Yii :: $ app -> getSession ( ) -> setFlash ( 'success' , 'Success' ) ; } else { Yii :: $ app -> getSession ( ) -> setFlash ( 'error' , 'Error' ) ; } } return $ this -> redirect ( [ 'index' ] ) ; }
/*
IMPORT WITH PHPEXCEL
/ * IMPORT WITH PHPEXCEL