3

I need to generate Excel files, I did a search and phpexcel seems to be good and stable. I'd like to know: * how to integrate it into my project given that it's a symfony2.0 project * if I can do all what I do normally on excel file through this php library (cells colors, adding lists ...)

Thanks,

pnuts
  • 58,317
  • 11
  • 87
  • 139
smarber
  • 4,829
  • 7
  • 37
  • 78
  • 1
    PHPExcel works with an SPL compatible autoloader, so it should work alongside Symfony 2; and it is capable of doing most (though not all) things (you can't do Pivot tables for example) that you can do with MS Excel – Mark Baker May 05 '14 at 21:35

1 Answers1

5

If you are using composer, and since PHPExcel is registered with Packagist, then this is simply a matter of adding PHPExcel to your composer.json config, for example:

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "~2.4",
    "doctrine/orm": "~2.2,>=2.2.3",
    ...
    "phpoffice/phpexcel": "~1.8.0",

This is from a Symfony 2.4 configuration, but should work equally well for any version of Symfony.

Run $ php composer.phar update to grab the package and it should then be autoloaded into your project. You can then use PHPExcel immediately, say in a controller:

<?php
namespace SomeProject\SomeBundle\Controller;
use PHPExcel;

Or just reference \PHPExcel directly from within your code.

Update:

Note that composer found it's way into Symfony from around version 2.0.4, and was used as an alternative to the old deps and deps.lock files until the deps approach was deprecated from 2.1.

If you're still using deps, you can append this to your deps file:

[PHPExcel]
    git=git://github.com/PHPOffice/PHPExcel.git
    target=phpexcel

and run php bin/vendors install. This will put the PHPExcel in vendors/phpexcel.

Register PHPExcel with the registerPrefixes array in app/autoload.php and it should be available to your project as described above.

$loader->registerPrefixes(array(
    'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
    'Twig_'            => __DIR__.'/../vendor/twig/lib',
    'PHPExcel'         => __DIR__.'/../vendor/phpexcel/Classes'
));
nurikabe
  • 3,802
  • 2
  • 31
  • 39
  • Unfortunately, I'm not using composer so far into my project (symfony2.0). Do you think it's easy to add it to symfony2.0 project as well? – smarber May 06 '14 at 10:59
  • Wow, so from the pre-composer days? Should be possible. Answer updated. – nurikabe May 07 '14 at 13:36
  • I tried it and it re-install all my existing packages :( – smarber May 09 '14 at 21:03
  • Is it possible to just update, which means to just add phpexcel? – smarber May 09 '14 at 21:34
  • "$ bin/vendors update" might do this for you. bin/vendors wasn't all that smart as I recall; really it was just a temporary solution until composer stabilized. composer is far better at managing dependencies and individual packages. Both package managers are basically just mechanisms to download/save source archives. If you can't get bin/vendors to work and don't want to upgrade to composer, you could always add PHPExcel to the vendors directory (or anywhere, really) manually. Make sure that the registerPrefixes config points to PHPExcel's Classes directory. – nurikabe May 11 '14 at 10:46