This is a class with an autoloader and few steps for use PHPExcel inside a Yii application.
/** PHPExcel root directory */ /*if (!defined('PHPEXCEL_ROOT')) { define('PHPEXCEL_ROOT', dirname(__FILE__) . '/'); require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); }*/
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();
// autoloading model and component classes 'import'=>array( ... 'application.vendors.phpexcel.PHPExcel', ... ),
Just create a PHPExcel instance:
$objPHPExcel = new PHPExcel();
Read the SiteController.php example file located inside example directory.
Total 11 comments
The line;
'$app = Yii::createWebApplication($config);'
Trying to create new yii application.
How to use this extension with already created application?
Here is my new wrapper extension: https://github.com/marcovtwout/yii-phpexcel
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
@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.
Wouldn't it be better to do something like define('PHPEXCEL_ROOT') instead of commenting part of source file?
@ 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
Hello,
Can you tell what's the difference/improvements compared to the extension EExcelView?
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
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).
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
Editing bootstrap files (e.g. index.php) is bad practice. Especially with so many code.
Leave a comment
Please login to leave your comment.