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

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • 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 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • 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 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • 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

Share this topic:


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

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