|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
|
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 EsportaTabellaXls |
|
11
|
|
|
{ |
|
12
|
1 |
|
public function esportaexcel($parametri = array()) |
|
13
|
|
|
{ |
|
14
|
1 |
|
set_time_limit(960); |
|
15
|
1 |
|
ini_set('memory_limit', '2048M'); |
|
16
|
|
|
|
|
17
|
|
|
//Creare un nuovo file |
|
18
|
1 |
|
$spreadsheet = new Spreadsheet(); /* ----Spreadsheet object----- */ |
|
19
|
|
|
$objPHPExcel = new Xls($spreadsheet); /* ----- Excel (Xls) Object */ |
|
20
|
1 |
|
$spreadsheet->setActiveSheetIndex(0); |
|
21
|
|
|
|
|
22
|
|
|
// Set properties |
|
23
|
1 |
|
$spreadsheet->getProperties()->setCreator('Comune di Firenze'); |
|
24
|
1 |
|
$spreadsheet->getProperties()->setLastModifiedBy('Comune di Firenze'); |
|
25
|
|
|
|
|
26
|
1 |
|
$testata = $parametri['testata']; |
|
27
|
1 |
|
$rispostaj = $parametri['griglia']; |
|
28
|
|
|
|
|
29
|
1 |
|
$modellicolonne = $testata['modellocolonne']; |
|
30
|
|
|
|
|
31
|
|
|
//Scrittura su file |
|
32
|
1 |
|
$sheet = $spreadsheet->getActiveSheet(); |
|
33
|
1 |
|
$titolosheet = 'Esportazione ' . $testata['tabella']; |
|
34
|
1 |
|
$sheet->setTitle(substr($titolosheet, 0, 30)); |
|
35
|
1 |
|
$sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana'); |
|
36
|
|
|
|
|
37
|
1 |
|
$this->printHeaderXls($modellicolonne, $testata, $sheet); |
|
38
|
|
|
|
|
39
|
1 |
|
$risposta = json_decode($rispostaj); |
|
40
|
1 |
|
if (isset($risposta->rows)) { |
|
41
|
1 |
|
$righe = $risposta->rows; |
|
42
|
|
|
} else { |
|
43
|
|
|
$righe = array(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
$this->printBodyXls($righe, $modellicolonne, $sheet); |
|
47
|
|
|
|
|
48
|
|
|
//Si crea un oggetto |
|
49
|
1 |
|
$todaydate = date('d-m-y'); |
|
50
|
|
|
|
|
51
|
1 |
|
$filename = 'Exportazione_' . $testata['tabella']; |
|
52
|
1 |
|
$filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true))); |
|
53
|
1 |
|
$filename = $filename . '.xls'; |
|
54
|
1 |
|
$filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename; |
|
55
|
|
|
|
|
56
|
1 |
|
if (file_exists($filename)) { |
|
57
|
|
|
unlink($filename); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
$objPHPExcel->save($filename); |
|
61
|
|
|
|
|
62
|
1 |
|
return $filename; |
|
63
|
|
|
} |
|
64
|
2 |
|
private function printHeaderXls($modellicolonne, $testata, $sheet) |
|
65
|
|
|
{ |
|
66
|
2 |
|
$indicecolonnaheader = 0; |
|
67
|
2 |
|
$letteracolonna = 0; |
|
68
|
2 |
|
foreach ($modellicolonne as $modellocolonna) { |
|
69
|
|
|
//Si imposta la larghezza delle colonne |
|
70
|
2 |
|
$letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonnaheader); |
|
71
|
2 |
|
$width = (int) $modellocolonna['width'] / 7; |
|
72
|
2 |
|
$indicecolonnaheadertitle = $testata['nomicolonne'][$indicecolonnaheader]; |
|
73
|
2 |
|
$coltitlecalc = isset($indicecolonnaheadertitle) ? $indicecolonnaheadertitle : $modellocolonna['name']; |
|
74
|
2 |
|
$coltitle = strtoupper($coltitlecalc); |
|
75
|
2 |
|
$sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle); |
|
76
|
2 |
|
$sheet->getColumnDimension($letteracolonna)->setWidth($width); |
|
77
|
|
|
|
|
78
|
2 |
|
++$indicecolonnaheader; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
if ($indicecolonnaheader > 0) { |
|
82
|
|
|
//Si imposta il colore dello sfondo delle celle |
|
83
|
|
|
//Colore header |
|
84
|
|
|
$style_header = array( |
|
85
|
2 |
|
'fill' => array( |
|
86
|
2 |
|
'type' => Fill::FILL_SOLID, |
|
87
|
|
|
'color' => array('rgb' => 'E5E4E2') |
|
88
|
|
|
), |
|
89
|
|
|
'font' => array( |
|
90
|
|
|
'bold' => true |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
2 |
|
$sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
$sheet->getRowDimension('1')->setRowHeight(20); |
|
97
|
2 |
|
} |
|
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
|
2 |
|
private function printBodyXls($righe, $modellicolonne, $sheet) |
|
119
|
|
|
{ |
|
120
|
2 |
|
$row = 2; |
|
121
|
2 |
|
foreach ($righe as $riga) { |
|
122
|
2 |
|
$vettorecelle = $riga->cell; |
|
123
|
2 |
|
$col = 0; |
|
124
|
2 |
|
foreach ($vettorecelle as $vettorecella) { |
|
125
|
2 |
|
if ($vettorecella === '' || $vettorecella === null) { |
|
126
|
1 |
|
$col = $col + 1; |
|
127
|
1 |
|
continue; |
|
128
|
|
|
} |
|
129
|
2 |
|
$valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella); |
|
130
|
2 |
|
$sheet->setCellValueByColumnAndRow($col, $row, $valore); |
|
131
|
2 |
|
$col = $col + 1; |
|
132
|
|
|
} |
|
133
|
2 |
|
$sheet->getRowDimension($row)->setRowHeight(18); |
|
134
|
2 |
|
++$row; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
$indicecolonna = 0; |
|
138
|
2 |
|
foreach ($modellicolonne as $modellocolonna) { |
|
139
|
2 |
|
$letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonna); |
|
140
|
2 |
|
switch ($modellocolonna['tipocampo']) { |
|
141
|
2 |
|
case 'text': |
|
142
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
143
|
|
|
->getNumberFormat() |
|
144
|
|
|
->setFormatCode("@"); |
|
145
|
|
|
break; |
|
146
|
2 |
|
case 'string': |
|
147
|
2 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
148
|
2 |
|
->getNumberFormat() |
|
149
|
2 |
|
->setFormatCode("@"); |
|
150
|
2 |
|
break; |
|
151
|
2 |
|
case 'integer': |
|
152
|
2 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
153
|
2 |
|
->getNumberFormat() |
|
154
|
2 |
|
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER); |
|
155
|
2 |
|
break; |
|
156
|
1 |
|
case 'float': |
|
157
|
1 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
158
|
1 |
|
->getNumberFormat() |
|
159
|
1 |
|
->setFormatCode('#,##0.00'); |
|
160
|
1 |
|
break; |
|
161
|
1 |
|
case 'number': |
|
162
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
163
|
|
|
->getNumberFormat() |
|
164
|
|
|
->setFormatCode('#,##0.00'); |
|
165
|
|
|
break; |
|
166
|
1 |
|
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
|
1 |
|
case 'date': |
|
173
|
1 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
174
|
1 |
|
->getNumberFormat() |
|
175
|
1 |
|
->setFormatCode("dd/mm/yyyy"); |
|
176
|
1 |
|
break; |
|
177
|
|
|
default: |
|
178
|
1 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
179
|
1 |
|
->getNumberFormat() |
|
180
|
1 |
|
->setFormatCode("@"); |
|
181
|
1 |
|
break; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
2 |
|
++$indicecolonna; |
|
185
|
|
|
} |
|
186
|
2 |
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|