New way to use PHPExcel with Yii framework


(Tieulonglanh) #1

I use Yii to build my project and I find a new way to use PHPExcel (from codeplex) with Yii framework. I’ve read some posts about PHPExcel and Yii, but it’s not very useful for me or PHPExcel’s not as an extension of Yii. So I decided to integrate PHPExcel as an extension for Yii.

First, you need PHPExcel library, which I attach with my topic.

Then, following these steps:

  • Copy all files and folders in PHPExcel4Yii/ to folder:

protected/extensions/

  • Add to config file: main.php:


'import' => array( ...

		'application.extensions.*', 

		...)

+ 

'components' => array(

		...

	        'excel'=>array(

	          'class'=>'application.extensions.PHPExcel',

	        ),

		...

	   )

  • New functions is added:

    • Create a new excel object:

$objPHPExcel = Yii::app()->excel; 

(required when using PHPExcel with Yii framework)

+ Save a excel file (excel 2007): 

$objPHPExcel->saveExcel2007($objPHPExcel,$filename);

+ Create a 2007 excel object reader : 

$objReader = $objPHPExcel->readExcel2007();

  • You only need start with

$objPHPExcel = Yii::app()->excel; 

and use PHPExcel like documentation in phpexcel.codeplex.com/releases/view/45412 (I’m newbie so that I can’t post link :D )

Example code:


 

        $model = new DemoModel();

        $labels = $model->attributeNames();

        $data = $model->findAll();

        //Khởi tạo đối tượng excel 

        //Begin an excel object

        $objPHPExcel = Yii::app()->excel;

        //Đặt giá trị cho các cel tương ứng trong file excel

        //Set labels in excel file

        $objPHPExcel->getActiveSheet()->setCellValue('A1',$labels[0]);

        $objPHPExcel->getActiveSheet()->setCellValue('B1',$labels[1]);

        $objPHPExcel->getActiveSheet()->setCellValue('C1',$labels[2]);

        $objPHPExcel->getActiveSheet()->setCellValue('D1',$labels[3]);

        $i = 2;

        foreach($data as $record){

            //Lấy giá trị vào các cell của file excel

            //Set value for each cell

            $objPHPExcel->getActiveSheet()->setCellValue('A'. $i,$record->id);

            $objPHPExcel->getActiveSheet()->setCellValue('B'. $i,$record->name);

            $objPHPExcel->getActiveSheet()->setCellValue('C'. $i,$record->homepage);

            $objPHPExcel->getActiveSheet()->setCellValue('D'. $i,$record->description);

            $i++;

        }

        //Định giá trị về độ rộng các cột trong file excel

        //Set width for each B,C,D cell 

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

	objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(24);

	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(24);

        //Tên file

        //file name

        $filename = "abc.xlsx";

        //Lưu file

        //Save to abc.xlsx file

        // You can use like PHPExcel documentation from codeplex: 

        //$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

        //$objWriter->save($filename);

        $objPHPExcel->saveExcel2007($objPHPExcel,$filename);




(Steven Yamashiro) #2

I’d like to try this but your zip file doesn’t seem to be working. Is there another place I can get it?


(Tieulonglanh) #3

You can use 7zip or winrar program to extract this file.


(Veroansilvio) #4

first of all excuse my English, but if i wanted to read a file which should be located


(Jsfousseni) #5

Working fine!


(Hendri Cacem) #6

It works fine, when you are not creating instances of Yii active record models while you’re using PHPExcel,

I try to write an import function from XLS using PHPExcel and Yii Active Record, and it’s crashed for some reason, any suggesion?

Cheers


(Pumpk2k) #7

I have the following problem

ZipArchive::getFromName() [<a href=‘ziparchive.getfromname’>ziparchive.getfromname</a>]: Invalid or unitialized Zip object

any idea?


(Stasuss) #8

i think you are missing zip php module on the server


(Pumpk2k) #9

it was loaded i guess, cause in phpinfo() it show it, im was searching and in some forums say that is a windows temp folder problem, but im on windows 7, and the permissions is a little creepy


(Stasuss) #10

Try this. It works for me.


(C Pandit) #11

I use the below code but i have erroe for this

Parse error: syntax error, unexpected ‘;’, expecting ‘)’ in D:\xampp\xampp\htdocs\maveric\protected\config\main.php on line 44

What i do Pls help me any one


(Giancarloalbanese 74) #12

It’s possible use this way to load a file xls?

Into traditional phpexcel:




$objReader = PHPExcel_IOFactory::createReader('Excel2007');//use excel2007 

$objPHPExcel = $objReader->load("c:\profile.xls"); //DIR ARCHIVE

$val = ($objPHPExcel->getActiveSheet()->getCell('A1')); //CELL

$temp = $val->getvalue();//returns the value in the cell



How can I translate the above snippet using your way?

I think something like this:




$objPHPExcel = Yii::app()->excel;

$objReader = $objPHPExcel->readExcel2007();		

$objPHPExcel = $objReader->load($file); //ARCHIVE excel2007 dir		

$val = ($objPHPExcel->getActiveSheet()->getCell('A1')); //CELL -> this line crash

$temp = $val->getvalue();//returns the value in the cell

die($temp);		




(Igor Savinkin) #13

What to do if this does not work for me?

other resourcedoesn’t work as well.


(Fahad2001akram) #14

[size=“5”]ZipArchive::getFromName() [<a href=‘ziparchive.getfromname’>ziparchive.getfromname</a>]: Invalid or unitialized Zip object [/size]

even zip module in wamp server is active

any solution


(Fahad2001akram) #15

i used @ Giancarlo style of getting data from excel

like this

    &#036;objPHPExcel = Yii::app()-&gt;excel;


    &#036;myfile = 'd:&#092;fahadtest.xls';


    &#036;objPHPExcel = Yii::app()-&gt;excel;


    &#036;objReader = &#036;objPHPExcel-&gt;readExcel2007();


    &#036;objPHPExcel = &#036;objReader-&gt;load(&#036;myfile); //ARCHIVE excel2007 dir         


    &#036;val = (&#036;objPHPExcel-&gt;getActiveSheet()-&gt;getCell('A1')); //CELL -&gt; this line crash


    &#036;temp = &#036;val-&gt;getvalue(); //returns the value in the cell


    die(&#036;temp);

(Eder Ac Ruiz) #16

Hello! I need help, I tried this post but it is not work for me, I get an error with page load, error’s Chrome say

This page is not available and error code is ERR_CONNECTION_RESET. I get this error when I used $objPHPExcel->getActiveSheet()->setCellValue(‘D1’,‘hello’); Thanks for you help.