1
|
|
|
<?php |
2
|
|
|
namespace Ellumilel\Xl\Worksheets; |
3
|
|
|
|
4
|
|
|
use Ellumilel\Helpers\ExcelHelper; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @link https://msdn.microsoft.com/en-us/library/bb264572(v=office.12).aspx |
8
|
|
|
* |
9
|
|
|
* Class SheetXml |
10
|
|
|
* @package Ellumilel\Xl\Worksheets |
11
|
|
|
* @author Denis Tikhonov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class SheetXml |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
private $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $urlOpenXmlFormat = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; |
19
|
|
|
/** @var string */ |
20
|
|
|
private $urlSchemaFormat = 'http://schemas.openxmlformats.org/officeDocument/2006'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function getXml() |
26
|
|
|
{ |
27
|
|
|
return $this->xml; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getSheetPr() |
34
|
|
|
{ |
35
|
|
|
$sPr = '<sheetPr filterMode="false">'; |
36
|
|
|
$sPr .= '<pageSetUpPr fitToPage="false"/>'; |
37
|
|
|
$sPr .= '</sheetPr>'; |
38
|
|
|
|
39
|
|
|
return $sPr; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getWorksheet() |
46
|
|
|
{ |
47
|
|
|
$wSheet = '<worksheet xmlns="'.$this->urlOpenXmlFormat.'" xmlns:r="'.$this->urlSchemaFormat.'/relationships">'; |
48
|
|
|
|
49
|
|
|
return $wSheet; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $selectedTab |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getSheetViews($selectedTab) |
58
|
|
|
{ |
59
|
|
|
$sViews = '<sheetViews>'; |
60
|
|
|
$sViews .= '<sheetView colorId="64" defaultGridColor="true" rightToLeft="false" showFormulas="false"'; |
61
|
|
|
$sViews .= ' showGridLines="true" showOutlineSymbols="true" showRowColHeaders="true" showZeros="true"'; |
62
|
|
|
$sViews .= ' tabSelected="'.$selectedTab.'" topLeftCell="A1" view="normal" windowProtection="false"'; |
63
|
|
|
$sViews .= ' workbookViewId="0" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100">'; |
64
|
|
|
$sViews .= '<selection activeCell="A1" activeCellId="0" pane="topLeft" sqref="A1"/>'; |
65
|
|
|
$sViews .= '</sheetView>'; |
66
|
|
|
$sViews .= '</sheetViews>'; |
67
|
|
|
|
68
|
|
|
return $sViews; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getCools() |
75
|
|
|
{ |
76
|
|
|
$sCols = '<cols>'; |
77
|
|
|
$sCols .= '<col collapsed="false" hidden="false" max="1025" min="1" style="0" width="11.5"/>'; |
78
|
|
|
$sCols .= '</cols>'; |
79
|
|
|
|
80
|
|
|
return $sCols; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $maxCell |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getDimension($maxCell) |
89
|
|
|
{ |
90
|
|
|
$sCols = '<dimension ref="A1:'.$maxCell.'"/>'; |
91
|
|
|
|
92
|
|
|
return $sCols; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @todo refactor |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getHeaderFooter() |
101
|
|
|
{ |
102
|
|
|
$hf = '<headerFooter differentFirst="false" differentOddEven="false">'; |
103
|
|
|
$hf .= '<oddHeader>&C&"Times New Roman,Regular"&12&A</oddHeader>'; |
104
|
|
|
$hf .= '<oddFooter>&C&"Times New Roman,Regular"&12Page &P</oddFooter>'; |
105
|
|
|
$hf .= '</headerFooter>'; |
106
|
|
|
|
107
|
|
|
return $hf; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getPageSetup() |
114
|
|
|
{ |
115
|
|
|
$ps = '<pageSetup blackAndWhite="false" cellComments="none" copies="1" draft="false" firstPageNumber="1"'; |
116
|
|
|
$ps .= ' fitToHeight="1" fitToWidth="1" horizontalDpi="300" orientation="portrait" pageOrder="downThenOver"'; |
117
|
|
|
$ps .= ' paperSize="1" scale="100" useFirstPageNumber="true" usePrinterDefaults="false" verticalDpi="300"/>'; |
118
|
|
|
|
119
|
|
|
return $ps; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getPageMargins() |
126
|
|
|
{ |
127
|
|
|
return '<pageMargins left="0.5" right="0.5" top="1.0" bottom="1.0" header="0.5" footer="0.5"/>'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getPrintOptions() |
134
|
|
|
{ |
135
|
|
|
return '<printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" |
136
|
|
|
verticalCentered="false"/>'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param array $mergeCells |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getMergeCells(array $mergeCells) |
145
|
|
|
{ |
146
|
|
|
$mc = '<mergeCells>'; |
147
|
|
|
foreach ($mergeCells as $range) { |
148
|
|
|
$mc .= '<mergeCell ref="'.$range.'"/>'; |
149
|
|
|
} |
150
|
|
|
$mc .= '</mergeCells>'; |
151
|
|
|
|
152
|
|
|
return $mc; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $cellName |
157
|
|
|
* @param $cellIndex |
158
|
|
|
* @param $cellType |
159
|
|
|
* @param $value |
160
|
|
|
* |
161
|
|
|
* @return bool|string |
162
|
|
|
*/ |
163
|
|
|
public function getCell($cellName, $cellIndex, $cellType, $value) |
164
|
|
|
{ |
165
|
|
|
if ($cellType == '@') { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (!is_scalar($value) || $value === '') { |
170
|
|
|
return '<c r="'.$cellName.'" s="'.$cellIndex.'"/>'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (is_string($value) && $value{0} == '=') { |
174
|
|
|
return $this->getFormulaCell($cellName, $cellIndex, $value); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($cellType == 'date' || $cellType == 'datetime') { |
178
|
|
|
return $this->getDateCell($cellName, $cellIndex, $value); |
179
|
|
|
} elseif ($cellType == 'currency' || $cellType == 'percent' || $cellType == 'numeric') { |
180
|
|
|
return $this->getCurrencyCell($cellName, $cellIndex, $value); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this->checkIntCell($cellName, $cellIndex, $value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param $cellName |
188
|
|
|
* @param $cellIndex |
189
|
|
|
* @param $value |
190
|
|
|
* |
191
|
|
|
* @return bool|string |
192
|
|
|
*/ |
193
|
|
|
private function checkIntCell($cellName, $cellIndex, $value) |
194
|
|
|
{ |
195
|
|
|
if (!is_string($value)) { |
196
|
|
|
return $this->getIntCell($cellName, $cellIndex, $value); |
197
|
|
|
} else { |
198
|
|
|
if ($value{0} != '0' && |
199
|
|
|
$value{0} != '+' && |
200
|
|
|
filter_var( |
201
|
|
|
$value, |
202
|
|
|
FILTER_VALIDATE_INT, |
203
|
|
|
['options' => ['max_range' => ExcelHelper::EXCEL_MAX_RANGE]] |
204
|
|
|
) |
205
|
|
|
) { |
206
|
|
|
return $this->getIntCell($cellName, $cellIndex, $value); |
207
|
|
|
} else { |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param $cellName |
215
|
|
|
* @param $cellIndex |
216
|
|
|
* @param $value |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
private function getDateCell($cellName, $cellIndex, $value) |
221
|
|
|
{ |
222
|
|
|
return sprintf( |
223
|
|
|
'<c r="%s" s="%s" t="n"><v>%s</v></c>', |
224
|
|
|
$cellName, |
225
|
|
|
$cellIndex, |
226
|
|
|
ExcelHelper::convertDateTime($value) |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $cellName |
232
|
|
|
* @param $cellIndex |
233
|
|
|
* @param $value |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
private function getCurrencyCell($cellName, $cellIndex, $value) |
238
|
|
|
{ |
239
|
|
|
return sprintf( |
240
|
|
|
'<c r="%s" s="%s" t="n"><v>%s</v></c>', |
241
|
|
|
$cellName, |
242
|
|
|
$cellIndex, |
243
|
|
|
ExcelHelper::xmlspecialchars($value) |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param $cellName |
249
|
|
|
* @param $cellIndex |
250
|
|
|
* @param $value |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
|
|
private function getIntCell($cellName, $cellIndex, $value) |
255
|
|
|
{ |
256
|
|
|
return '<c r="'.$cellName.'" s="'.$cellIndex.'" t="n"><v>'.($value * 1).'</v></c>'; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param $cellName |
261
|
|
|
* @param $cellIndex |
262
|
|
|
* @param $value |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
private function getFormulaCell($cellName, $cellIndex, $value) |
267
|
|
|
{ |
268
|
|
|
return sprintf( |
269
|
|
|
'<c r="%s" s="%s" t="s"><f>%s</f></c>', |
270
|
|
|
$cellName, |
271
|
|
|
$cellIndex, |
272
|
|
|
ExcelHelper::xmlspecialchars($value) |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|