Class 'PHPExcel_IOFactory' not found - CakePHP & PHPExcel

Bird87 ZA picture Bird87 ZA · Oct 9, 2013 · Viewed 9.1k times · Source

Background

I have issues using PHPExcel's IOFactory in my app. My PHPExcel helper works if I want to generate an excel document from scratch (my app creates about 20 excel sheets per week, with no issues).

Why do you need It?

I need to now combine some sheets (uploaded and generated to a general sheet and then send that off to who-ever. Problem is that all the sheets are generated by a different person. So they upload all these sheets to the app, and it combines the sheets and sends it out.

What's the problem then?

In my PHPExcelHelper.php file, I've added the following function:

public function createMasterFile($ExcelFile) {
    $MasterFile = PHPExcel_IOFactory::load($ExcelFile);
    return $MasterFile;
}

This is basically just supposed to take a file, to which it will add all the other files as spreadsheets. I'm using this question as a guide.

However, it throws the following error:

Error: Class 'PHPExcel_IOFactory' not found 

This tells me that it can't find that class (duh) and that I need to include it.

What have you tried?

I've tried to add it directly by adding require_once('PHPExcel/IOFactory.php'); to the end of the PHPExcel.php file located in the Vendors-folder. No luck.

Then I tried adding it to my loadEssentials()-function in my PHPExcelHelper.php-file, which was like this one and then I just added this:

App::import('Vendor', 'PHPExcel/IOFactory');
if (!class_exists('PHPExcel/IOFactory')) {
    throw new CakeException('Vendor class IOFactory not found!');
}

No luck.

And I've also tried calling it by using public $helpers = array('PHPExcel','PHPExcel/IOFactory'); in my Controller, but this just asked for a helper which didn't exist. So that also gave me no luck.

What do you want from us?

Any assistance in getting this to work really. I have tried what I thought would be the logical ways to include IOFactory, but have had no success. Could someone please show me how to get PHPExcel's IOFactory to work?

Answer

arilia picture arilia · Oct 9, 2013

assuming your folder structure is like this

App
    Vendor
        PHPExcel
            PHPExcel.php
            PHPExcel
                IOFactory.php

you should use

App::import('Vendor', 'PHPExcel', array('file' => 'PHPExcel'.DS.'PHPExcel.php'));
App::import('Vendor', 'PHPExcel_IOFactory', array('file' => 'PHPExcel'.DS.'PHPExcel'.DS.'IOFactory.php'));