agence-gw / excelbundle
This is a Symfony Bundle helps you to read and write Excel files (including pdf, xlsx, odt), thanks to the PHPExcel library
Requires
- php: ^7.0|^8.0
- agence-gw/phpexcel: dev-master
- symfony/framework-bundle: ~2.6|~3.0|^4.0|^5.0
Requires (Dev)
- phpunit/phpunit: ^4.7|^5.0
- sensio/framework-extra-bundle: ~2.3|~3.0
- symfony/browser-kit: ~2.6|~3.0|^4.0|^5.0
- symfony/class-loader: ~2.6|~3.0|^4.0|^5.0
- symfony/finder: ~2.6|~3.0|^4.0|^5.0
- symfony/form: ~2.6|~3.0|^4.0|^5.0
- symfony/validator: ~2.6|~3.0|^4.0|^5.0
This package is not auto-updated.
Last update: 2024-11-16 15:40:28 UTC
README
This bundle permits you to create, modify and read excel objects.
License
Version 2
This is the shiny new version. There is a big BC with the 1.* version, but unit tests, functional tests, and the new factory is very simple to use.
Version 1.*
If you have installed an old version, and you are happy to use it, you could find documentation and files in the tag v1.0.6, browse the code.
Things to know:
CSV is faster so if you have to create simple xls file, I encourage you to use the built-in function for csv: http://php.net/manual-lookup.php?pattern=csv&lang=en&scope=quickref
Installation
1 Add to composer.json to the require
key
$composer require liuggio/excelbundle
2 Register the bundle in `app/AppKernel.php
`
$bundles = array(
// ...
new Liuggio\ExcelBundle\LiuggioExcelBundle(),
);
TL;DR
- Create an empty object:
$phpExcelObject = $this->get('Liuggio\ExcelBundle\Factory')->createPHPExcelObject();
- Create an object from a file:
$phpExcelObject = $this->get('Liuggio\ExcelBundle\Factoryl')->createPHPExcelObject('file.xls');
- Create a Excel5 and write to a file given the object:
$writer = $this->get('Liuggio\ExcelBundle\Factory')->createWriter($phpExcelObject, 'Excel5');
$writer->save('file.xls');
- Create a Excel5 and create a StreamedResponse:
$writer = $this->get('Liuggio\ExcelBundle\Factory')->createWriter($phpExcelObject, 'Excel5');
$response = $this->get('Liuggio\ExcelBundle\Factory')->createStreamedResponse($writer);
- Create a Excel file with an image:
$writer = $this->get('Liuggio\ExcelBundle\Factory')->createPHPExcelObject();
$writer->setActiveSheetIndex(0);
$activesheet = $writer->getActiveSheet();
$drawingobject = $this->get('Liuggio\ExcelBundle\Factory')->createPHPExcelWorksheetDrawing();
$drawingobject->setName('Image name');
$drawingobject->setDescription('Image description');
$drawingobject->setPath('/path/to/image');
$drawingobject->setHeight(60);
$drawingobject->setOffsetY(20);
$drawingobject->setCoordinates('A1');
$drawingobject->setWorksheet($activesheet)
Note: it's recommended to inject factory instead of fetching it from container
Not Only 'Excel5'
The list of the types are:
- 'Excel5'
- 'Excel2007'
- 'Excel2003XML'
- 'OOCalc'
- 'SYLK'
- 'Gnumeric'
- 'HTML'
- 'CSV'
Example
Fake Controller
The best place to start is the fake Controller at Tests/app/Controller/FakeController.php
, that is a working example.
More example
You could find a lot of examples in the official PHPExcel repository https://github.com/PHPOffice/PHPExcel/tree/develop/Examples
For lazy devs
namespace YOURNAME\YOURBUNDLE\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class DefaultController extends Controller
{
public function indexAction($name)
{
// ask the service for a Excel5
$phpExcelObject = $this->get('Liuggio\ExcelBundle\Factory')->createPHPExcelObject();
// or inject it: $phpExcelObject = $injectedExcelFactory->createPHPExcelObject();
$phpExcelObject->getProperties()->setCreator("liuggio")
->setLastModifiedBy("Giulio De Donato")
->setTitle("Office 2005 XLSX Test Document")
->setSubject("Office 2005 XLSX Test Document")
->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
->setKeywords("office 2005 openxml php")
->setCategory("Test result file");
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!');
$phpExcelObject->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$phpExcelObject->setActiveSheetIndex(0);
// create the writer
$writer = $this->get('Liuggio\ExcelBundle\Factory')->createWriter($phpExcelObject, 'Excel5');
// create the response
$response = $this->get('Liuggio\ExcelBundle\Factory')->createStreamedResponse($writer);
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'stream-file.xls'
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
}
Contributors
Contribute
- Fork the project
- Clone the repo
- Get the coding standard fixer:
wget http://cs.sensiolabs.org/get/php-cs-fixer.phar
- Before the PullRequest you should run the coding standard fixer with
php php-cs-fixer.phar fix -v .