|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Export; |
|
4
|
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
6
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xls; |
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
|
8
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
|
9
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\DoctrineFieldReader; |
|
10
|
|
|
|
|
11
|
|
|
class TabellaXls |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct($container) |
|
14
|
3 |
|
{ |
|
15
|
|
|
$this->container = $container; |
|
16
|
3 |
|
} |
|
17
|
3 |
|
|
|
18
|
|
|
public function esportaexcel($parametri = array()) |
|
19
|
|
|
{ |
|
20
|
3 |
|
set_time_limit(960); |
|
21
|
3 |
|
ini_set('memory_limit', '2048M'); |
|
22
|
3 |
|
|
|
23
|
|
|
//Creare un nuovo file |
|
24
|
|
|
$spreadsheet = new Spreadsheet(); |
|
25
|
3 |
|
$objPHPExcel = new Xls($spreadsheet); |
|
26
|
3 |
|
$spreadsheet->setActiveSheetIndex(0); |
|
27
|
|
|
|
|
28
|
|
|
// Set properties |
|
29
|
3 |
|
$spreadsheet->getProperties()->setCreator('Comune di Firenze'); |
|
30
|
3 |
|
$spreadsheet->getProperties()->setLastModifiedBy('Comune di Firenze'); |
|
31
|
|
|
|
|
32
|
3 |
|
|
|
33
|
3 |
|
$header = $parametri['parametritabella']; |
|
34
|
3 |
|
$rows = $parametri['recordstabella']; |
|
|
|
|
|
|
35
|
3 |
|
//Scrittura su file |
|
36
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
|
|
|
|
|
|
37
|
3 |
|
$titolosheet = 'Esportazione ' . $parametri['nomecontroller']; |
|
38
|
|
|
$sheet->setTitle(substr($titolosheet, 0, 30)); |
|
39
|
3 |
|
$sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana'); |
|
40
|
|
|
|
|
41
|
|
|
$this->printHeaderXls($header, $sheet); |
|
42
|
3 |
|
|
|
43
|
|
|
$this->printBodyXls($header, $rows, $sheet); |
|
44
|
3 |
|
|
|
45
|
3 |
|
//Si crea un oggetto |
|
46
|
3 |
|
$todaydate = date('d-m-y'); |
|
47
|
3 |
|
|
|
48
|
|
|
$filename = 'Exportazione'; |
|
49
|
3 |
|
$filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true))); |
|
50
|
|
|
$filename = $filename . '.xls'; |
|
51
|
|
|
$filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename; |
|
52
|
|
|
|
|
53
|
3 |
|
if (file_exists($filename)) { |
|
54
|
|
|
unlink($filename); |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
$objPHPExcel->save($filename); |
|
58
|
3 |
|
|
|
59
|
|
|
return $filename; |
|
60
|
3 |
|
} |
|
61
|
3 |
|
|
|
62
|
3 |
|
private function printHeaderXls($testata, $sheet) |
|
63
|
3 |
|
{ |
|
64
|
|
|
$indicecolonnaheader = 1; |
|
65
|
3 |
|
$letteracolonna = 0; |
|
|
|
|
|
|
66
|
3 |
|
foreach ($testata as $modellocolonna) { |
|
67
|
3 |
|
if ($modellocolonna["escluso"] === false) { |
|
|
|
|
|
|
68
|
3 |
|
//Si imposta la larghezza delle colonne |
|
69
|
3 |
|
$letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonnaheader); |
|
|
|
|
|
|
70
|
3 |
|
$width = (int) $modellocolonna['larghezza'] / 7; |
|
|
|
|
|
|
71
|
|
|
$indicecolonnaheadertitle = $modellocolonna['etichetta']; |
|
72
|
3 |
|
$coltitle = strtoupper($indicecolonnaheadertitle); |
|
|
|
|
|
|
73
|
|
|
$sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle); |
|
74
|
|
|
$sheet->getColumnDimension($letteracolonna)->setWidth($width); |
|
75
|
|
|
|
|
76
|
|
|
++$indicecolonnaheader; |
|
77
|
3 |
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
//Imposta lo stile per l'intestazione un po più decente |
|
81
|
3 |
|
$this->setHeaderStyle($sheet, $indicecolonnaheader - 1); |
|
82
|
|
|
|
|
83
|
3 |
|
|
|
84
|
|
|
$sheet->getRowDimension('1')->setRowHeight(20); |
|
85
|
3 |
|
} |
|
86
|
3 |
|
|
|
87
|
3 |
|
private function printBodyXls($header, $rows, $sheet) |
|
88
|
3 |
|
{ |
|
89
|
3 |
|
$row = 2; |
|
90
|
3 |
|
foreach ($rows as $riga) { |
|
91
|
3 |
|
$col = 1; |
|
92
|
3 |
|
foreach ($header as $colonnatestata => $valorecolonnatestata) { |
|
93
|
3 |
|
if ($valorecolonnatestata["escluso"] === false) { |
|
|
|
|
|
|
94
|
3 |
|
$dfr = new DoctrineFieldReader($this->container); |
|
|
|
|
|
|
95
|
3 |
|
$decodiche = isset($valorecolonnatestata["decodifiche"])?$valorecolonnatestata["decodifiche"]:null; |
|
|
|
|
|
|
96
|
3 |
|
$oggetto = $dfr->getField2Object($colonnatestata, $riga, $decodiche); |
|
|
|
|
|
|
97
|
|
|
$valorecampo = $dfr->object2view($oggetto, $valorecolonnatestata["tipocampo"]); |
|
|
|
|
|
|
98
|
3 |
|
if ($valorecampo === '' || $valorecampo === null) { |
|
99
|
3 |
|
$col = $col + 1; |
|
|
|
|
|
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
3 |
|
$sheet->setCellValueByColumnAndRow($col, $row, $this->getValueCell($valorecolonnatestata["tipocampo"], $valorecampo)); |
|
|
|
|
|
|
103
|
3 |
|
$col = $col + 1; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
3 |
|
$sheet->getRowDimension($row)->setRowHeight(18); |
|
107
|
|
|
++$row; |
|
108
|
3 |
|
} |
|
109
|
3 |
|
|
|
110
|
3 |
|
$col = 1; |
|
111
|
3 |
|
//Si impostano i formati cella in base al tipo di dato contenuto |
|
112
|
3 |
|
foreach ($header as $colonnatestata => $valorecolonnatestata) { |
|
113
|
|
|
if ($valorecolonnatestata["escluso"] === false) { |
|
|
|
|
|
|
114
|
|
|
$this->setCellColumnFormat($sheet, $col, $row - 1, $valorecolonnatestata["tipocampo"]); |
|
|
|
|
|
|
115
|
3 |
|
$this->setColumnAutowidth($sheet, $col); |
|
116
|
|
|
$col = $col + 1; |
|
|
|
|
|
|
117
|
3 |
|
} |
|
118
|
|
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
private function setHeaderStyle($sheet, $indiceultimacolonna) |
|
122
|
|
|
{ |
|
123
|
|
|
$letteraultimacolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indiceultimacolonna); |
|
124
|
|
|
$styleArray = [ |
|
|
|
|
|
|
125
|
3 |
|
'font' => [ |
|
126
|
|
|
'bold' => true, |
|
127
|
|
|
], |
|
128
|
|
|
'alignment' => [ |
|
129
|
3 |
|
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, |
|
130
|
|
|
], |
|
131
|
|
|
'borders' => [ |
|
132
|
|
|
'allBorders' => [ |
|
133
|
3 |
|
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, |
|
134
|
|
|
], |
|
135
|
|
|
], |
|
136
|
|
|
'fill' => [ |
|
137
|
|
|
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, |
|
138
|
|
|
'startColor' => [ |
|
139
|
|
|
'argb' => 'e5e5e5e5', |
|
140
|
3 |
|
], |
|
141
|
3 |
|
], |
|
142
|
|
|
]; |
|
143
|
3 |
|
|
|
144
|
|
|
$sheet->getStyle('A1:' . $letteraultimacolonna . '1')->applyFromArray($styleArray); |
|
145
|
3 |
|
} |
|
146
|
3 |
|
|
|
147
|
3 |
|
private function getValueCell($tipocampo, $valorecella) |
|
148
|
1 |
|
{ |
|
149
|
1 |
|
$valore = null; |
|
150
|
1 |
|
switch ($tipocampo) { |
|
151
|
1 |
|
case 'date': |
|
152
|
1 |
|
$d = (int) substr($valorecella, 0, 2); |
|
|
|
|
|
|
153
|
1 |
|
$m = (int) substr($valorecella, 3, 2); |
|
|
|
|
|
|
154
|
3 |
|
$y = (int) substr($valorecella, 6, 4); |
|
|
|
|
|
|
155
|
2 |
|
$t_date = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($y, $m, $d); |
|
156
|
2 |
|
$valore = $t_date; |
|
157
|
2 |
|
break; |
|
158
|
2 |
|
case 'datetime': |
|
159
|
2 |
|
$d = (int) substr($valorecella, 0, 2); |
|
|
|
|
|
|
160
|
2 |
|
$m = (int) substr($valorecella, 3, 2); |
|
|
|
|
|
|
161
|
2 |
|
$y = (int) substr($valorecella, 6, 4); |
|
|
|
|
|
|
162
|
2 |
|
$h = (int) substr($valorecella, 11, 2); |
|
|
|
|
|
|
163
|
|
|
$i = (int) substr($valorecella, 14, 2); |
|
|
|
|
|
|
164
|
3 |
|
$t_date = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($y, $m, $d, $h, $i, 0); |
|
165
|
3 |
|
$valore = $t_date; |
|
166
|
|
|
break; |
|
167
|
3 |
|
default: |
|
168
|
|
|
$valore = $valorecella; |
|
169
|
|
|
break; |
|
170
|
3 |
|
} |
|
171
|
|
|
return $valore; |
|
172
|
3 |
|
} |
|
173
|
3 |
|
|
|
174
|
3 |
|
private function setCellColumnFormat($sheet, $indicecolonna, $lastrow, $tipocampo) |
|
175
|
1 |
|
{ |
|
176
|
1 |
|
$letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonna); |
|
177
|
1 |
|
switch ($tipocampo) { |
|
178
|
1 |
|
case 'text': |
|
179
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
180
|
3 |
|
->getNumberFormat() |
|
181
|
3 |
|
->setFormatCode("@"); |
|
|
|
|
|
|
182
|
3 |
|
break; |
|
183
|
3 |
|
case 'string': |
|
184
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
185
|
3 |
|
->getNumberFormat() |
|
186
|
3 |
|
->setFormatCode("@"); |
|
|
|
|
|
|
187
|
3 |
|
break; |
|
188
|
3 |
|
case 'integer': |
|
189
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
190
|
|
|
->getNumberFormat() |
|
191
|
|
|
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER); |
|
192
|
|
|
break; |
|
193
|
|
|
case 'float': |
|
194
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
195
|
1 |
|
->getNumberFormat() |
|
196
|
1 |
|
->setFormatCode('#,##0.00'); |
|
197
|
1 |
|
break; |
|
198
|
1 |
|
case 'decimal': |
|
199
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
200
|
|
|
->getNumberFormat() |
|
201
|
|
|
->setFormatCode('#,##0.00'); |
|
202
|
|
|
break; |
|
203
|
|
|
case 'number': |
|
204
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
205
|
|
|
->getNumberFormat() |
|
206
|
2 |
|
->setFormatCode('#,##0.00'); |
|
207
|
2 |
|
break; |
|
208
|
2 |
|
case 'datetime': |
|
209
|
2 |
|
//\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYYSLASH |
|
210
|
3 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
211
|
1 |
|
->getNumberFormat() |
|
212
|
1 |
|
->setFormatCode("dd/mm/yyyy hh:mm:ss"); |
|
|
|
|
|
|
213
|
1 |
|
break; |
|
214
|
1 |
|
case 'date': |
|
215
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
216
|
3 |
|
->getNumberFormat() |
|
217
|
3 |
|
->setFormatCode("dd/mm/yyyy"); |
|
|
|
|
|
|
218
|
3 |
|
break; |
|
219
|
3 |
|
default: |
|
220
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $lastrow) |
|
221
|
3 |
|
->getNumberFormat() |
|
222
|
|
|
->setFormatCode("@"); |
|
|
|
|
|
|
223
|
3 |
|
break; |
|
224
|
|
|
} |
|
225
|
3 |
|
} |
|
226
|
3 |
|
|
|
227
|
3 |
|
private function setColumnAutowidth($sheet, $indicecolonna) |
|
228
|
|
|
{ |
|
229
|
|
|
$letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonna); |
|
230
|
|
|
$sheet->getColumnDimension($letteracolonna)->setAutoSize(true); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.