7

how can I integrate the PHPExcel into my Zend app.

My actual folder structure is the following:

/application
  controllers
  views  
  etc...
/library
  My
  Zend
  PHPExcel
/public
  index.php

I already include 'My' libs by using (in index.php):

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('My_');

Now I also want to use PHPExcel inside one of my controllers like:

$exc = PHPExcel_IOFactory::load('test.xls');
$excelWorksheet = $exc->getActiveSheet();

What do I have to do to make it work and get rid of the Class 'PHPExcel_IOFactory' not found Exception?

Thank you.
-lony

P.S.: A simple $autoloader->registerNamespace('PHPExcel_'); is not working. I tested it.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
lony
  • 6,733
  • 11
  • 60
  • 92

6 Answers6

9

Place the PHPExcel library into the /library folder, like this:

/application
...
/library
    /PHPExcel
    /PHPExcel.php

Next, in your application.ini config file, add the following:

autoloaderNamespaces[] = "PHPExcel_"
autoloaderNamespaces[] = "PHPExcel"

That should do it. Autoloader takes care of the rest, and you can just start using the example code to read an Excel file.

Update: Added the extra autoloaderNamespace as suggested by commenters

Miljar
  • 241
  • 1
  • 5
  • As described in my question I tried to use `$autoloader->registerNamespace('PHPExcel_');` and it was not working. Your solution only move the setting from the `index.php` to `application.ini`, but it is still not running. – lony Jan 27 '11 at 06:39
  • Do you have the same setup in your /library folder? The base PHPExcel.php file is right under /library, and then the rest of the classes is under /library/PHPExcel. I implemented this just yesterday, without any problems – Miljar Jan 27 '11 at 08:30
  • The only problem with this is that it makes adding PHPExcel as an svn:external either difficult (separate external for PHPExcel.php), or impossible. – Stephen Fuhry Feb 08 '11 at 17:32
  • So I placed PHPExcel library into the /library folder, added line to ini file and (!) updated my index.php file: `set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH . '/../library'),realpath(APPLICATION_PATH . '/../library/PHPExcel'),get_include_path())));` – Andron Jun 23 '11 at 13:53
  • I did it this way, but when in the controller I call $xlsObj = new PHPExcel(); it returns the error Fatal error: Class 'PHPExcel' not found in... – cwhisperer Oct 04 '11 at 09:32
  • For me the above solution didn't work either, resulting in class not found exception. Adding not only `autoloaderNamespaces[] = "PHPExcel_"` but also `autoloaderNamespaces[] = "PHPExcel"` did the trick for me. – signpainter Jun 25 '12 at 08:27
0

I know it's 2 years since the question is asked but it may help someone; the easiest way ( not the optimal) is to extract the PHPExcel folder in your Public and then just use the old way ex; (in your controller actions):

                include 'PHPExcel.php';
                include 'PHPExcel/Writer/Excel2007.php';

                $myobject = new PHPExcel();
timmz
  • 2,194
  • 3
  • 23
  • 29
0

Additionally I added a "\" on the line where PHPExcel_IOFactory uses In Controller Class:

public function reporteauditoriaAction()
{
    $objPHPExcel = new \PHPExcel();
    $objPHPExcel->createSheet();
    $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Alejin Wbn');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
    $objWriter->save("pruebaPhpExcelZend.xlsx"); 
    //$objPHPExcel->disconnectWorksheets();
    //unset($objPHPExcel);
    $consulta= "Reporte Auditoria, Reconocio los Archivos";
    $vista = new ViewModel(array( "consulta"=>$consulta));
    return $vista;        
}
0

I have same issue and i solved it update the composer and in my project folder phpoffice saved inside the vendor module (not in lib). And adding "\" on the PHPExcel_IOFactory whereever you seen.

yasir kk
  • 161
  • 5
  • 9
0

I found one solution:

require_once 'PHPExcel/PHPExcel/IOFactory.php';

If somebody has a better one, please keep posting!

@BoltClock: Thanks for updating the Tags.

lony
  • 6,733
  • 11
  • 60
  • 92
  • 1
    Well, from the snippet you posted above your problem is quite obvious, move all of the PHPExcel source one level up, so IOFactory.php is located in library/PHPExcel/IOFactory.php instead of library/PHPExcel/PHPExcel/IOFactory.php. That should do the trick (and you can ditch the require_once). – wimvds Jan 26 '11 at 16:22
0

It needs to be in your include path.

If you ever need a custom autoloader for other libraries that don't follow PSR-0, there's this too: Autoload PhpThumb with Zend Framework (disclaimer: I'm the author).

Stephen Fuhry
  • 12,624
  • 6
  • 56
  • 55