YakobeYak
YakobeYak

Reputation: 555

How to use PHPExcel correctly with Symfony 2

I need to use PHPExcel with a Symfony2 project. Anyone know how to set up the project correctly to use the library? Should i put it in the vendor directory? What should be changed in the configuration files etc?

Upvotes: 16

Views: 34746

Answers (6)

PengWei
PengWei

Reputation: 66

If you are using composer to manage your project, you can just change the composer.json file:

"autoload": {
    "psr-4": {
        "": "src/",
        "": "vendor/phpoffice/phpexcel/Classes/"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

Then add

use PHPExcel;
use PHPExcel_IOFactory;

to your controller file, and you can use the PHPExcel like this:

$objPHPExcel = new PHPExcel();

Hope it helps.

Upvotes: 5

sjagr
sjagr

Reputation: 16502

As of Symfony 2.3, you can now do this:

...
"require": {
    ...
    "phpoffice/phpexcel": "dev-master"
    ...
},
...

Then just run composer update and dependencies will resolve automatically.

Or you can do composer require phpoffice/phpexcel:dev-master if you don't want to mess with the composer.json file.

Upvotes: 7

gakhov
gakhov

Reputation: 1971

Actually, to do it right you need to follow next steps:

  • Edit your deps file and add dependency from the PHPExcel
[PHPExcel]
git=http://github.com/PHPOffice/PHPExcel.git
target=/phpexcel
version=origin/master
  • Run php bin/vendors install in order to install all missing dependencies (PHPExcel in our case)

  • Update prefixes section in app/autoload.php:

$loader->registerPrefixes(array(
    // ...
    'PHPExcel'         => __DIR__.'/../vendor/phpexcel/Classes',
));
  • Done. Now, you can use it in your bundle's controller (code based on PHPExcel example from Tests/01simple-download-xls.php):
<?php
namespace Demo\MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use PHPExcel;
use PHPExcel_IOFactory;

class DemoController extends Controller
{
   public function demoAction()
   { 
       $response = new Response();

       // Create new PHPExcel object
       $objPHPExcel = new PHPExcel();

       // Set document properties
       $objPHPExcel->getProperties()->setCreator("Me")
                   ->setLastModifiedBy("Someone")
                   ->setTitle("My first demo")
                   ->setSubject("Demo Document");
       // Add some data
       $objPHPExcel->setActiveSheetIndex(0)
                   ->setCellValue('A1', 'Hello')
                   ->setCellValue('B2', 'world!')
                   ->setCellValue('C1', 'Hello')
                   ->setCellValue('D2', 'world!');
       // Set active sheet index to the first sheet
       $objPHPExcel->setActiveSheetIndex(0);

       // Redirect output to a client’s web browser (Excel5)
       $response->headers->set('Content-Type', 'application/vnd.ms-excel');
       $response->headers->set('Content-Disposition', 'attachment;filename="demo.xls"');
       $response->headers->set('Cache-Control', 'max-age=0');
       $response->prepare();
       $response->sendHeaders();
       $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
       $objWriter->save('php://output');
       exit();
   }
}

Upvotes: 14

timaschew
timaschew

Reputation: 16602

With composer (since Symfony2.1) it's really easy, you only have to modify the composer.json.
You don't need to register the namespace anymore!

Only two things, to notice:

  1. refer to github tags, I only found a soltion with the package type
  2. when changing something in the composer.json related to the class autoloading stuff, you have to remove the whole directory in the vendor dir

Here is the related link: use PHPExcel with composer and Symfony2.2

Upvotes: 2

linuxatico
linuxatico

Reputation: 1896

actually the best solution is to use https://github.com/liuggio/ExcelBundle. I tried to use @Crozin's solution but I was still getting an error about IOFactory::createWriter. Hope this helps, Simone

Upvotes: 10

Crozin
Crozin

Reputation: 44376

  1. Copy the library to your vendors directory.
  2. Configure autoloader in your bootstrap file:

    $loader->registerPrefixes(array(
        // Swift, Twig etc.
        'PHPExcel' => __DIR__ . '/../vendor/phpexcel/lib/PHPExcel'
    ));
    
  3. That's all.

Upvotes: 11

Related Questions