Using PHPExcel in CakePHP

Bird87 ZA picture Bird87 ZA · Feb 8, 2013 · Viewed 16.1k times · Source

Background

Well I tried to ask this question, but it got closed down faster than a horrible security leak. I want to write an excel file using PEAR's Spreadsheet Excel Writer. I Google'd endlessly for a easy way to do this, and finally found a helper that might actually do the job.

But it uses PHPExcel, so I'll have to rewrite that script, which is the least of my worries. I just don't want to start now, and then still not be able to get it working.

I have also downloaded PHPExcel as required by the helper.

What do I have?

I have the helper and I've added the helper to the controller like so:

public $helpers = array('PhpExcel.PhpExcel'); 

and I've created (copied and pasted) the script online into the view, just to test if it works.

What is the problem

I get an error:

Error: The application is trying to load a file from the PhpExcel plugin

Error: Make sure your plugin PhpExcel is in the app\Plugin directory and was loaded

I have included the file in the APP/Vendor/PHPExcel.php folder (because I read somewhere that if the plugin doesn't follow the MVC framework, it needs to go in there) but it still doesn't work. Also tried it in the plugin directory and same error. This is the first time I'm using helpers (and hopefully not the last) so I'm pretty clueless with this.

I've also included the actual helper in the APP/View/Helper/ folder as PhpExcelHelper.php.

And the thing is I can't even get past step 1. Can someone tell me where the files need to go so that I can get this to work?

Answer

Borislav Sabev picture Borislav Sabev · Feb 13, 2013

If you take a peek in the Helper code you will notice that there is this function there called loadEssentials:

protected function loadEssentials() {
    // load vendor class
    App::import('Vendor', 'PHPExcel/Classes/PHPExcel');
    if (!class_exists('PHPExcel')) {
        throw new CakeException('Vendor class PHPExcel not found!');
    }
}

Now as you can see it looks in the Vendor folder for the PHPExcel library. What you should do is put the PHPExcel code in App/Vendor/. Then take a look at how to load vendor packages. Then the Helper should be in App/View/Helper and when you instantate it it should work. Also check the raed/write/excute permissions of the files in Vendor depending on your OS.