1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Document\Properties; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Settings; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
18
|
|
|
use SimpleXMLElement; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Reader for SpreadsheetML, the XML schema for Microsoft Office Excel 2003. |
22
|
|
|
*/ |
23
|
|
|
class Xml extends BaseReader |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Formats. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $styles = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Character set used in the file. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $charSet = 'UTF-8'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new Excel2003XML Reader instance. |
41
|
|
|
*/ |
42
|
27 |
|
public function __construct() |
43
|
|
|
{ |
44
|
27 |
|
parent::__construct(); |
45
|
27 |
|
$this->securityScanner = XmlScanner::getInstance($this); |
46
|
27 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Can the current IReader read the file? |
50
|
|
|
* |
51
|
|
|
* @param string $pFilename |
52
|
|
|
* |
53
|
|
|
* @throws Exception |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
5 |
|
public function canRead($pFilename) |
58
|
|
|
{ |
59
|
|
|
// Office xmlns:o="urn:schemas-microsoft-com:office:office" |
60
|
|
|
// Excel xmlns:x="urn:schemas-microsoft-com:office:excel" |
61
|
|
|
// XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" |
62
|
|
|
// Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" |
63
|
|
|
// XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" |
64
|
|
|
// XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" |
65
|
|
|
// MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset" |
66
|
|
|
// Rowset xmlns:z="#RowsetSchema" |
67
|
|
|
// |
68
|
|
|
|
69
|
|
|
$signature = [ |
70
|
5 |
|
'<?xml version="1.0"', |
71
|
|
|
'<?mso-application progid="Excel.Sheet"?>', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
// Open file |
75
|
5 |
|
$this->openFile($pFilename); |
76
|
5 |
|
$fileHandle = $this->fileHandle; |
77
|
|
|
|
78
|
|
|
// Read sample data (first 2 KB will do) |
79
|
5 |
|
$data = fread($fileHandle, 2048); |
|
|
|
|
80
|
5 |
|
fclose($fileHandle); |
|
|
|
|
81
|
5 |
|
$data = str_replace("'", '"', $data); // fix headers with single quote |
82
|
|
|
|
83
|
5 |
|
$valid = true; |
84
|
5 |
|
foreach ($signature as $match) { |
85
|
|
|
// every part of the signature must be present |
86
|
5 |
|
if (strpos($data, $match) === false) { |
87
|
|
|
$valid = false; |
88
|
|
|
|
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Retrieve charset encoding |
94
|
5 |
|
if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/um', $data, $matches)) { |
95
|
5 |
|
$this->charSet = strtoupper($matches[1]); |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
return $valid; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check if the file is a valid SimpleXML. |
103
|
|
|
* |
104
|
|
|
* @param string $pFilename |
105
|
|
|
* |
106
|
|
|
* @throws Exception |
107
|
|
|
* |
108
|
|
|
* @return false|\SimpleXMLElement |
109
|
|
|
*/ |
110
|
4 |
|
public function trySimpleXMLLoadString($pFilename) |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
4 |
|
$xml = simplexml_load_string( |
114
|
4 |
|
$this->securityScanner->scan(file_get_contents($pFilename)), |
115
|
4 |
|
'SimpleXMLElement', |
116
|
4 |
|
Settings::getLibXmlLoaderOptions() |
117
|
|
|
); |
118
|
1 |
|
} catch (\Exception $e) { |
119
|
1 |
|
throw new Exception('Cannot load invalid XML file: ' . $pFilename, 0, $e); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
return $xml; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object. |
127
|
|
|
* |
128
|
|
|
* @param string $pFilename |
129
|
|
|
* |
130
|
|
|
* @throws Exception |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function listWorksheetNames($pFilename) |
135
|
|
|
{ |
136
|
|
|
File::assertFile($pFilename); |
137
|
|
|
if (!$this->canRead($pFilename)) { |
138
|
|
|
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$worksheetNames = []; |
142
|
|
|
|
143
|
|
|
$xml = $this->trySimpleXMLLoadString($pFilename); |
144
|
|
|
|
145
|
|
|
$namespaces = $xml->getNamespaces(true); |
146
|
|
|
|
147
|
|
|
$xml_ss = $xml->children($namespaces['ss']); |
148
|
|
|
foreach ($xml_ss->Worksheet as $worksheet) { |
149
|
|
|
$worksheet_ss = $worksheet->attributes($namespaces['ss']); |
150
|
|
|
$worksheetNames[] = self::convertStringEncoding((string) $worksheet_ss['Name'], $this->charSet); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $worksheetNames; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
158
|
|
|
* |
159
|
|
|
* @param string $pFilename |
160
|
|
|
* |
161
|
|
|
* @throws Exception |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function listWorksheetInfo($pFilename) |
166
|
|
|
{ |
167
|
|
|
File::assertFile($pFilename); |
168
|
|
|
|
169
|
|
|
$worksheetInfo = []; |
170
|
|
|
|
171
|
|
|
$xml = $this->trySimpleXMLLoadString($pFilename); |
172
|
|
|
|
173
|
|
|
$namespaces = $xml->getNamespaces(true); |
174
|
|
|
|
175
|
|
|
$worksheetID = 1; |
176
|
|
|
$xml_ss = $xml->children($namespaces['ss']); |
177
|
|
|
foreach ($xml_ss->Worksheet as $worksheet) { |
178
|
|
|
$worksheet_ss = $worksheet->attributes($namespaces['ss']); |
179
|
|
|
|
180
|
|
|
$tmpInfo = []; |
181
|
|
|
$tmpInfo['worksheetName'] = ''; |
182
|
|
|
$tmpInfo['lastColumnLetter'] = 'A'; |
183
|
|
|
$tmpInfo['lastColumnIndex'] = 0; |
184
|
|
|
$tmpInfo['totalRows'] = 0; |
185
|
|
|
$tmpInfo['totalColumns'] = 0; |
186
|
|
|
|
187
|
|
|
if (isset($worksheet_ss['Name'])) { |
188
|
|
|
$tmpInfo['worksheetName'] = (string) $worksheet_ss['Name']; |
189
|
|
|
} else { |
190
|
|
|
$tmpInfo['worksheetName'] = "Worksheet_{$worksheetID}"; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (isset($worksheet->Table->Row)) { |
194
|
|
|
$rowIndex = 0; |
195
|
|
|
|
196
|
|
|
foreach ($worksheet->Table->Row as $rowData) { |
197
|
|
|
$columnIndex = 0; |
198
|
|
|
$rowHasData = false; |
199
|
|
|
|
200
|
|
|
foreach ($rowData->Cell as $cell) { |
201
|
|
|
if (isset($cell->Data)) { |
202
|
|
|
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex); |
203
|
|
|
$rowHasData = true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
++$columnIndex; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
++$rowIndex; |
210
|
|
|
|
211
|
|
|
if ($rowHasData) { |
212
|
|
|
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1); |
218
|
|
|
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1; |
219
|
|
|
|
220
|
|
|
$worksheetInfo[] = $tmpInfo; |
221
|
|
|
++$worksheetID; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $worksheetInfo; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Loads Spreadsheet from file. |
229
|
|
|
* |
230
|
|
|
* @param string $pFilename |
231
|
|
|
* |
232
|
|
|
* @throws Exception |
233
|
|
|
* |
234
|
|
|
* @return Spreadsheet |
235
|
|
|
*/ |
236
|
3 |
|
public function load($pFilename) |
237
|
|
|
{ |
238
|
|
|
// Create new Spreadsheet |
239
|
3 |
|
$spreadsheet = new Spreadsheet(); |
240
|
3 |
|
$spreadsheet->removeSheetByIndex(0); |
241
|
|
|
|
242
|
|
|
// Load into this instance |
243
|
3 |
|
return $this->loadIntoExisting($pFilename, $spreadsheet); |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
private static function identifyFixedStyleValue($styleList, &$styleAttributeValue) |
247
|
|
|
{ |
248
|
2 |
|
$styleAttributeValue = strtolower($styleAttributeValue); |
249
|
2 |
|
foreach ($styleList as $style) { |
250
|
2 |
|
if ($styleAttributeValue == strtolower($style)) { |
251
|
2 |
|
$styleAttributeValue = $style; |
252
|
|
|
|
253
|
2 |
|
return true; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* pixel units to excel width units(units of 1/256th of a character width). |
262
|
|
|
* |
263
|
|
|
* @param float $pxs |
264
|
|
|
* |
265
|
|
|
* @return float |
266
|
|
|
*/ |
267
|
|
|
protected static function pixel2WidthUnits($pxs) |
268
|
|
|
{ |
269
|
|
|
$UNIT_OFFSET_MAP = [0, 36, 73, 109, 146, 182, 219]; |
270
|
|
|
|
271
|
|
|
$widthUnits = 256 * ($pxs / 7); |
272
|
|
|
$widthUnits += $UNIT_OFFSET_MAP[($pxs % 7)]; |
273
|
|
|
|
274
|
|
|
return $widthUnits; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* excel width units(units of 1/256th of a character width) to pixel units. |
279
|
|
|
* |
280
|
|
|
* @param float $widthUnits |
281
|
|
|
* |
282
|
|
|
* @return float |
283
|
|
|
*/ |
284
|
|
|
protected static function widthUnits2Pixel($widthUnits) |
285
|
|
|
{ |
286
|
|
|
$pixels = ($widthUnits / 256) * 7; |
287
|
|
|
$offsetWidthUnits = $widthUnits % 256; |
288
|
|
|
|
289
|
|
|
return $pixels + round($offsetWidthUnits / (256 / 7)); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
protected static function hex2str($hex) |
293
|
|
|
{ |
294
|
|
|
return chr(hexdec($hex[1])); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Loads from file into Spreadsheet instance. |
299
|
|
|
* |
300
|
|
|
* @param string $pFilename |
301
|
|
|
* @param Spreadsheet $spreadsheet |
302
|
|
|
* |
303
|
|
|
* @throws Exception |
304
|
|
|
* |
305
|
|
|
* @return Spreadsheet |
306
|
|
|
*/ |
307
|
3 |
|
public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet) |
308
|
|
|
{ |
309
|
3 |
|
File::assertFile($pFilename); |
310
|
3 |
|
if (!$this->canRead($pFilename)) { |
311
|
|
|
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.'); |
312
|
|
|
} |
313
|
|
|
|
314
|
3 |
|
$xml = $this->trySimpleXMLLoadString($pFilename); |
315
|
|
|
|
316
|
3 |
|
$namespaces = $xml->getNamespaces(true); |
317
|
|
|
|
318
|
3 |
|
$docProps = $spreadsheet->getProperties(); |
319
|
3 |
|
if (isset($xml->DocumentProperties[0])) { |
320
|
|
|
foreach ($xml->DocumentProperties[0] as $propertyName => $propertyValue) { |
321
|
|
|
switch ($propertyName) { |
322
|
|
|
case 'Title': |
323
|
|
|
$docProps->setTitle(self::convertStringEncoding($propertyValue, $this->charSet)); |
324
|
|
|
|
325
|
|
|
break; |
326
|
|
|
case 'Subject': |
327
|
|
|
$docProps->setSubject(self::convertStringEncoding($propertyValue, $this->charSet)); |
328
|
|
|
|
329
|
|
|
break; |
330
|
|
|
case 'Author': |
331
|
|
|
$docProps->setCreator(self::convertStringEncoding($propertyValue, $this->charSet)); |
332
|
|
|
|
333
|
|
|
break; |
334
|
|
|
case 'Created': |
335
|
|
|
$creationDate = strtotime($propertyValue); |
336
|
|
|
$docProps->setCreated($creationDate); |
337
|
|
|
|
338
|
|
|
break; |
339
|
|
|
case 'LastAuthor': |
340
|
|
|
$docProps->setLastModifiedBy(self::convertStringEncoding($propertyValue, $this->charSet)); |
341
|
|
|
|
342
|
|
|
break; |
343
|
|
|
case 'LastSaved': |
344
|
|
|
$lastSaveDate = strtotime($propertyValue); |
345
|
|
|
$docProps->setModified($lastSaveDate); |
346
|
|
|
|
347
|
|
|
break; |
348
|
|
|
case 'Company': |
349
|
|
|
$docProps->setCompany(self::convertStringEncoding($propertyValue, $this->charSet)); |
350
|
|
|
|
351
|
|
|
break; |
352
|
|
|
case 'Category': |
353
|
|
|
$docProps->setCategory(self::convertStringEncoding($propertyValue, $this->charSet)); |
354
|
|
|
|
355
|
|
|
break; |
356
|
|
|
case 'Manager': |
357
|
|
|
$docProps->setManager(self::convertStringEncoding($propertyValue, $this->charSet)); |
358
|
|
|
|
359
|
|
|
break; |
360
|
|
|
case 'Keywords': |
361
|
|
|
$docProps->setKeywords(self::convertStringEncoding($propertyValue, $this->charSet)); |
362
|
|
|
|
363
|
|
|
break; |
364
|
|
|
case 'Description': |
365
|
|
|
$docProps->setDescription(self::convertStringEncoding($propertyValue, $this->charSet)); |
366
|
|
|
|
367
|
|
|
break; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
} |
371
|
3 |
|
if (isset($xml->CustomDocumentProperties)) { |
372
|
|
|
foreach ($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) { |
373
|
|
|
$propertyAttributes = $propertyValue->attributes($namespaces['dt']); |
374
|
|
|
$propertyName = preg_replace_callback('/_x([0-9a-z]{4})_/', ['self', 'hex2str'], $propertyName); |
375
|
|
|
$propertyType = Properties::PROPERTY_TYPE_UNKNOWN; |
376
|
|
|
switch ((string) $propertyAttributes) { |
377
|
|
|
case 'string': |
378
|
|
|
$propertyType = Properties::PROPERTY_TYPE_STRING; |
379
|
|
|
$propertyValue = trim($propertyValue); |
380
|
|
|
|
381
|
|
|
break; |
382
|
|
|
case 'boolean': |
383
|
|
|
$propertyType = Properties::PROPERTY_TYPE_BOOLEAN; |
384
|
|
|
$propertyValue = (bool) $propertyValue; |
385
|
|
|
|
386
|
|
|
break; |
387
|
|
|
case 'integer': |
388
|
|
|
$propertyType = Properties::PROPERTY_TYPE_INTEGER; |
389
|
|
|
$propertyValue = (int) $propertyValue; |
390
|
|
|
|
391
|
|
|
break; |
392
|
|
|
case 'float': |
393
|
|
|
$propertyType = Properties::PROPERTY_TYPE_FLOAT; |
394
|
|
|
$propertyValue = (float) $propertyValue; |
395
|
|
|
|
396
|
|
|
break; |
397
|
|
|
case 'dateTime.tz': |
398
|
|
|
$propertyType = Properties::PROPERTY_TYPE_DATE; |
399
|
|
|
$propertyValue = strtotime(trim($propertyValue)); |
400
|
|
|
|
401
|
|
|
break; |
402
|
|
|
} |
403
|
|
|
$docProps->setCustomProperty($propertyName, $propertyValue, $propertyType); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
3 |
|
$this->parseStyles($xml, $namespaces); |
|
|
|
|
408
|
|
|
|
409
|
3 |
|
$worksheetID = 0; |
410
|
3 |
|
$xml_ss = $xml->children($namespaces['ss']); |
411
|
|
|
|
412
|
3 |
|
foreach ($xml_ss->Worksheet as $worksheet) { |
413
|
3 |
|
$worksheet_ss = $worksheet->attributes($namespaces['ss']); |
414
|
|
|
|
415
|
3 |
|
if ((isset($this->loadSheetsOnly)) && (isset($worksheet_ss['Name'])) && |
416
|
3 |
|
(!in_array($worksheet_ss['Name'], $this->loadSheetsOnly))) { |
417
|
|
|
continue; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
// Create new Worksheet |
421
|
3 |
|
$spreadsheet->createSheet(); |
422
|
3 |
|
$spreadsheet->setActiveSheetIndex($worksheetID); |
423
|
3 |
|
if (isset($worksheet_ss['Name'])) { |
424
|
3 |
|
$worksheetName = self::convertStringEncoding((string) $worksheet_ss['Name'], $this->charSet); |
425
|
|
|
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in |
426
|
|
|
// formula cells... during the load, all formulae should be correct, and we're simply bringing |
427
|
|
|
// the worksheet name in line with the formula, not the reverse |
428
|
3 |
|
$spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false); |
429
|
|
|
} |
430
|
|
|
|
431
|
3 |
|
$columnID = 'A'; |
432
|
3 |
|
if (isset($worksheet->Table->Column)) { |
433
|
3 |
|
foreach ($worksheet->Table->Column as $columnData) { |
434
|
3 |
|
$columnData_ss = $columnData->attributes($namespaces['ss']); |
435
|
3 |
|
if (isset($columnData_ss['Index'])) { |
436
|
3 |
|
$columnID = Coordinate::stringFromColumnIndex((int) $columnData_ss['Index']); |
437
|
|
|
} |
438
|
3 |
|
if (isset($columnData_ss['Width'])) { |
439
|
3 |
|
$columnWidth = $columnData_ss['Width']; |
440
|
3 |
|
$spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4); |
441
|
|
|
} |
442
|
3 |
|
++$columnID; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
446
|
3 |
|
$rowID = 1; |
447
|
3 |
|
if (isset($worksheet->Table->Row)) { |
448
|
3 |
|
$additionalMergedCells = 0; |
449
|
3 |
|
foreach ($worksheet->Table->Row as $rowData) { |
450
|
3 |
|
$rowHasData = false; |
451
|
3 |
|
$row_ss = $rowData->attributes($namespaces['ss']); |
452
|
3 |
|
if (isset($row_ss['Index'])) { |
453
|
2 |
|
$rowID = (int) $row_ss['Index']; |
454
|
|
|
} |
455
|
|
|
|
456
|
3 |
|
$columnID = 'A'; |
457
|
3 |
|
foreach ($rowData->Cell as $cell) { |
458
|
3 |
|
$cell_ss = $cell->attributes($namespaces['ss']); |
459
|
3 |
|
if (isset($cell_ss['Index'])) { |
460
|
2 |
|
$columnID = Coordinate::stringFromColumnIndex((int) $cell_ss['Index']); |
461
|
|
|
} |
462
|
3 |
|
$cellRange = $columnID . $rowID; |
463
|
|
|
|
464
|
3 |
|
if ($this->getReadFilter() !== null) { |
465
|
3 |
|
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) { |
|
|
|
|
466
|
|
|
++$columnID; |
467
|
|
|
|
468
|
|
|
continue; |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
3 |
|
if (isset($cell_ss['HRef'])) { |
473
|
2 |
|
$spreadsheet->getActiveSheet()->getCell($cellRange)->getHyperlink()->setUrl($cell_ss['HRef']); |
474
|
|
|
} |
475
|
|
|
|
476
|
3 |
|
if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown']))) { |
477
|
2 |
|
$columnTo = $columnID; |
478
|
2 |
|
if (isset($cell_ss['MergeAcross'])) { |
479
|
2 |
|
$additionalMergedCells += (int) $cell_ss['MergeAcross']; |
480
|
2 |
|
$columnTo = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($columnID) + $cell_ss['MergeAcross']); |
481
|
|
|
} |
482
|
2 |
|
$rowTo = $rowID; |
483
|
2 |
|
if (isset($cell_ss['MergeDown'])) { |
484
|
2 |
|
$rowTo = $rowTo + $cell_ss['MergeDown']; |
485
|
|
|
} |
486
|
2 |
|
$cellRange .= ':' . $columnTo . $rowTo; |
487
|
2 |
|
$spreadsheet->getActiveSheet()->mergeCells($cellRange); |
488
|
|
|
} |
489
|
|
|
|
490
|
3 |
|
$cellIsSet = $hasCalculatedValue = false; |
491
|
3 |
|
$cellDataFormula = ''; |
492
|
3 |
|
if (isset($cell_ss['Formula'])) { |
493
|
2 |
|
$cellDataFormula = $cell_ss['Formula']; |
494
|
2 |
|
$hasCalculatedValue = true; |
495
|
|
|
} |
496
|
3 |
|
if (isset($cell->Data)) { |
497
|
3 |
|
$cellValue = $cellData = $cell->Data; |
498
|
3 |
|
$type = DataType::TYPE_NULL; |
499
|
3 |
|
$cellData_ss = $cellData->attributes($namespaces['ss']); |
500
|
3 |
|
if (isset($cellData_ss['Type'])) { |
501
|
3 |
|
$cellDataType = $cellData_ss['Type']; |
502
|
3 |
|
switch ($cellDataType) { |
503
|
|
|
/* |
504
|
|
|
const TYPE_STRING = 's'; |
505
|
|
|
const TYPE_FORMULA = 'f'; |
506
|
|
|
const TYPE_NUMERIC = 'n'; |
507
|
|
|
const TYPE_BOOL = 'b'; |
508
|
|
|
const TYPE_NULL = 'null'; |
509
|
|
|
const TYPE_INLINE = 'inlineStr'; |
510
|
|
|
const TYPE_ERROR = 'e'; |
511
|
|
|
*/ |
512
|
3 |
|
case 'String': |
513
|
3 |
|
$cellValue = self::convertStringEncoding($cellValue, $this->charSet); |
514
|
3 |
|
$type = DataType::TYPE_STRING; |
515
|
|
|
|
516
|
3 |
|
break; |
517
|
3 |
|
case 'Number': |
518
|
3 |
|
$type = DataType::TYPE_NUMERIC; |
519
|
3 |
|
$cellValue = (float) $cellValue; |
520
|
3 |
|
if (floor($cellValue) == $cellValue) { |
521
|
3 |
|
$cellValue = (int) $cellValue; |
522
|
|
|
} |
523
|
|
|
|
524
|
3 |
|
break; |
525
|
2 |
|
case 'Boolean': |
526
|
2 |
|
$type = DataType::TYPE_BOOL; |
527
|
2 |
|
$cellValue = ($cellValue != 0); |
528
|
|
|
|
529
|
2 |
|
break; |
530
|
2 |
|
case 'DateTime': |
531
|
2 |
|
$type = DataType::TYPE_NUMERIC; |
532
|
2 |
|
$cellValue = Date::PHPToExcel(strtotime($cellValue)); |
533
|
|
|
|
534
|
2 |
|
break; |
535
|
|
|
case 'Error': |
536
|
|
|
$type = DataType::TYPE_ERROR; |
537
|
|
|
|
538
|
|
|
break; |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
3 |
|
if ($hasCalculatedValue) { |
543
|
2 |
|
$type = DataType::TYPE_FORMULA; |
544
|
2 |
|
$columnNumber = Coordinate::columnIndexFromString($columnID); |
545
|
2 |
|
if (substr($cellDataFormula, 0, 3) == 'of:') { |
546
|
2 |
|
$cellDataFormula = substr($cellDataFormula, 3); |
547
|
2 |
|
$temp = explode('"', $cellDataFormula); |
548
|
2 |
|
$key = false; |
549
|
2 |
|
foreach ($temp as &$value) { |
550
|
|
|
// Only replace in alternate array entries (i.e. non-quoted blocks) |
551
|
2 |
|
if ($key = !$key) { |
|
|
|
|
552
|
2 |
|
$value = str_replace(['[.', '.', ']'], '', $value); |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
} else { |
556
|
|
|
// Convert R1C1 style references to A1 style references (but only when not quoted) |
557
|
|
|
$temp = explode('"', $cellDataFormula); |
558
|
|
|
$key = false; |
559
|
|
|
foreach ($temp as &$value) { |
560
|
|
|
// Only replace in alternate array entries (i.e. non-quoted blocks) |
561
|
|
|
if ($key = !$key) { |
|
|
|
|
562
|
|
|
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); |
563
|
|
|
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way |
564
|
|
|
// through the formula from left to right. Reversing means that we work right to left.through |
565
|
|
|
// the formula |
566
|
|
|
$cellReferences = array_reverse($cellReferences); |
567
|
|
|
// Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, |
568
|
|
|
// then modify the formula to use that new reference |
569
|
|
|
foreach ($cellReferences as $cellReference) { |
570
|
|
|
$rowReference = $cellReference[2][0]; |
571
|
|
|
// Empty R reference is the current row |
572
|
|
|
if ($rowReference == '') { |
573
|
|
|
$rowReference = $rowID; |
574
|
|
|
} |
575
|
|
|
// Bracketed R references are relative to the current row |
576
|
|
|
if ($rowReference[0] == '[') { |
577
|
|
|
$rowReference = $rowID + trim($rowReference, '[]'); |
578
|
|
|
} |
579
|
|
|
$columnReference = $cellReference[4][0]; |
580
|
|
|
// Empty C reference is the current column |
581
|
|
|
if ($columnReference == '') { |
582
|
|
|
$columnReference = $columnNumber; |
583
|
|
|
} |
584
|
|
|
// Bracketed C references are relative to the current column |
585
|
|
|
if ($columnReference[0] == '[') { |
586
|
|
|
$columnReference = $columnNumber + trim($columnReference, '[]'); |
587
|
|
|
} |
588
|
|
|
$A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference; |
589
|
|
|
$value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
} |
594
|
2 |
|
unset($value); |
595
|
|
|
// Then rebuild the formula string |
596
|
2 |
|
$cellDataFormula = implode('"', $temp); |
597
|
|
|
} |
598
|
|
|
|
599
|
3 |
|
$spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue), $type); |
600
|
3 |
|
if ($hasCalculatedValue) { |
601
|
2 |
|
$spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setCalculatedValue($cellValue); |
602
|
|
|
} |
603
|
3 |
|
$cellIsSet = $rowHasData = true; |
604
|
|
|
} |
605
|
|
|
|
606
|
3 |
|
if (isset($cell->Comment)) { |
607
|
3 |
|
$commentAttributes = $cell->Comment->attributes($namespaces['ss']); |
608
|
3 |
|
$author = 'unknown'; |
609
|
3 |
|
if (isset($commentAttributes->Author)) { |
610
|
|
|
$author = (string) $commentAttributes->Author; |
611
|
|
|
} |
612
|
3 |
|
$node = $cell->Comment->Data->asXML(); |
613
|
3 |
|
$annotation = strip_tags($node); |
614
|
3 |
|
$spreadsheet->getActiveSheet()->getComment($columnID . $rowID)->setAuthor(self::convertStringEncoding($author, $this->charSet))->setText($this->parseRichText($annotation)); |
615
|
|
|
} |
616
|
|
|
|
617
|
3 |
|
if (($cellIsSet) && (isset($cell_ss['StyleID']))) { |
618
|
2 |
|
$style = (string) $cell_ss['StyleID']; |
619
|
2 |
|
if ((isset($this->styles[$style])) && (!empty($this->styles[$style]))) { |
620
|
2 |
|
if (!$spreadsheet->getActiveSheet()->cellExists($columnID . $rowID)) { |
621
|
|
|
$spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setValue(null); |
622
|
|
|
} |
623
|
2 |
|
$spreadsheet->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styles[$style]); |
624
|
|
|
} |
625
|
|
|
} |
626
|
3 |
|
++$columnID; |
627
|
3 |
|
while ($additionalMergedCells > 0) { |
628
|
2 |
|
++$columnID; |
629
|
2 |
|
--$additionalMergedCells; |
630
|
|
|
} |
631
|
|
|
} |
632
|
|
|
|
633
|
3 |
|
if ($rowHasData) { |
634
|
3 |
|
if (isset($row_ss['Height'])) { |
635
|
3 |
|
$rowHeight = $row_ss['Height']; |
636
|
3 |
|
$spreadsheet->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight); |
637
|
|
|
} |
638
|
|
|
} |
639
|
|
|
|
640
|
3 |
|
++$rowID; |
641
|
|
|
} |
642
|
|
|
} |
643
|
3 |
|
++$worksheetID; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
// Return |
647
|
3 |
|
return $spreadsheet; |
648
|
|
|
} |
649
|
|
|
|
650
|
3 |
|
protected static function convertStringEncoding($string, $charset) |
651
|
|
|
{ |
652
|
3 |
|
if ($charset != 'UTF-8') { |
653
|
|
|
return StringHelper::convertEncoding($string, 'UTF-8', $charset); |
654
|
|
|
} |
655
|
|
|
|
656
|
3 |
|
return $string; |
657
|
|
|
} |
658
|
|
|
|
659
|
3 |
|
protected function parseRichText($is) |
660
|
|
|
{ |
661
|
3 |
|
$value = new RichText(); |
662
|
|
|
|
663
|
3 |
|
$value->createText(self::convertStringEncoding($is, $this->charSet)); |
664
|
|
|
|
665
|
3 |
|
return $value; |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* @param SimpleXMLElement $xml |
670
|
|
|
* @param array $namespaces |
671
|
|
|
*/ |
672
|
3 |
|
private function parseStyles(SimpleXMLElement $xml, array $namespaces) |
673
|
|
|
{ |
674
|
3 |
|
if (!isset($xml->Styles)) { |
675
|
1 |
|
return; |
676
|
|
|
} |
677
|
|
|
|
678
|
2 |
|
foreach ($xml->Styles[0] as $style) { |
679
|
2 |
|
$style_ss = $style->attributes($namespaces['ss']); |
680
|
2 |
|
$styleID = (string) $style_ss['ID']; |
681
|
2 |
|
$this->styles[$styleID] = (isset($this->styles['Default'])) ? $this->styles['Default'] : []; |
682
|
2 |
|
foreach ($style as $styleType => $styleData) { |
683
|
2 |
|
$styleAttributes = $styleData->attributes($namespaces['ss']); |
684
|
2 |
|
switch ($styleType) { |
685
|
2 |
|
case 'Alignment': |
686
|
2 |
|
$this->parseStyleAlignment($styleID, $styleAttributes); |
687
|
|
|
|
688
|
2 |
|
break; |
689
|
2 |
|
case 'Borders': |
690
|
2 |
|
$this->parseStyleBorders($styleID, $styleData, $namespaces); |
691
|
|
|
|
692
|
2 |
|
break; |
693
|
2 |
|
case 'Font': |
694
|
2 |
|
$this->parseStyleFont($styleID, $styleAttributes); |
695
|
|
|
|
696
|
2 |
|
break; |
697
|
2 |
|
case 'Interior': |
698
|
2 |
|
$this->parseStyleInterior($styleID, $styleAttributes); |
699
|
|
|
|
700
|
2 |
|
break; |
701
|
2 |
|
case 'NumberFormat': |
702
|
2 |
|
$this->parseStyleNumberFormat($styleID, $styleAttributes); |
703
|
|
|
|
704
|
2 |
|
break; |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
} |
708
|
2 |
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* @param string $styleID |
712
|
|
|
* @param SimpleXMLElement $styleAttributes |
713
|
|
|
*/ |
714
|
2 |
|
private function parseStyleAlignment($styleID, SimpleXMLElement $styleAttributes) |
715
|
|
|
{ |
716
|
|
|
$verticalAlignmentStyles = [ |
717
|
2 |
|
Alignment::VERTICAL_BOTTOM, |
718
|
1 |
|
Alignment::VERTICAL_TOP, |
719
|
1 |
|
Alignment::VERTICAL_CENTER, |
720
|
1 |
|
Alignment::VERTICAL_JUSTIFY, |
721
|
|
|
]; |
722
|
|
|
$horizontalAlignmentStyles = [ |
723
|
2 |
|
Alignment::HORIZONTAL_GENERAL, |
724
|
1 |
|
Alignment::HORIZONTAL_LEFT, |
725
|
1 |
|
Alignment::HORIZONTAL_RIGHT, |
726
|
1 |
|
Alignment::HORIZONTAL_CENTER, |
727
|
1 |
|
Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
728
|
1 |
|
Alignment::HORIZONTAL_JUSTIFY, |
729
|
|
|
]; |
730
|
|
|
|
731
|
2 |
|
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) { |
732
|
2 |
|
$styleAttributeValue = (string) $styleAttributeValue; |
733
|
2 |
|
switch ($styleAttributeKey) { |
734
|
2 |
|
case 'Vertical': |
735
|
2 |
|
if (self::identifyFixedStyleValue($verticalAlignmentStyles, $styleAttributeValue)) { |
736
|
2 |
|
$this->styles[$styleID]['alignment']['vertical'] = $styleAttributeValue; |
737
|
|
|
} |
738
|
|
|
|
739
|
2 |
|
break; |
740
|
2 |
|
case 'Horizontal': |
741
|
2 |
|
if (self::identifyFixedStyleValue($horizontalAlignmentStyles, $styleAttributeValue)) { |
742
|
2 |
|
$this->styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue; |
743
|
|
|
} |
744
|
|
|
|
745
|
2 |
|
break; |
746
|
2 |
|
case 'WrapText': |
747
|
2 |
|
$this->styles[$styleID]['alignment']['wrapText'] = true; |
748
|
|
|
|
749
|
2 |
|
break; |
750
|
|
|
} |
751
|
|
|
} |
752
|
2 |
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* @param $styleID |
756
|
|
|
* @param SimpleXMLElement $styleData |
757
|
|
|
* @param array $namespaces |
758
|
|
|
*/ |
759
|
2 |
|
private function parseStyleBorders($styleID, SimpleXMLElement $styleData, array $namespaces) |
760
|
|
|
{ |
761
|
2 |
|
foreach ($styleData->Border as $borderStyle) { |
762
|
2 |
|
$borderAttributes = $borderStyle->attributes($namespaces['ss']); |
763
|
2 |
|
$thisBorder = []; |
764
|
2 |
|
foreach ($borderAttributes as $borderStyleKey => $borderStyleValue) { |
765
|
2 |
|
switch ($borderStyleKey) { |
766
|
2 |
|
case 'LineStyle': |
767
|
2 |
|
$thisBorder['borderStyle'] = Border::BORDER_MEDIUM; |
768
|
|
|
|
769
|
2 |
|
break; |
770
|
2 |
|
case 'Weight': |
771
|
2 |
|
break; |
772
|
2 |
|
case 'Position': |
773
|
2 |
|
$borderPosition = strtolower($borderStyleValue); |
774
|
|
|
|
775
|
2 |
|
break; |
776
|
2 |
|
case 'Color': |
777
|
2 |
|
$borderColour = substr($borderStyleValue, 1); |
778
|
2 |
|
$thisBorder['color']['rgb'] = $borderColour; |
779
|
|
|
|
780
|
2 |
|
break; |
781
|
|
|
} |
782
|
|
|
} |
783
|
2 |
|
if (!empty($thisBorder)) { |
784
|
2 |
|
if (($borderPosition == 'left') || ($borderPosition == 'right') || ($borderPosition == 'top') || ($borderPosition == 'bottom')) { |
|
|
|
|
785
|
2 |
|
$this->styles[$styleID]['borders'][$borderPosition] = $thisBorder; |
786
|
|
|
} |
787
|
|
|
} |
788
|
|
|
} |
789
|
2 |
|
} |
790
|
|
|
|
791
|
|
|
/** |
792
|
|
|
* @param $styleID |
793
|
|
|
* @param SimpleXMLElement $styleAttributes |
794
|
|
|
*/ |
795
|
2 |
|
private function parseStyleFont($styleID, SimpleXMLElement $styleAttributes) |
796
|
|
|
{ |
797
|
|
|
$underlineStyles = [ |
798
|
2 |
|
Font::UNDERLINE_NONE, |
799
|
1 |
|
Font::UNDERLINE_DOUBLE, |
800
|
1 |
|
Font::UNDERLINE_DOUBLEACCOUNTING, |
801
|
1 |
|
Font::UNDERLINE_SINGLE, |
802
|
1 |
|
Font::UNDERLINE_SINGLEACCOUNTING, |
803
|
|
|
]; |
804
|
|
|
|
805
|
2 |
|
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) { |
806
|
2 |
|
$styleAttributeValue = (string) $styleAttributeValue; |
807
|
2 |
|
switch ($styleAttributeKey) { |
808
|
2 |
|
case 'FontName': |
809
|
2 |
|
$this->styles[$styleID]['font']['name'] = $styleAttributeValue; |
810
|
|
|
|
811
|
2 |
|
break; |
812
|
2 |
|
case 'Size': |
813
|
2 |
|
$this->styles[$styleID]['font']['size'] = $styleAttributeValue; |
814
|
|
|
|
815
|
2 |
|
break; |
816
|
2 |
|
case 'Color': |
817
|
2 |
|
$this->styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue, 1); |
818
|
|
|
|
819
|
2 |
|
break; |
820
|
2 |
|
case 'Bold': |
821
|
2 |
|
$this->styles[$styleID]['font']['bold'] = true; |
822
|
|
|
|
823
|
2 |
|
break; |
824
|
2 |
|
case 'Italic': |
825
|
2 |
|
$this->styles[$styleID]['font']['italic'] = true; |
826
|
|
|
|
827
|
2 |
|
break; |
828
|
2 |
|
case 'Underline': |
829
|
2 |
|
if (self::identifyFixedStyleValue($underlineStyles, $styleAttributeValue)) { |
830
|
2 |
|
$this->styles[$styleID]['font']['underline'] = $styleAttributeValue; |
831
|
|
|
} |
832
|
|
|
|
833
|
2 |
|
break; |
834
|
|
|
} |
835
|
|
|
} |
836
|
2 |
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* @param $styleID |
840
|
|
|
* @param SimpleXMLElement $styleAttributes |
841
|
|
|
*/ |
842
|
2 |
|
private function parseStyleInterior($styleID, SimpleXMLElement $styleAttributes) |
843
|
|
|
{ |
844
|
2 |
|
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) { |
845
|
2 |
|
switch ($styleAttributeKey) { |
846
|
2 |
|
case 'Color': |
847
|
2 |
|
$this->styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue, 1); |
848
|
|
|
|
849
|
2 |
|
break; |
850
|
2 |
|
case 'Pattern': |
851
|
2 |
|
$this->styles[$styleID]['fill']['fillType'] = strtolower($styleAttributeValue); |
852
|
|
|
|
853
|
2 |
|
break; |
854
|
|
|
} |
855
|
|
|
} |
856
|
2 |
|
} |
857
|
|
|
|
858
|
|
|
/** |
859
|
|
|
* @param $styleID |
860
|
|
|
* @param SimpleXMLElement $styleAttributes |
861
|
|
|
*/ |
862
|
2 |
|
private function parseStyleNumberFormat($styleID, SimpleXMLElement $styleAttributes) |
863
|
|
|
{ |
864
|
2 |
|
$fromFormats = ['\-', '\ ']; |
865
|
2 |
|
$toFormats = ['-', ' ']; |
866
|
|
|
|
867
|
2 |
|
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) { |
868
|
2 |
|
$styleAttributeValue = str_replace($fromFormats, $toFormats, $styleAttributeValue); |
869
|
|
|
switch ($styleAttributeValue) { |
870
|
2 |
|
case 'Short Date': |
871
|
2 |
|
$styleAttributeValue = 'dd/mm/yyyy'; |
872
|
|
|
|
873
|
2 |
|
break; |
874
|
|
|
} |
875
|
|
|
|
876
|
2 |
|
if ($styleAttributeValue > '') { |
877
|
2 |
|
$this->styles[$styleID]['numberFormat']['formatCode'] = $styleAttributeValue; |
878
|
|
|
} |
879
|
|
|
} |
880
|
2 |
|
} |
881
|
|
|
} |
882
|
|
|
|