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