Yii 1.1: yiiexcel

Wrapper for PHPExcel
16 followers

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.

Resources

Total 13 comments

#16248 report it
Alex D. at 2014/02/03 08:04am
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?

#14164 report it
Emil Fedorciuc at 2013/07/24 07:20am
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

#13639 report it
anilherath at 2013/06/13 06:41am
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?

#13023 report it
marcovtwout at 2013/04/29 04:11am
New wrapper

Here is my new wrapper extension: https://github.com/marcovtwout/yii-phpexcel

#12993 report it
marcovtwout at 2013/04/26 08:40am
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

#12972 report it
Yeye at 2013/04/24 12:38pm
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.

#12863 report it
davidovv at 2013/04/16 11:09am
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?

#11668 report it
Yeye at 2013/01/25 12:13pm
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

#11653 report it
yJeroen at 2013/01/25 02:53am
Difference?

Hello,

Can you tell what's the difference/improvements compared to the extension EExcelView?

#11627 report it
Yeye at 2013/01/23 11:20am
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

#11626 report it
Yiivgeny at 2013/01/23 10:32am
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).

#11625 report it
Yeye at 2013/01/23 10:09am
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

#11614 report it
Yiivgeny at 2013/01/23 02:31am
Editing bootstrap files

Editing bootstrap files (e.g. index.php) is bad practice. Especially with so many code.

Leave a comment

Please to leave your comment.

Create extension