New way to use PHPExcel with Yii framework

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:


  • Add to config file: main.php:

'import' => array( ...




'components' => array(







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


+ Create a 2007 excel object reader : 

$objReader = $objPHPExcel->readExcel2007();

  • You only need start with

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

and use PHPExcel like documentation in (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





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



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

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




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



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

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

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

Working fine!

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?


I have the following problem

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

any idea?

i think you are missing zip php module on the server

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

Try this. It works for me.

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

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


What to do if this does not work for me?

other resourcedoesn’t work as well.

[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

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


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.