This is a class with an autoloader and few steps for use PHPExcel inside a Yii application.
Requirements ¶
- Yii 1.1 or above
- PHPExcel 1.7.8
Installation ¶
- Copy yiiexcel directory to protected/extensions.
- Download PHPExcel.
- Create a phpexcel directory on protected/vendors.
- Unzip PHPExcel and copy Classes directory content to protected/extensions/phpexcel.
- Edit PHPExcel.php file and comment the autoload inclusion:
/** PHPExcel root directory */
/*if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}*/
- Edit index.php file and register the YiiExcel autoloader:
require_once($yii);
//do not run app before register YiiExcel autoload
$app = Yii::createWebApplication($config);
Yii::import('ext.yiiexcel.YiiExcel', true);
Yii::registerAutoloader(array('YiiExcel', 'autoload'), true);
// Optional:
// As we always try to run the autoloader before anything else, we can use it to do a few
// simple checks and initialisations
PHPExcel_Shared_ZipStreamWrapper::register();
if (ini_get('mbstring.func_overload') & 2) {
throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
}
PHPExcel_Shared_String::buildCharacterSets();
//Now you can run application
$app->run();
- Edit main.php config file to import PHPExcel main class:
// autoloading model and component classes
'import'=>array(
...
'application.vendors.phpexcel.PHPExcel',
...
),
Usage ¶
Just create a PHPExcel instance:
$objPHPExcel = new PHPExcel();
Read the SiteController.php example file located inside example directory.
Editing bootstrap files
Editing bootstrap files (e.g. index.php) is bad practice. Especially with so many code.
It's like on Yii Cookbook
Well, I do this practice like is on the Yii CookBook, by Alexander Makarov. :D
I don't see any problem to add some lines to bootstrap index.php
Autoloading
1) AFAIK, Alexander Makarov writing about ZF autoloading. It's good variant for core & low-level solutions, but not for extension used on only some pages.
2) It's uncomfortably if using many boostrap script e.g. yiic, index-test. Bootstrap scripts need manually editing by user if your extension will be updated later. All your lines may be putted on init method, if you create real app component (CApplicationComponent).
Needs
I guess you have right on that context.
I write this for cover a special need on some projects, where I have to do a heavy use of excel files, so in my scenario I need register the autoload only in the index.php instead the init method.
Anyway, if a user prefer put that lines on init method instead of the index.php file, all will work fine.
I can fix that instruction later. :D
Regards
Difference?
Hello,
Can you tell what's the difference/improvements compared to the extension EExcelView?
This is just a wrapper
@ yJeroen This is just a wrapper.
While in EExcelView the main idea is to easily export already defined grids to excel files, YiiExcel just is an autoload for use PHPExcel class.
With YiiExcel I have to do nothing else on YiiReport for PHPExcel calls.
Regards
editing vendor files is also bad practice
Wouldn't it be better to do something like
define('PHPEXCEL_ROOT') instead of commenting part of source file?
Of course
@davidovv
Of course. This code is just a suggestion, not a stone rule. :)
Anyway, when I did that was because I dont want to load the PHPExcel autoloader.
Bad practices
Like previous comments mensioned, this extension should be self contained.
Asking users to edit their index.php by default is bad practise.
Could you update your extension?
For a proper implementation example, see what the Yii authors did with CHtmlPurifier: https://github.com/yiisoft/yii/blob/1.1.13/framework/web/widgets/CHtmlPurifier.php
New wrapper
Here is my new wrapper extension: https://github.com/marcovtwout/yii-phpexcel
How to use with already created yii application
The line;
'$app = Yii::createWebApplication($config);'
Trying to create new yii application.
How to use this extension with already created application?
Quick fix not to edit the bootstrap
I'm working on a project that uses Smarty and that code would mess up my Smarty autoloader. So instead I put all that code in the action that would render the report and it works like a charm, not sure if that's the best way to do things, but it's a quick fix.
P.S. no changes to index.php needed
Does this wrapper is actually needed?
Hi! Just to make all clear for me - is this wrapper needed after second solution?
http://www.yiiframework.com/wiki/101/how-to-use-phpexcel-external-library-with-yii/
"In "Classes/PHPExcel/Autoloader.php", replace the line
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));
with:
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'), true, true);
"
As I understand, if I have no problems with changing just 1 line of code, this wrapper doesn't add any additional functionality\style?
Como debo
Hola estoy intentando utilizar esta extension por las bondades que presta pero me da este error como que no encuentra lo relacionado con esta linea:
PHPExcel_Shared_String::buildCharacterSets();
como lo soluciono
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.