How To Load Phpexcel In Yii 2.0 Project?

I want to import/export data with PHPExcel, but i can’t load PHPExcel classes.

The classes of PHPExcel always have a prefix ‘phpexcel_’, Yii 2 seems don’t like it.

How to load PHPExcel into YII 2.0 project?

How can i use the PHPExcel library in a YII 2 project?

Could you give me a example?

thanks.

I have it with Composer:

Put this in the require bit:




 "phpoffice/phpexcel": "dev-develop"



and do a composer update.

Then just use it in your code:




$objPHPExcel = new PHPExcel();



thanks!

i have done as you said, but i have a error like this:

PHP Fatal Error – yii\base\ErrorException

Class ‘frontend\controllers\PHPExcel’ not found

use


$objPHPExcel = new \PHPExcel();

Thank you!

It’s works!

Can you give me an example of import/export data with phpexcel in yii2 project?

Thanks for sugestion, can you help giving example how to generate excel to browser using yii2. Thanks before

This is what it looks like in your view file. After it is install with composer, the extension will work exactly how it is described in the documentation. There is a lot of options for formatting your cells.




<?php


$objPHPExcel = new \PHPExcel();

 

 $sheet=0;

  

 $objPHPExcel->setActiveSheetIndex($sheet);

 

foreach ($foos as $foo) {  

	

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);

    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);

	

    $objPHPExcel->getActiveSheet()->setTitle($foo->bar)

	

     ->setCellValue('A1', 'Firstname')

     ->setCellValue('B1', 'Lastname');

	 

	 $row=2;

		

    $objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo->firstname); 

    $objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo->lastname);

		$row++ ;

		}

	

        header('Content-Type: application/vnd.ms-excel');

        $filename = "MyExcelReport_".date("d-m-Y-His").".xls";

	header('Content-Disposition: attachment;filename='.$filename .' ');

        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output'); 	



Thanks, will try it soon

I’ve tried several ways, but the response is always PHPExcel_IOFactory 'not found. Are you sure it works?

Ok I found the problem,

I think we should improve the documentation of the installation and use of the class.

Hi there!

I can use PHPExcel just like mentioned before


$objPHPExcel = new \PHPExcel;

However, I can’t get the content assist from eclipse working when I press ctrl+space.

For example when I’m tryng to type


$objPHPExcel->getActiveSheet()

if I press ctrl+space after “$objPHPExcel->” I don’t get any suggestions, probably meaning that content assist can’t see further into PHPExcel, as he sees other extensions;

Does anyone know how to sort this out?

tks in advance.

I have follow the same steps but every time it is giving error

PHP Fatal Error – yii\base\ErrorException

Class ‘PHPExcel’ not found

Following are my stpes:

[list=1]

[]I have added "phpoffice/phpexcel":""

[*]in required section of www/project/yii-application/composer.json

[*]update composer

[*]in controller $objPHPExcel = new \PHPExcel(); or $objPHPExcel = new \PHPExcel; Both not working

[/list]

Please help

there are a few, very minor problems with your code. Here’s what I wrote and got to work:




		$objPHPExcel = new \PHPExcel();

		 

		$sheet=0;

		  

		$objPHPExcel->setActiveSheetIndex($sheet);

		$foos = [

			['firstname'=>'John',

			'lastname'=>'Doe'],

			['firstname'=>'John',

			'lastname'=>'Jones'],

			['firstname'=>'Jane',

			'lastname'=>'Doe'],

		];

		 

	    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);

	    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);

	        

	    $objPHPExcel->getActiveSheet()->setTitle('xxx')		        

	     ->setCellValue('A1', 'Firstname')

	     ->setCellValue('B1', 'Lastname');

	         

         $row=2;

		                

		foreach ($foos as $foo) {  

		        

		    $objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo['firstname']); 

		    $objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$foo['lastname']);

		    $row++ ;

		}

		        

        header('Content-Type: application/vnd.ms-excel');

        $filename = "MyExcelReport_".date("d-m-Y-His").".xls";

        header('Content-Disposition: attachment;filename='.$filename .' ');

        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output');      	    



Thanks very much for your example. It’s what I needed to get something basic working.

Thank you, it worked with Oracle and Sql server as well. Here is working code:

public function actionExcel(){

Yii::import(‘ext.phpexcel.XPHPExcel’);

  &#036;objPHPExcel= XPHPExcel::createPHPExcel();


  &#036;objPHPExcel-&gt;getProperties()-&gt;setCreator(&quot;Morshedul Basher&quot;)


                         -&gt;setLastModifiedBy(&quot;Morshedul Basher&quot;)


                         -&gt;setTitle(&quot;Office 2007 XLSX Test Document&quot;)


                         -&gt;setSubject(&quot;Office 2007 XLSX Test Document&quot;)


                         -&gt;setDescription(&quot;Test document for Office 2007 XLSX, generated using PHP classes.&quot;)


                         -&gt;setKeywords(&quot;office 2007 openxml php&quot;)


                         -&gt;setCategory(&quot;Test result file&quot;);





    


 


    


       &#036;model = Yii::app()-&gt;db-&gt;createCommand(&quot;select AREA,REGION,Market,SWITCH from SWITCH_LOCATION&quot;)-&gt;queryAll();


       


        &#036;objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('A')-&gt;setWidth(20);


        &#036;objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setWidth(20);


         &#036;objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('C')-&gt;setWidth(20);


        &#036;objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('D')-&gt;setWidth(20);


            


        &#036;objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('PSAP')                     


         -&gt;setCellValue('A1', 'Area')


         -&gt;setCellValue('B1', 'Region')


        -&gt;setCellValue('C1', 'Market')


         -&gt;setCellValue('D1', 'Switch');


             


     &#036;row=2;


                            


            foreach (&#036;model as &#036;foo) {  


                    


                &#036;objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A'.&#036;row,&#036;foo['AREA']); 


                &#036;objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('B'.&#036;row,&#036;foo['REGION']);


                 &#036;objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('C'.&#036;row,&#036;foo['Market']); 


                &#036;objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('D'.&#036;row,&#036;foo['SWITCH']);


                &#036;row++ ;


            }


                    


    header('Content-Type: application/vnd.ms-excel');


    &#036;filename = &quot;psap&quot;.date(&quot;d-m-Y-His&quot;).&quot;.xls&quot;;


    header('Content-Disposition: attachment;filename='.&#036;filename .' ');


    header('Cache-Control: max-age=0');


    &#036;objWriter = &#092;PHPExcel_IOFactory::createWriter(&#036;objPHPExcel, 'Excel5');


    &#036;objWriter-&gt;save('php://output');  

}

I can make it work but I want to get the file to download instead of writing on disk. What can I do?