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