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