Yii Framework Forum: How To Load Phpexcel In Yii 2.0 Project? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Load Phpexcel In Yii 2.0 Project? PHPExcel classes couldn't autoload by YII 2

#1 User is offline   lujisheng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 14-May 12

Posted 07 March 2014 - 08:06 PM

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.
0

#2 User is offline   porchpt 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 30-April 13
  • Location:PT

Posted 07 March 2014 - 08:27 PM

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();

0

#3 User is offline   lujisheng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 14-May 12

Posted 08 March 2014 - 01:16 AM

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
0

#4 User is offline   Dong Nguyen Huu 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 06-December 13

Posted 08 March 2014 - 12:06 PM

use
$objPHPExcel = new \PHPExcel();

0

#5 User is offline   lujisheng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 14-May 12

Posted 08 March 2014 - 09:06 PM

Thank you!

It's works!

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

#6 User is offline   Yudi Lee 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 31-March 11
  • Location:Surabaya, Jawa Timur

Posted 26 March 2014 - 03:15 AM

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


View PostDong Nguyen Huu, on 08 March 2014 - 12:06 PM, said:

use
$objPHPExcel = new \PHPExcel();


0

#7 User is offline   stromgol 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 16-March 13

Posted 26 March 2014 - 08:00 AM

View PostYudi Lee, on 26 March 2014 - 03:15 AM, said:

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'); 	

0

#8 User is offline   Yudi Lee 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 31-March 11
  • Location:Surabaya, Jawa Timur

Posted 26 March 2014 - 08:32 PM

Thanks, will try it soon

View Poststromgol, on 26 March 2014 - 08:00 AM, said:

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'); 	


0

#9 User is offline   shark69 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-June 13
  • Location:Near Venice Italy

Posted 15 May 2014 - 03:21 AM

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

#10 User is offline   shark69 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-June 13
  • Location:Near Venice Italy

Posted 15 May 2014 - 04:53 AM

Ok I found the problem,

Quote

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



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

#11 User is offline   jotap28 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-November 14

Posted 17 November 2014 - 07:39 AM

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.
0

#12 User is offline   Muhammad Nauman Yousaf 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 06-February 12

Posted 10 April 2015 - 09:26 AM

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:
  • 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


Please help
0

#13 User is offline   Alec Smythe 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 13-January 13

Posted 27 August 2015 - 05:59 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users