Menus

Sunday 30 August 2015

Importing data from Excel Sheet


How to load Phpexcell in yii2


 "phpoffice/phpexcel": "dev-develop"


Put this code in composor.json and do a composer update.

open project folder in teminal and do composer update

First create a excell sheet same fields as import table.



Controller Action




 public function actionImportexcel()
    {

         $inputFiles = 'uploads/branches.xlsx';

         
         try{
             $inputFileType = \PHPExcel_IOFactory::identify($inputFiles);
             $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
             $objPHPExcel = $objReader->load($inputFiles);
             
         } catch (Exception $ex) {             
             die('Error');
         }
                  
         $sheet = $objPHPExcel->getSheet(0);
         $highestRow = $sheet->getHighestRow();
         $highestColumn = $sheet->getHighestColumn();
         
         //$row is start 2 because first row assigned for heading.         
         for($row=2; $row<=$highestRow; ++$row)
         {                  
         
             $rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE);
             
            //save to branch table.
             $branch = new Branches;
             
             $branch->branch_id = $rowData[0][0];
             $branch->companies_company_id = $rowData[0][1];
             .
             .
             .
             $branch->save();
             
         }
    }




Video for Importing Excel Sheet















3 comments: