Skip to content

Commit

Permalink
Group By example
Browse files Browse the repository at this point in the history
  • Loading branch information
Viburnum committed Aug 17, 2016
1 parent 74c506a commit 8056cda
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/app/logs/*
!app/cache/.gitkeep
!app/logs/.gitkeep
/examples/*.xlsx
/build/
/vendor/
/bin/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ composer require stinger-soft/excel-creator

* [Simple Binding](docs/SimpleBinding.md)
* [Horizontal outline](docs/OutlinedBinding.md)
* [Group rows](docs/GroupedBinding.md)
27 changes: 27 additions & 0 deletions docs/GroupedBinding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# GroupedBinding

To group data by a property (i.e. vertical outline) you have to define the corresponding column binding as a group-by-binding.

## Code
```php
<?php

...

$zipBinding = new ColumnBinding();
$zipBinding->setLabel('Zipcode');
$zipBinding->setBinding('zipcode');
$zipBinding->setColumnWidth('auto');
$zipBinding->setOutline(1);
$sheet1->addColumnBinding($zipBinding);

//Group results by zip code
$sheet1->setGroupByBinding($zipBinding);

...

```

## Result

![GroupedBinding Result](images/grouped_binding.png "GroupedBinding Result")
2 changes: 2 additions & 0 deletions docs/OutlinedBinding.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The following example extends the [Simple Binding](SimpleBinding.md) example by adding more information (i.e. columns) to the excel file and groups them by specifying an outline for each additional binding.

Please keep in mind that Excel currently supports outline up to level 6. Higher levels are ignored.

## Code
```php
<?php
Expand Down
Binary file added docs/images/grouped_binding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions examples/GroupedBinding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use StingerSoft\ExcelCreator\ConfiguredExcel;
use StingerSoft\ExcelCreator\ColumnBinding;

include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/Person.php';

//Create excel file
$excel = new ConfiguredExcel();

//and a first sheet called 'Party guests'
$sheet1 = $excel->addSheet('Party guests');

//Add a column with the caption 'name'
$nameBinding = new ColumnBinding();
$nameBinding->setLabel('Name');

//bind it to the property of the data to be rendered by specifying the property path (see http://symfony.com/doc/current/components/property_access.html )
$nameBinding->setBinding('name');

//Width will be calculated by excel
$nameBinding->setColumnWidth('auto');
$sheet1->addColumnBinding($nameBinding);


//Add a column with the caption 'E-Mail'
$emailBinding = new ColumnBinding();
$emailBinding->setLabel('E-Mail');
$emailBinding->setBinding('email');
$emailBinding->setColumnWidth('auto');
$sheet1->addColumnBinding($emailBinding);

//Add other information in outline 1
$streetBinding = new ColumnBinding();
$streetBinding->setLabel('Address');
$streetBinding->setBinding('address');
$streetBinding->setColumnWidth('auto');
$sheet1->addColumnBinding($streetBinding);

$cityBinding = new ColumnBinding();
$cityBinding->setLabel('City');
$cityBinding->setBinding('city');
$cityBinding->setColumnWidth('auto');
$sheet1->addColumnBinding($cityBinding);

$zipBinding = new ColumnBinding();
$zipBinding->setLabel('Zipcode');
$zipBinding->setBinding('zipcode');
$zipBinding->setColumnWidth('auto');
$sheet1->addColumnBinding($zipBinding);

//Group results by zip code
$sheet1->setGroupByBinding($zipBinding);

$guests = array();

$person = new Person('Peter Mobb', 'peter@mobbtrix.de');
$person->setAddress('Musterstraße 1');
$person->setCity('Bremen');
$person->setZipCode(28357);
$guests[] = $person;

$person = new Person('Oliver Kotte', 'oliver.kotte@stinger-soft.net');
$person->setAddress('Musterstraße 2');
$person->setCity('Bremen');
$person->setZipCode(28357);
$guests[] = $person;

$person = new Person('Florian Meyer', 'florian.meyer@stinger-soft.net');
$person->setAddress('Musterstraße 3');
$person->setCity('Bremen');
$person->setZipCode(28357);
$guests[] = $person;

$person = new Person('Peter und Uschi', 'peter_uschi@meppen.de');
$person->setAddress('Meppstraße 1');
$person->setCity('Meppen');
$person->setZipCode(49716);
$guests[] = $person;

$sheet1->setData($guests);
$sheet1->applyData();

\PHPExcel_IOFactory::createWriter($excel->getPhpExcel(), 'Excel2007')->save(__DIR__.'/grouped_binding.xlsx');

0 comments on commit 8056cda

Please sign in to comment.