|
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 (!is_scalar($value) || $value === '') { |
|
166
|
|
|
return '<c r="'.$cellName.'" s="'.$cellIndex.'"/>'; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if (is_string($value) && $value{0} == '=') { |
|
170
|
|
|
return $this->getFormulaCell($cellName, $cellIndex, $value); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if ($cellType == 'date' || $cellType == 'datetime') { |
|
174
|
|
|
return $this->getDateCell($cellName, $cellIndex, $value); |
|
175
|
|
|
} elseif ($cellType == 'currency' || $cellType == 'percent' || $cellType == 'numeric') { |
|
176
|
|
|
return $this->getCurrencyCell($cellName, $cellIndex, $value); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (!is_string($value)) { |
|
180
|
|
|
return $this->getIntCell($cellName, $cellIndex, $value); |
|
181
|
|
|
} else { |
|
182
|
|
|
if ($value{0} != '0' && |
|
183
|
|
|
$value{0} != '+' && |
|
184
|
|
|
filter_var( |
|
185
|
|
|
$value, |
|
186
|
|
|
FILTER_VALIDATE_INT, |
|
187
|
|
|
['options' => ['max_range' => ExcelHelper::EXCEL_MAX_RANGE]] |
|
188
|
|
|
) |
|
189
|
|
|
) { |
|
190
|
|
|
return $this->getIntCell($cellName, $cellIndex, $value); |
|
191
|
|
|
} else { |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param $cellName |
|
199
|
|
|
* @param $cellIndex |
|
200
|
|
|
* @param $value |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
private function getDateCell($cellName, $cellIndex, $value) |
|
205
|
|
|
{ |
|
206
|
|
|
return sprintf( |
|
207
|
|
|
'<c r="%s" s="%s" t="n"><v>%s</v></c>', |
|
208
|
|
|
$cellName, |
|
209
|
|
|
$cellIndex, |
|
210
|
|
|
ExcelHelper::convertDateTime($value) |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param $cellName |
|
216
|
|
|
* @param $cellIndex |
|
217
|
|
|
* @param $value |
|
218
|
|
|
* |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
private function getCurrencyCell($cellName, $cellIndex, $value) |
|
222
|
|
|
{ |
|
223
|
|
|
return sprintf( |
|
224
|
|
|
'<c r="%s" s="%s" t="n"><v>%s</v></c>', |
|
225
|
|
|
$cellName, |
|
226
|
|
|
$cellIndex, |
|
227
|
|
|
ExcelHelper::xmlspecialchars($value) |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param $cellName |
|
233
|
|
|
* @param $cellIndex |
|
234
|
|
|
* @param $value |
|
235
|
|
|
* |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
private function getIntCell($cellName, $cellIndex, $value) |
|
239
|
|
|
{ |
|
240
|
|
|
return '<c r="'.$cellName.'" s="'.$cellIndex.'" t="n"><v>'.($value * 1).'</v></c>'; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param $cellName |
|
245
|
|
|
* @param $cellIndex |
|
246
|
|
|
* @param $value |
|
247
|
|
|
* |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
private function getFormulaCell($cellName, $cellIndex, $value) |
|
251
|
|
|
{ |
|
252
|
|
|
return sprintf( |
|
253
|
|
|
'<c r="%s" s="%s" t="s"><f>%s</f></c>', |
|
254
|
|
|
$cellName, |
|
255
|
|
|
$cellIndex, |
|
256
|
|
|
ExcelHelper::xmlspecialchars($value) |
|
257
|
|
|
); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|