phpexcelsymfony2

How to add phpexcel to symfony2?


I need to generate Excel files, I did a search and phpexcel seems to be good and stable. I'd like to know:


Solution

  • 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'
    ));