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