Yii Framework Forum: New way to use PHPExcel with Yii framework - Yii Framework Forum

Jump to content

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

New way to use PHPExcel with Yii framework Rate Topic: -----

#1 User is offline   tieulonglanh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-April 11

Posted 18 April 2011 - 05:11 AM

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

Attached File(s)


2

#2 User is offline   MountainCastle 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 18-August 11

Posted 26 October 2011 - 06:13 PM

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

View Posttieulonglanh, on 18 April 2011 - 05:11 AM, said:

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


0

#3 User is offline   tieulonglanh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-April 11

Posted 13 December 2011 - 01:50 AM

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

#4 User is offline   Veroan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 21-December 11

Posted 22 December 2011 - 07:48 AM

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

#5 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 07 January 2012 - 10:35 AM

Working fine!
Posted Image
1

#6 User is offline   Hendri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 28-March 11

Posted 26 January 2012 - 12:40 AM

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
0

#7 User is offline   Nymphetamine 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 13-January 12

Posted 17 February 2012 - 03:24 PM

I have the following problem

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

any idea?
0

#8 User is offline   StasuSS 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 20-February 12
  • Location:Kaluga, Russia

Posted 20 February 2012 - 08:09 AM

i think you are missing zip php module on the server
Things are very seldom what they seem. In my experience, they’re usually a damn sight worse. (Inquisitor Titus Drake)
0

#9 User is offline   Nymphetamine 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 13-January 12

Posted 20 February 2012 - 09:00 AM

View PostStasuSS, on 20 February 2012 - 08:09 AM, said:

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
0

#10 User is offline   StasuSS 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 20-February 12
  • Location:Kaluga, Russia

Posted 21 February 2012 - 02:53 AM

Try this. It works for me.
Things are very seldom what they seem. In my experience, they’re usually a damn sight worse. (Inquisitor Titus Drake)
0

#11 User is offline   tpandi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 22-May 12

Posted 25 May 2012 - 01:04 AM

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


View Posttieulonglanh, on 18 April 2011 - 05:11 AM, said:

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


0

#12 User is offline   Giancarlo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 242
  • Joined: 20-December 11
  • Location:Turin, Italy

Posted 01 June 2012 - 09:10 AM

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


Posted Image Non preoccuparti, è dentro il monitor!

Sapevi che cercando su google yii + "la funzione su cui stai impazzendo", trovi quasi sempre dei buoni indizi in italiano, per risolvere i tuoi problemi, senza perdere tempo a postare?
0

#13 User is offline   Igor Savinkin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 30-July 13

Posted 27 August 2013 - 01:43 PM

View PostStasuSS, on 21 February 2012 - 02:53 AM, said:


What to do if this does not work for me?
other resourcedoesn't work as well.
0

#14 User is offline   fahadakram 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 08-February 13

Posted 13 February 2014 - 10:49 AM

ZipArchive::getFromName() [<a href='ziparchive.getfromname'>ziparchive.getfromname</a>]: Invalid or unitialized Zip object
even zip module in wamp server is active
any solution
0

#15 User is offline   fahadakram 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 08-February 13

Posted 13 February 2014 - 10:51 AM

i used @ Giancarlo style of getting data from excel

like this
$objPHPExcel = Yii::app()->excel;
$myfile = 'd:\fahadtest.xls';
$objPHPExcel = Yii::app()->excel;
$objReader = $objPHPExcel->readExcel2007();
$objPHPExcel = $objReader->load($myfile); //ARCHIVE excel2007 dir
$val = ($objPHPExcel->getActiveSheet()->getCell('A1')); //CELL -> this line crash
$temp = $val->getvalue(); //returns the value in the cell
die($temp);
0

#16 User is offline   EAndres 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 22-April 14

Posted 30 April 2014 - 11:28 AM

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

Share this topic:


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

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