Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 495
YakobeYak Avatar asked Jun 19 '11 14:06

YakobeYak


3 Answers

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();
   }
}
like image 123
gakhov Avatar answered Oct 12 '22 12:10

gakhov


  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.

like image 27
Crozin Avatar answered Oct 12 '22 14:10

Crozin


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

like image 42
linuxatico Avatar answered Oct 12 '22 13:10

linuxatico