Skip to content

Commit

Permalink
Release Candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
liuggio committed Dec 6, 2013
1 parent 933bdd5 commit f1924d8
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ before_script:
- composer install --dev - composer install --dev


script: script:
- php php-cs-fixer.phar fix -v --dry-run .; EXIT 0; - php php-cs-fixer.phar fix --dry-run --verbose --diff . || true;
- bin/phpunit - bin/phpunit
5 changes: 1 addition & 4 deletions DependencyInjection/Configuration.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder() public function getConfigTreeBuilder()
{ {
$treeBuilder = new TreeBuilder(); $treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('liuggio_excel'); $treeBuilder->root('liuggio_excel');


// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder; return $treeBuilder;
} }
} }
72 changes: 72 additions & 0 deletions Factory.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Liuggio\ExcelBundle;

use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* Factory for PHPExcel objects, StreamedResponse, and PHPExcel_Writer_IWriter.
*
* @package Liuggio\ExcelBundle
*/
class Factory
{
private $phpExcelIO;

public function __construct($phpExcelIO = '\PHPExcel_IOFactory')
{
$this->phpExcelIO = $phpExcelIO;
}
/**
* Creates an empty PHPExcel Object if the filename is empty, otherwise loads the file into the object.
*
* @param string $filename
*
* @return \PHPExcel
*/
public function createPHPExcelObject($filename = null)
{
if (null == $filename) {
$phpExcelObject = new \PHPExcel();

return $phpExcelObject;
}

return call_user_func(array($this->phpExcelIO, 'load'), array($filename));
}

/**
* Create a writer given the PHPExcelObject and the type,
* the type coul be one of PHPExcel_IOFactory::$_autoResolveClasses
*
* @param \PHPExcel $phpExcelObject
* @param string $type
*
*
* @return \PHPExcel_Writer_IWriter
*/
public function createWriter(\PHPExcel $phpExcelObject, $type = 'Excel5')
{
return call_user_func(array($this->phpExcelIO, 'createWriter'), $phpExcelObject, $type);
}

/**
* Stream the file as Response.
*
* @param \PHPExcel_Writer_IWriter $writer
* @param int $status
* @param array $headers
*
* @return StreamedResponse
*/
public function createStreamedResponse(\PHPExcel_Writer_IWriter $writer, $status = 200, $headers = array())
{
return new StreamedResponse(
function () use ($writer) {
$writer->save('php://output');
},
$status,
$headers
);
}
}
147 changes: 82 additions & 65 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@ Symfony2 Excel bundle


This bundle permits you to create an easily modifiable excel object. This bundle permits you to create an easily modifiable excel object.


You should know that csv is faster so I encourage you to use the built-in function for csv: http://php.net/manual-lookup.php?pattern=csv&lang=en&scope=quickref ## 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](https://github.com/liuggio/ExcelBundle/releases/tag/v1.0.6),
[browse the code](https://github.com/liuggio/ExcelBundle/tree/cf0ecbeea411d7c3bdc8abab14c3407afdf530c4).

### 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 ## Installation


**1** Add to composer.json to the `require` key **1** Add to composer.json to the `require` key


``` yml ``` yml
"require" : { "require" : {
"liuggio/excelbundle": "2.0.x-dev", "liuggio/excelbundle": "~2.0",
} }
``` ```


Expand All @@ -29,30 +43,57 @@ You should know that csv is faster so I encourage you to use the built-in functi
); );
``` ```


## Services ## TL;DR

- Create an empty object:

``` php
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
```


The list of the services are listed in `/Resources/config/services.yml`. - Create an object from a file:


``` php ``` php
// create MS Excel5 $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject('file.xls');
$this->get('xls.excel5'); ```
// create MS Excel 2007
$this->get('xls.excel2007'); - Create a Excel5 and write to a file given the object:
// read file
$excelService = $this->get('xls.excel5')->load($filename); ```php
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$writer->save('file.xls');
``` ```


- Create a Excel5 and create a StreamedResponse:

```php
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$writer->save('file.xls');
```
## 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 ## Example


### Fake Controller ### Fake Controller


The best place to start is the fake Controller at `Tests/app/Controller/FakeController.php`, this is a working example. The best place to start is the fake Controller at `Tests/app/Controller/FakeController.php`, that is a working example.


### More example ### More example


A lot of great example are in the official PHPExcel repository https://github.com/PHPOffice/PHPExcel/tree/develop/Examples You could find a lot of examples in the official PHPExcel repository https://github.com/PHPOffice/PHPExcel/tree/develop/Examples


### For lazy :) ### For lazy devs


``` php ``` php
namespace YOURNAME\YOURBUNDLE\Controller; namespace YOURNAME\YOURBUNDLE\Controller;
Expand All @@ -65,32 +106,29 @@ class DefaultController extends Controller
public function indexAction($name) public function indexAction($name)
{ {
// ask the service for a Excel5 // ask the service for a Excel5
$excelService = $this->get('xls.excel5'); $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
// or $this->get('xls.excel5')->load($filename);
// or create your own is easy just modify services.yml $phpExcelObject->getProperties()->setCreator("liuggio")

->setLastModifiedBy("Giulio De Donato")
// create the object see http://phpexcel.codeplex.com documentation ->setTitle("Office 2005 XLSX Test Document")
$excelService->excelObj->getProperties()->setCreator("liuggio") ->setSubject("Office 2005 XLSX Test Document")
->setLastModifiedBy("Giulio De Donato") ->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
->setTitle("Office 2005 XLSX Test Document") ->setKeywords("office 2005 openxml php")
->setSubject("Office 2005 XLSX Test Document") ->setCategory("Test result file");
->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.") $phpExcelObject->setActiveSheetIndex(0)
->setKeywords("office 2005 openxml php") ->setCellValue('A1', 'Hello')
->setCategory("Test result file"); ->setCellValue('B2', 'world!');
$excelService->excelObj->setActiveSheetIndex(0) $phpExcelObject->getActiveSheet()->setTitle('Simple');
->setCellValue('A1', 'Hello') // Set active sheet index to the first sheet, so Excel opens this as the first sheet
->setCellValue('B2', 'world!'); $phpExcelObject->setActiveSheetIndex(0);
$excelService->excelObj->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet // create the writer
$excelService->excelObj->setActiveSheetIndex(0); $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');

// create the response
//create the Symfony Streamed Response $response = $this->get('phpexcel')->createStreamedResponse($writer);
$response = $excelService->createStreamedResponse(); // adding headers

$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8'); $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment;filename=stdream2.xls'); $response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');

// If you are using a https connection, you have to set those two headers and use sendHeaders() for compatibility with IE <9
$response->headers->set('Pragma', 'public'); $response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1'); $response->headers->set('Cache-Control', 'maxage=1');


Expand All @@ -99,34 +137,13 @@ class DefaultController extends Controller
} }
``` ```


With the correct writer (e.g. PHPExcel_Writer_Excel5) you could also write the output to a file:

``` php

public function indexAction($name)
{
$excelService = $this->get('xls.excel5');
//...load and modify or create php excel object
$excelService->getStreamWriter()->write($filename);
}
```

## Contributors ## Contributors


@pivasyk the [list of contributors](https://github.com/liuggio/ExcelBundle/graphs/contributors)

@dirkbl

@DerStoffel

@artturi

@isqad88

@mazenovi

@gnat42


@jochenhilgers ## Contribute


@Squazic 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 fix .`
23 changes: 3 additions & 20 deletions Resources/config/services.yml
Original file line number Original file line Diff line number Diff line change
@@ -1,23 +1,6 @@
parameters: parameters:
xls.phpexcel.class: PHPExcel phpexcel.class: Liuggio\ExcelBundle\Factory
xls.service.class: Liuggio\ExcelBundle\Service\ExcelContainer


services: services:
xls.phpexcel: phpexcel:
class: %xls.phpexcel.class% class: %phpexcel.class%

xls.service:
class: Liuggio\ExcelBundle\Service\Excel
arguments: ["@xls.phpexcel"]

xls.excel5:
class: Liuggio\ExcelBundle\Service\Excel
arguments: ["@xls.phpexcel" ]
calls:
- [ setType, [ "Excel5" ] ]

xls.excel2007:
class: Liuggio\ExcelBundle\Service\Excel
arguments: ["@xls.phpexcel"]
calls:
- [ setType, [ "Excel2007" ] ]
Loading

0 comments on commit f1924d8

Please sign in to comment.