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? Wha
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:
package
type vendor
dirHere is the related link: use PHPExcel with composer and Symfony2.2
Actually, to do it right you need to follow next steps:
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', ));
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();
}
}
Configure autoloader in your bootstrap file:
$loader->registerPrefixes(array(
// Swift, Twig etc.
'PHPExcel' => __DIR__ . '/../vendor/phpexcel/lib/PHPExcel'
));
That's all.
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.
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.
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