1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
class EsportaTabellaXls |
6
|
|
|
{ |
7
|
|
|
public function esportaexcel($parametri = array()) |
8
|
|
|
{ |
9
|
|
|
set_time_limit(960); |
10
|
|
|
ini_set('memory_limit', '2048M'); |
11
|
|
|
|
12
|
|
|
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp; |
|
|
|
|
13
|
|
|
$cacheSettings = array('memoryCacheSize' => '8MB'); |
14
|
|
|
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); |
15
|
|
|
|
16
|
|
|
//Creare un nuovo file |
17
|
|
|
$objPHPExcel = new \PHPExcel(); |
18
|
|
|
|
19
|
|
|
$objPHPExcel->setActiveSheetIndex(0); |
20
|
|
|
|
21
|
|
|
// Set properties |
22
|
|
|
$objPHPExcel->getProperties()->setCreator('Comune di Firenze'); |
23
|
|
|
$objPHPExcel->getProperties()->setLastModifiedBy('Comune di Firenze'); |
24
|
|
|
|
25
|
|
|
$testata = $parametri['testata']; |
|
|
|
|
26
|
|
|
$rispostaj = $parametri['griglia']; |
27
|
|
|
|
28
|
|
|
$modellicolonne = $testata['modellocolonne']; |
29
|
|
|
|
30
|
|
|
//Scrittura su file |
31
|
|
|
$sheet = $objPHPExcel->getActiveSheet(); |
|
|
|
|
32
|
|
|
$titolosheet = 'Esportazione ' . $testata['tabella']; |
33
|
|
|
$sheet->setTitle(substr($titolosheet, 0, 30)); |
34
|
|
|
$sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana'); |
35
|
|
|
|
36
|
|
|
$this->printHeaderXls($modellicolonne, $testata, $sheet); |
37
|
|
|
|
38
|
|
|
$risposta = json_decode($rispostaj); |
39
|
|
|
if (isset($risposta->rows)) { |
40
|
|
|
$righe = $risposta->rows; |
41
|
|
|
} else { |
42
|
|
|
$righe = array(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->printBodyXls($righe, $modellicolonne, $sheet); |
46
|
|
|
|
47
|
|
|
//Si crea un oggetto |
48
|
|
|
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); |
49
|
|
|
$todaydate = date('d-m-y'); |
50
|
|
|
|
51
|
|
|
$filename = 'Exportazione_' . $testata['tabella']; |
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
|
|
|
$objWriter->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 = \PHPExcel_Cell::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' => \PHPExcel_Style_Fill::FILL_SOLID, |
87
|
|
|
'color' => array('rgb' => '0404B4'), |
88
|
|
|
), |
89
|
|
|
'font' => array( |
90
|
|
|
'bold' => true, |
91
|
|
|
'color' => array('rgb' => 'FFFFFF'), |
92
|
|
|
), |
93
|
|
|
); |
94
|
|
|
$sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$sheet->getRowDimension('1')->setRowHeight(20); |
98
|
|
|
} |
99
|
|
|
private function getValueCell($tipocampo, $vettorecella) |
100
|
|
|
{ |
101
|
|
|
$valore = null; |
102
|
|
|
switch ($tipocampo) { |
103
|
|
|
case 'date': |
104
|
|
|
$d = (int) substr($vettorecella, 0, 2); |
|
|
|
|
105
|
|
|
$m = (int) substr($vettorecella, 3, 2); |
|
|
|
|
106
|
|
|
$y = (int) substr($vettorecella, 6, 4); |
|
|
|
|
107
|
|
|
$t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel($y, $m, $d); |
|
|
|
|
108
|
|
|
$valore = $t_date; |
109
|
|
|
break; |
110
|
|
|
case 'boolean': |
111
|
|
|
$valore = ($vettorecella == 1) ? 'SI' : 'NO'; |
112
|
|
|
break; |
113
|
|
|
default: |
114
|
|
|
$valore = $vettorecella; |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
return $valore; |
118
|
|
|
} |
119
|
|
|
private function printBodyXls($righe, $modellicolonne, $sheet) |
120
|
|
|
{ |
121
|
|
|
$row = 2; |
122
|
|
|
foreach ($righe as $riga) { |
123
|
|
|
$vettorecelle = $riga->cell; |
124
|
|
|
$col = 0; |
|
|
|
|
125
|
|
|
foreach ($vettorecelle as $vettorecella) { |
126
|
|
|
if ($vettorecella === '' || $vettorecella === null) { |
127
|
|
|
$col = $col + 1; |
|
|
|
|
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
$valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella); |
131
|
|
|
$sheet->setCellValueByColumnAndRow($col, $row, $valore); |
132
|
|
|
$col = $col + 1; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
$sheet->getRowDimension($row)->setRowHeight(18); |
135
|
|
|
++$row; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$indicecolonna = 0; |
139
|
|
|
foreach ($modellicolonne as $modellocolonna) { |
140
|
|
|
$letteracolonna = \PHPExcel_Cell::stringFromColumnIndex($indicecolonna); |
141
|
|
|
switch ($modellocolonna['tipocampo']) { |
142
|
|
|
case 'text': |
143
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
144
|
|
|
->getNumberFormat() |
145
|
|
|
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
146
|
|
|
break; |
147
|
|
|
case 'string': |
148
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
149
|
|
|
->getNumberFormat() |
150
|
|
|
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
151
|
|
|
break; |
152
|
|
|
case 'integer': |
153
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
154
|
|
|
->getNumberFormat() |
155
|
|
|
->setFormatCode(\PHPExcel_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
|
|
|
$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(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
181
|
|
|
break; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
++$indicecolonna; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.