-
Notifications
You must be signed in to change notification settings - Fork 125
/
ExcelBuilderAction.php.twig
114 lines (99 loc) · 3.91 KB
/
ExcelBuilderAction.php.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{% use '../CommonAdmin/security_action.php.twig' %}
<?php
namespace Admingenerated\{{ namespace_prefix }}{{ bundle_name }}\{{ builder.generator.GeneratedControllerFolder }};
{{- block('security_use') }}
{{- block('csrf_protection_use') }}
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
/**
* @author Bob van de Vijver
*/
class ExcelController extends ListController
{
/**
* Generates the Excel object and send a streamed response
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function excelAction()
{
{{ block('security_action') }}
// Create the PHPExcel object with some standard values
try {
$phpexcel = $this->get('phpexcel');
} catch (ServiceNotFoundException $e){
throw new \Exception('You will need to enable the PHPExcel bundle for this function to work.', null, $e);
}
$phpExcelObject = $phpexcel->createPHPExcelObject();
$this->createExcelObject($phpExcelObject);
$sheet = $phpExcelObject->setActiveSheetIndex(0);
// Create the first bold row in the Excel spreadsheet
$this->createExcelHeader($sheet);
// Print the data
$this->createExcelData($sheet);
// Create the Writer, Response and add header
$writer = $phpexcel->createWriter($phpExcelObject, '{{ builder.filetype }}');
$response = $phpexcel->createStreamedResponse($writer);
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment;filename={{ builder.filename }}.xlsx');
return $response;
}
/**
* Override this method to add your own creator, title and more to the Excel spreadsheet
*/
protected function createExcelObject(\PHPExcel $phpExcelObject)
{
$phpExcelObject->getProperties()->setCreator("AdminGeneratorBundle")
->setTitle('AdminGenerator Excel Export')
->setSubject("AdminGenerator Excel Export")
->setDescription("AdminGenerator Excel export");
}
/**
* Fill the Excel speadsheet with the headers
*/
protected function createExcelheader(\PhpExcel_Worksheet $sheet)
{
{% set colNum = 0 %}
{% for column in builder.columns %}
$sheet->setCellValueByColumnAndRow({{ colNum }}, 1, "{{ column.label }}", true);
$columnLetter = \PHPExcel_Cell::stringFromColumnIndex({{ colNum }});
$sheet->getStyle($columnLetter . '1')->getFont()->setBold(true);
$sheet->getColumnDimension($columnLetter)->setAutoSize(true);
{% set colNum = colNum + 1 %}
{% endfor %}
}
/**
* Fills the Excel spreadsheet with data
*/
protected function createExcelData(\PhpExcel_Worksheet $sheet)
{
$row = 2;
$results = $this->getResults();
foreach($results as $rowData) {
{% set colNum = 0 %}
{% for column in builder.columns %}
// Retrieve relations
$getter = '{{ column.getter }}';
$data = $rowData;
while (($pos = strpos($getter, '.')) > 0) {
$tempGetter = 'get' . ucfirst(substr($getter, 0, $pos));
$getter = substr($getter, $pos + 1);
$data = $data->$tempGetter();
}
$getter = 'get' . ucfirst($getter);
$data = $data->$getter();
// Convert DateTime object to given format
if ($data instanceof \DateTime){
$data = $data->format('{{ builder.dateTimeFormat }}');
}
$sheet->setCellValueByColumnAndRow({{ colNum }}, $row, $data);
{% set colNum = colNum + 1 %}
{% endfor %}
$row++;
}
}
{% block getResults -%}
protected function getResults()
{
// ORM JOB
}
{% endblock %}
}