Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 60458e4

Browse files
committedJun 19, 2014
Updated Excel export
From now on, the Excel export supports ManyToOne relations. Also DateTime objects can be used fo the export. Doc is updated.
1 parent 7170743 commit 60458e4

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed
 

‎Builder/Admin/ExcelBuilder.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ public function getYamlKey()
2424
}
2525

2626
public function getFileName(){
27-
if(null === ($filename =$this->getVariable('filename'))){
27+
if(null === ($filename = $this->getVariable('filename'))){
2828
$filename = 'admin_export_'. str_replace(' ', '_', strtolower($this->getGenerator()->getFromYaml('builders.list.params.title')));
2929
}
3030
return $filename;
3131
}
3232

3333
public function getFileType(){
34-
if(null === ($filetype =$this->getVariable('filetype'))){
34+
if(null === ($filetype = $this->getVariable('filetype'))){
3535
$filetype = 'Excel2007';
3636
}
3737
return $filetype;
3838
}
39+
40+
public function getDateTimeFormat(){
41+
if(null === ($dateTimeFormat = $this->getVariable('datetime_format'))){
42+
$dateTimeFormat = 'Y-m-d H:i:s';
43+
}
44+
return $dateTimeFormat;
45+
}
3946
}

‎Resources/doc/builders/excel-builder.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
To use the Excel export make sure you have installed the recommended dependency `liuggio/excelbundle` (version `>= 2.0`) and enabled it in your AppKernel.php (`new Liuggio\ExcelBundle\LiuggioExcelBundle()`). Without this bundle enabled the ExcelAction will not work.
1111

1212
### 1. Usage
13-
The Excel builder provides an Excel export of the list view. The export uses the data that is visible on the screen (and the next pages) and thus uses the filters and scopes selected.
13+
The Excel builder provides an Excel export of the list view. The export uses the data that is visible on the screen (and the next pages) and thus uses the filters and scopes selected. You can use object associations by just entering a dot.
1414

1515
### 2. Options
1616

1717
* `display`: The same as in list/new/show/edit, selects the columns to export
1818
* `filename`: Specify the export filename. When null 'admin_export_{list title}' is used
1919
* `filetype`: Default Excel2007. See the [excelbundle documention](https://github.com/liuggio/excelbundle#not-only-excel5) for the possible options.
20+
* `datetime_format`: Specify the DateTime format to be used in the Excel export. Default is `Y-m-d H:i:s`.
21+
22+
### 3. Header titles in Excel
23+
24+
The header titles are fully customizable just as every other block. Just use the `fields` key directly under the builder. This even works for associated fields like `person.fullname`.

‎Resources/templates/CommonAdmin/ExcelAction/ExcelBuilderAction.php.twig

+17-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,23 @@ class ExcelController extends ListController
7777
foreach($results as $rowData){
7878
{% set column = 0 %}
7979
{% for builder_column in builder.columns %}
80-
$sheet->setCellValueByColumnAndRow({{ column }}, $row, $rowData->get{{ builder_column.getter|capitalize }}());
80+
81+
// Retrieve relations
82+
$getter = '{{ builder_column.getter }}';
83+
$data = $rowData;
84+
while (($pos = strpos($getter, '.')) > 0){
85+
$tempGetter = 'get' . ucfirst(substr($getter, 0, $pos));
86+
$getter = substr($getter, $pos + 1);
87+
$data = $data->$tempGetter();
88+
}
89+
$getter = 'get' . ucfirst($getter);
90+
$data = $data->$getter();
91+
92+
// Convert DateTime object to given format
93+
if ($data instanceof \DateTime){
94+
$data = $data->format('{{ builder.dateTimeFormat }}');
95+
}
96+
$sheet->setCellValueByColumnAndRow({{ column }}, $row, $data);
8197
{% set column = column + 1 %}
8298
{% endfor %}
8399
$row++;

0 commit comments

Comments
 (0)
Please sign in to comment.