Git Product home page Git Product logo

excelbundle's Introduction

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

    $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('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);
  • Create a Excel file with an image:
$writer = $this->get('phpexcel')->createPHPExcelObject();
$writer->setActiveSheetIndex(0);
$activesheet = $writer->getActiveSheet();

$drawingobject = $this->get('phpexcel')->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)

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;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

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
        $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

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 .

excelbundle's People

Contributors

liuggio avatar renatomefi avatar squazic avatar tomasvotruba avatar sethunath avatar pauvos avatar ashalaev avatar anyqax avatar lvancrayelynghe avatar cbergau avatar jebbench avatar mmoreram avatar lemoinem avatar gnat42 avatar nursultanturdaliev avatar oscargala avatar vrtak-cz avatar mussbach avatar s4brown avatar dxops avatar vctls avatar dirkbloessl avatar gondo avatar ryzy avatar waldo2188 avatar

Watchers

James Cloos avatar Agus Suhartono avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.