Developed with love by KnpLabs Hire us for your project!
158

ExcelBundle

by liuggio

Excel integration in Symfony2 thanks to https://github.com/PHPOffice/PHPExcel library

Symfony2 Excel bundle

This bundle permits you to create, modify and read excel objects.

Build Status
Total Downloads
Latest Stable Version
Latest Unstable Version

License

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

    "require" : {
        "liuggio/excelbundle": "~2.0",
    }

2 Register the bundle in app/AppKernel.php

    $bundles = array(
        // ...
        new Liuggio\ExcelBundle\LiuggioExcelBundle(),
    );

TL;DR

  • Create an empty object:
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
  • Create an object from a file:
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject('file.xls');
  • Create a Excel5 and write to a file given the object:
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$writer->save('file.xls');
  • Create a Excel5 and create a StreamedResponse:
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$response = $this->get('phpexcel')->createStreamedResponse($writer);

Not Only 'Excel5'

The list of the types are:

  1. 'Excel5'
  2. 'Excel2007'
  3. 'Excel2003XML'
  4. 'OOCalc'
  5. 'SYLK'
  6. 'Gnumeric'
  7. 'HTML'
  8. '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;

class DefaultController extends Controller
{

    public function indexAction($name)
    {
        // ask the service for a Excel5
       $phpExcelObject = $this->get('phpexcel')->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('phpexcel')->createWriter($phpExcelObject, 'Excel5');
        // create the response
        $response = $this->get('phpexcel')->createStreamedResponse($writer);
        // adding headers
        $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
        $response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');
        $response->headers->set('Pragma', 'public');
        $response->headers->set('Cache-Control', 'maxage=1');

        return $response;        
    }
}

Contributors

the list of contributors

Contribute

  1. fork the project
  2. clone the repo
  3. get the coding standard fixer: wget http://cs.sensiolabs.org/get/php-cs-fixer.phar
  4. before the PullRequest you should run the coding standard fixer with php php-cs-fixer.phar fix -v .
Copyright (c) 2004-2014

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
liuggio_excel:        []
  • Merge pull request #70 from ryzy/master
    By liuggio, 2 months ago
  • Update PHPExcel to ~1.8.0
    By , 2 months ago
  • Update README.md
    By liuggio, 4 months ago
  • Update README.md
    By liuggio, 4 months ago
  • added MIT
    By liuggio, 4 months ago
  • Merge pull request #59 from AaL/patch-1
    By liuggio, 7 months ago
  • Update README.md
    By , 7 months ago
  • fixing #56 sf version
    By liuggio, 8 months ago
  • Merge pull request #55 from s4brown/objectcreate-fix
    By liuggio, 8 months ago
  • Changed createPHPExcelObject to pass filename as a string to fix ContextErrorException
    By , 8 months ago
  • fixed Readme
    By liuggio, 8 months ago
  • added 5.5 to travis
    By liuggio, 8 months ago
  • Release Candidate
    By liuggio, 8 months ago
  • CS fixes
    By liuggio, 8 months ago
  • Streamed response, removed unused dep.
    By liuggio, 8 months ago
  • added functional test
    By liuggio, 8 months ago
  • started 2.0-dev
    By liuggio, 8 months ago
  • Merge pull request #49 from gnat42/Reorganize
    By liuggio, 8 months ago
  • Merge pull request #52 from sergeyz/patch-1
    By liuggio, 10 months ago
  • Excess headers send
    By sergeyz, 10 months ago
  • Reorganized services so you can load and write the same file
    By gnat42, 11 months ago
  • Merge pull request #48 from cbergau/master
    By liuggio, 11 months ago
  • Add types in code documentary
    By cbergau, 11 months ago
  • Merge pull request #42 from lemoinem/patch-1
    By liuggio, 11 months ago
  • Merge pull request #46 from jinfollc/readme-update
    By liuggio, 1 year ago
  • Added sendHeaders for compatibility with https connections and IE<9
    By jinfollc, 1 year ago
  • Switch "CodePlex/PHPExcel" required version to 1.7.9 (last stable) and now use official repo
    By lemoinem, 1 year ago
  • Update README.md
    By liuggio, 1 year ago
  • Merge pull request #39 from waldo2188/master
    By liuggio, 1 year ago
  • Update Resources/config/services.yml
    By waldo2188, 1 year ago