How to use PHPExcel correctly with Symfony 2

2020-02-17 07:59发布

问题:

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?

回答1:

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.



回答2:

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();
   }
}


回答3:

  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.



回答4:

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



回答5:

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.



回答6:

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