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
|
|
|
|
10
|
|
|
class TabellaXls |
11
|
|
|
{ |
12
|
|
|
public function esportaexcel($parametri = array()) |
13
|
|
|
{ |
14
|
|
|
set_time_limit(960); |
15
|
|
|
ini_set('memory_limit', '2048M'); |
16
|
|
|
|
17
|
|
|
//Creare un nuovo file |
18
|
|
|
$spreadsheet = new Spreadsheet(); |
19
|
|
|
$objPHPExcel = new Xls($spreadsheet); |
20
|
|
|
$spreadsheet->setActiveSheetIndex(0); |
21
|
|
|
|
22
|
|
|
// Set properties |
23
|
|
|
$spreadsheet->getProperties()->setCreator('Comune di Firenze'); |
24
|
|
|
$spreadsheet->getProperties()->setLastModifiedBy('Comune di Firenze'); |
25
|
|
|
|
26
|
|
|
// $testata = $parametri['testata']; |
|
|
|
|
27
|
|
|
// $rispostaj = $parametri['griglia']; |
28
|
|
|
// |
29
|
|
|
// $modellicolonne = $testata['modellocolonne']; |
30
|
|
|
// |
31
|
|
|
// //Scrittura su file |
32
|
|
|
// $sheet = $spreadsheet->getActiveSheet(); |
33
|
|
|
// $titolosheet = 'Esportazione ' . $testata['tabella']; |
34
|
|
|
// $sheet->setTitle(substr($titolosheet, 0, 30)); |
35
|
|
|
// $sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana'); |
36
|
|
|
// |
37
|
|
|
// $this->printHeaderXls($modellicolonne, $testata, $sheet); |
38
|
|
|
// |
39
|
|
|
// $risposta = json_decode($rispostaj); |
40
|
|
|
// if (isset($risposta->rows)) { |
41
|
|
|
// $righe = $risposta->rows; |
42
|
|
|
// } else { |
43
|
|
|
// $righe = array(); |
44
|
|
|
// } |
45
|
|
|
// |
46
|
|
|
// $this->printBodyXls($righe, $modellicolonne, $sheet); |
47
|
|
|
|
48
|
|
|
//Si crea un oggetto |
49
|
|
|
$todaydate = date('d-m-y'); |
50
|
|
|
|
51
|
|
|
$filename = 'Exportazione'; |
52
|
|
|
$filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true))); |
53
|
|
|
$filename = $filename . '.xls'; |
54
|
|
|
$filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename; |
55
|
|
|
|
56
|
|
|
if (file_exists($filename)) { |
57
|
|
|
unlink($filename); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$objPHPExcel->save($filename); |
61
|
|
|
|
62
|
|
|
return $filename; |
63
|
|
|
} |
64
|
|
|
// private function printHeaderXls($modellicolonne, $testata, $sheet) |
|
|
|
|
65
|
|
|
// { |
66
|
|
|
// $indicecolonnaheader = 0; |
67
|
|
|
// $letteracolonna = 0; |
68
|
|
|
// foreach ($modellicolonne as $modellocolonna) { |
69
|
|
|
// //Si imposta la larghezza delle colonne |
70
|
|
|
// $letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonnaheader); |
71
|
|
|
// $width = (int) $modellocolonna['width'] / 7; |
72
|
|
|
// $indicecolonnaheadertitle = $testata['nomicolonne'][$indicecolonnaheader]; |
73
|
|
|
// $coltitlecalc = isset($indicecolonnaheadertitle) ? $indicecolonnaheadertitle : $modellocolonna['name']; |
74
|
|
|
// $coltitle = strtoupper($coltitlecalc); |
75
|
|
|
// $sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle); |
76
|
|
|
// $sheet->getColumnDimension($letteracolonna)->setWidth($width); |
77
|
|
|
// |
78
|
|
|
// ++$indicecolonnaheader; |
79
|
|
|
// } |
80
|
|
|
// |
81
|
|
|
// if ($indicecolonnaheader > 0) { |
82
|
|
|
// //Si imposta il colore dello sfondo delle celle |
83
|
|
|
// //Colore header |
84
|
|
|
// $style_header = array( |
85
|
|
|
// 'fill' => array( |
86
|
|
|
// 'type' => Fill::FILL_SOLID, |
87
|
|
|
// 'color' => array('rgb' => 'E5E4E2') |
88
|
|
|
// ), |
89
|
|
|
// 'font' => array( |
90
|
|
|
// 'bold' => true |
91
|
|
|
// ) |
92
|
|
|
// ); |
93
|
|
|
// $sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header); |
94
|
|
|
// } |
95
|
|
|
// |
96
|
|
|
// $sheet->getRowDimension('1')->setRowHeight(20); |
97
|
|
|
// } |
98
|
|
|
// private function getValueCell($tipocampo, $vettorecella) |
99
|
|
|
// { |
100
|
|
|
// $valore = null; |
101
|
|
|
// switch ($tipocampo) { |
102
|
|
|
// case 'date': |
103
|
|
|
// $d = (int) substr($vettorecella, 0, 2); |
104
|
|
|
// $m = (int) substr($vettorecella, 3, 2); |
105
|
|
|
// $y = (int) substr($vettorecella, 6, 4); |
106
|
|
|
// $t_date = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($y, $m, $d); |
107
|
|
|
// $valore = $t_date; |
108
|
|
|
// break; |
109
|
|
|
// case 'boolean': |
110
|
|
|
// $valore = ($vettorecella == 1) ? 'SI' : 'NO'; |
111
|
|
|
// break; |
112
|
|
|
// default: |
113
|
|
|
// $valore = $vettorecella; |
114
|
|
|
// break; |
115
|
|
|
// } |
116
|
|
|
// return $valore; |
117
|
|
|
// } |
118
|
|
|
// private function printBodyXls($righe, $modellicolonne, $sheet) |
119
|
|
|
// { |
120
|
|
|
// $row = 2; |
121
|
|
|
// foreach ($righe as $riga) { |
122
|
|
|
// $vettorecelle = $riga->cell; |
123
|
|
|
// $col = 0; |
124
|
|
|
// foreach ($vettorecelle as $vettorecella) { |
125
|
|
|
// if ($vettorecella === '' || $vettorecella === null) { |
126
|
|
|
// $col = $col + 1; |
127
|
|
|
// continue; |
128
|
|
|
// } |
129
|
|
|
// $valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella); |
130
|
|
|
// $sheet->setCellValueByColumnAndRow($col, $row, $valore); |
131
|
|
|
// $col = $col + 1; |
132
|
|
|
// } |
133
|
|
|
// $sheet->getRowDimension($row)->setRowHeight(18); |
134
|
|
|
// ++$row; |
135
|
|
|
// } |
136
|
|
|
// |
137
|
|
|
// $indicecolonna = 0; |
138
|
|
|
// foreach ($modellicolonne as $modellocolonna) { |
139
|
|
|
// $letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonna); |
140
|
|
|
// switch ($modellocolonna['tipocampo']) { |
141
|
|
|
// case 'text': |
142
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
143
|
|
|
// ->getNumberFormat() |
144
|
|
|
// ->setFormatCode("@"); |
145
|
|
|
// break; |
146
|
|
|
// case 'string': |
147
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
148
|
|
|
// ->getNumberFormat() |
149
|
|
|
// ->setFormatCode("@"); |
150
|
|
|
// break; |
151
|
|
|
// case 'integer': |
152
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
153
|
|
|
// ->getNumberFormat() |
154
|
|
|
// ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER); |
155
|
|
|
// break; |
156
|
|
|
// case 'float': |
157
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
158
|
|
|
// ->getNumberFormat() |
159
|
|
|
// ->setFormatCode('#,##0.00'); |
160
|
|
|
// break; |
161
|
|
|
// case 'number': |
162
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
163
|
|
|
// ->getNumberFormat() |
164
|
|
|
// ->setFormatCode('#,##0.00'); |
165
|
|
|
// break; |
166
|
|
|
// case 'datetime': |
167
|
|
|
// //\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYYSLASH |
168
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
169
|
|
|
// ->getNumberFormat() |
170
|
|
|
// ->setFormatCode("dd/mm/yyyy"); |
171
|
|
|
// break; |
172
|
|
|
// case 'date': |
173
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
174
|
|
|
// ->getNumberFormat() |
175
|
|
|
// ->setFormatCode("dd/mm/yyyy"); |
176
|
|
|
// break; |
177
|
|
|
// default: |
178
|
|
|
// $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
179
|
|
|
// ->getNumberFormat() |
180
|
|
|
// ->setFormatCode("@"); |
181
|
|
|
// break; |
182
|
|
|
// } |
183
|
|
|
// |
184
|
|
|
// ++$indicecolonna; |
185
|
|
|
// } |
186
|
|
|
// } |
187
|
|
|
} |
188
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.