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