-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Xls.php
7645 lines (6329 loc) · 282 KB
/
Xls.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace PhpOffice\PhpSpreadsheet\Reader;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Reader\Xls\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Style\CellFont;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Style\FillPattern;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Shared\CodePage;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Escher;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer\SpContainer;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Shared\OLE;
use PhpOffice\PhpSpreadsheet\Shared\OLERead;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Shared\Xls as SharedXls;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Worksheet\SheetView;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
// Original file header of ParseXL (used as the base for this class):
// --------------------------------------------------------------------------------
// Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
// trex005, and mmp11 (SourceForge.net)
// https://sourceforge.net/projects/phpexcelreader/
// Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
// Modelled moreso after Perl Excel Parse/Write modules
// Added Parse_Excel_Spreadsheet object
// Reads a whole worksheet or tab as row,column array or as
// associated hash of indexed rows and named column fields
// Added variables for worksheet (tab) indexes and names
// Added an object call for loading individual woorksheets
// Changed default indexing defaults to 0 based arrays
// Fixed date/time and percent formats
// Includes patches found at SourceForge...
// unicode patch by nobody
// unpack("d") machine depedency patch by matchy
// boundsheet utf16 patch by bjaenichen
// Renamed functions for shorter names
// General code cleanup and rigor, including <80 column width
// Included a testcase Excel file and PHP example calls
// Code works for PHP 5.x
// Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
// http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
// Decoding of formula conditions, results, and tokens.
// Support for user-defined named cells added as an array "namedcells"
// Patch code for user-defined named cells supports single cells only.
// NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
// external sheet reference structure
class Xls extends BaseReader
{
private const HIGH_ORDER_BIT = 0x80 << 24;
private const FC000000 = 0xFC << 24;
private const FE000000 = 0xFE << 24;
// ParseXL definitions
const XLS_BIFF8 = 0x0600;
const XLS_BIFF7 = 0x0500;
const XLS_WORKBOOKGLOBALS = 0x0005;
const XLS_WORKSHEET = 0x0010;
// record identifiers
const XLS_TYPE_FORMULA = 0x0006;
const XLS_TYPE_EOF = 0x000A;
const XLS_TYPE_PROTECT = 0x0012;
const XLS_TYPE_OBJECTPROTECT = 0x0063;
const XLS_TYPE_SCENPROTECT = 0x00DD;
const XLS_TYPE_PASSWORD = 0x0013;
const XLS_TYPE_HEADER = 0x0014;
const XLS_TYPE_FOOTER = 0x0015;
const XLS_TYPE_EXTERNSHEET = 0x0017;
const XLS_TYPE_DEFINEDNAME = 0x0018;
const XLS_TYPE_VERTICALPAGEBREAKS = 0x001A;
const XLS_TYPE_HORIZONTALPAGEBREAKS = 0x001B;
const XLS_TYPE_NOTE = 0x001C;
const XLS_TYPE_SELECTION = 0x001D;
const XLS_TYPE_DATEMODE = 0x0022;
const XLS_TYPE_EXTERNNAME = 0x0023;
const XLS_TYPE_LEFTMARGIN = 0x0026;
const XLS_TYPE_RIGHTMARGIN = 0x0027;
const XLS_TYPE_TOPMARGIN = 0x0028;
const XLS_TYPE_BOTTOMMARGIN = 0x0029;
const XLS_TYPE_PRINTGRIDLINES = 0x002B;
const XLS_TYPE_FILEPASS = 0x002F;
const XLS_TYPE_FONT = 0x0031;
const XLS_TYPE_CONTINUE = 0x003C;
const XLS_TYPE_PANE = 0x0041;
const XLS_TYPE_CODEPAGE = 0x0042;
const XLS_TYPE_DEFCOLWIDTH = 0x0055;
const XLS_TYPE_OBJ = 0x005D;
const XLS_TYPE_COLINFO = 0x007D;
const XLS_TYPE_IMDATA = 0x007F;
const XLS_TYPE_SHEETPR = 0x0081;
const XLS_TYPE_HCENTER = 0x0083;
const XLS_TYPE_VCENTER = 0x0084;
const XLS_TYPE_SHEET = 0x0085;
const XLS_TYPE_PALETTE = 0x0092;
const XLS_TYPE_SCL = 0x00A0;
const XLS_TYPE_PAGESETUP = 0x00A1;
const XLS_TYPE_MULRK = 0x00BD;
const XLS_TYPE_MULBLANK = 0x00BE;
const XLS_TYPE_DBCELL = 0x00D7;
const XLS_TYPE_XF = 0x00E0;
const XLS_TYPE_MERGEDCELLS = 0x00E5;
const XLS_TYPE_MSODRAWINGGROUP = 0x00EB;
const XLS_TYPE_MSODRAWING = 0x00EC;
const XLS_TYPE_SST = 0x00FC;
const XLS_TYPE_LABELSST = 0x00FD;
const XLS_TYPE_EXTSST = 0x00FF;
const XLS_TYPE_EXTERNALBOOK = 0x01AE;
const XLS_TYPE_DATAVALIDATIONS = 0x01B2;
const XLS_TYPE_TXO = 0x01B6;
const XLS_TYPE_HYPERLINK = 0x01B8;
const XLS_TYPE_DATAVALIDATION = 0x01BE;
const XLS_TYPE_DIMENSION = 0x0200;
const XLS_TYPE_BLANK = 0x0201;
const XLS_TYPE_NUMBER = 0x0203;
const XLS_TYPE_LABEL = 0x0204;
const XLS_TYPE_BOOLERR = 0x0205;
const XLS_TYPE_STRING = 0x0207;
const XLS_TYPE_ROW = 0x0208;
const XLS_TYPE_INDEX = 0x020B;
const XLS_TYPE_ARRAY = 0x0221;
const XLS_TYPE_DEFAULTROWHEIGHT = 0x0225;
const XLS_TYPE_WINDOW2 = 0x023E;
const XLS_TYPE_RK = 0x027E;
const XLS_TYPE_STYLE = 0x0293;
const XLS_TYPE_FORMAT = 0x041E;
const XLS_TYPE_SHAREDFMLA = 0x04BC;
const XLS_TYPE_BOF = 0x0809;
const XLS_TYPE_SHEETPROTECTION = 0x0867;
const XLS_TYPE_RANGEPROTECTION = 0x0868;
const XLS_TYPE_SHEETLAYOUT = 0x0862;
const XLS_TYPE_XFEXT = 0x087D;
const XLS_TYPE_PAGELAYOUTVIEW = 0x088B;
const XLS_TYPE_CFHEADER = 0x01B0;
const XLS_TYPE_CFRULE = 0x01B1;
const XLS_TYPE_UNKNOWN = 0xFFFF;
// Encryption type
const MS_BIFF_CRYPTO_NONE = 0;
const MS_BIFF_CRYPTO_XOR = 1;
const MS_BIFF_CRYPTO_RC4 = 2;
// Size of stream blocks when using RC4 encryption
const REKEY_BLOCK = 0x400;
// should be consistent with Writer\Xls\Style\CellBorder
const BORDER_STYLE_MAP = [
Border::BORDER_NONE, // => 0x00,
Border::BORDER_THIN, // => 0x01,
Border::BORDER_MEDIUM, // => 0x02,
Border::BORDER_DASHED, // => 0x03,
Border::BORDER_DOTTED, // => 0x04,
Border::BORDER_THICK, // => 0x05,
Border::BORDER_DOUBLE, // => 0x06,
Border::BORDER_HAIR, // => 0x07,
Border::BORDER_MEDIUMDASHED, // => 0x08,
Border::BORDER_DASHDOT, // => 0x09,
Border::BORDER_MEDIUMDASHDOT, // => 0x0A,
Border::BORDER_DASHDOTDOT, // => 0x0B,
Border::BORDER_MEDIUMDASHDOTDOT, // => 0x0C,
Border::BORDER_SLANTDASHDOT, // => 0x0D,
Border::BORDER_OMIT, // => 0x0E,
Border::BORDER_OMIT, // => 0x0F,
];
/**
* Summary Information stream data.
*/
private ?string $summaryInformation = null;
/**
* Extended Summary Information stream data.
*/
private ?string $documentSummaryInformation = null;
/**
* Workbook stream data. (Includes workbook globals substream as well as sheet substreams).
*/
private string $data;
/**
* Size in bytes of $this->data.
*/
private int $dataSize;
/**
* Current position in stream.
*/
private int $pos;
/**
* Workbook to be returned by the reader.
*/
private Spreadsheet $spreadsheet;
/**
* Worksheet that is currently being built by the reader.
*/
private Worksheet $phpSheet;
/**
* BIFF version.
*/
private int $version = 0;
/**
* Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
* For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'.
*/
private string $codepage = '';
/**
* Shared formats.
*/
private array $formats;
/**
* Shared fonts.
*
* @var Font[]
*/
private array $objFonts;
/**
* Color palette.
*/
private array $palette;
/**
* Worksheets.
*/
private array $sheets;
/**
* External books.
*/
private array $externalBooks;
/**
* REF structures. Only applies to BIFF8.
*/
private array $ref;
/**
* External names.
*/
private array $externalNames;
/**
* Defined names.
*/
private array $definedname;
/**
* Shared strings. Only applies to BIFF8.
*/
private array $sst;
/**
* Panes are frozen? (in sheet currently being read). See WINDOW2 record.
*/
private bool $frozen;
/**
* Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
*/
private bool $isFitToPages;
/**
* Objects. One OBJ record contributes with one entry.
*/
private array $objs;
/**
* Text Objects. One TXO record corresponds with one entry.
*/
private array $textObjects;
/**
* Cell Annotations (BIFF8).
*/
private array $cellNotes;
/**
* The combined MSODRAWINGGROUP data.
*/
private string $drawingGroupData;
/**
* The combined MSODRAWING data (per sheet).
*/
private string $drawingData;
/**
* Keep track of XF index.
*/
private int $xfIndex;
/**
* Mapping of XF index (that is a cell XF) to final index in cellXf collection.
*/
private array $mapCellXfIndex;
/**
* Mapping of XF index (that is a style XF) to final index in cellStyleXf collection.
*/
private array $mapCellStyleXfIndex;
/**
* The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
*/
private array $sharedFormulas;
/**
* The shared formula parts in a sheet. One FORMULA record contributes with one value if it
* refers to a shared formula.
*/
private array $sharedFormulaParts;
/**
* The type of encryption in use.
*/
private int $encryption = 0;
/**
* The position in the stream after which contents are encrypted.
*/
private int $encryptionStartPos = 0;
/**
* The current RC4 decryption object.
*/
private ?Xls\RC4 $rc4Key = null;
/**
* The position in the stream that the RC4 decryption object was left at.
*/
private int $rc4Pos = 0;
/**
* The current MD5 context state.
* It is never set in the program, so code which uses it is suspect.
*/
private string $md5Ctxt; // @phpstan-ignore-line
private int $textObjRef;
private string $baseCell;
private bool $activeSheetSet = false;
/**
* Create a new Xls Reader instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Can the current IReader read the file?
*/
public function canRead(string $filename): bool
{
if (File::testFileNoThrow($filename) === false) {
return false;
}
try {
// Use ParseXL for the hard work.
$ole = new OLERead();
// get excel data
$ole->read($filename);
if ($ole->wrkbook === null) {
throw new Exception('The filename ' . $filename . ' is not recognised as a Spreadsheet file');
}
return true;
} catch (PhpSpreadsheetException) {
return false;
}
}
public function setCodepage(string $codepage): void
{
if (CodePage::validate($codepage) === false) {
throw new PhpSpreadsheetException('Unknown codepage: ' . $codepage);
}
$this->codepage = $codepage;
}
public function getCodepage(): string
{
return $this->codepage;
}
/**
* Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
*/
public function listWorksheetNames(string $filename): array
{
File::assertFile($filename);
$worksheetNames = [];
// Read the OLE file
$this->loadOLE($filename);
// total byte size of Excel data (workbook global substream + sheet substreams)
$this->dataSize = strlen($this->data);
$this->pos = 0;
$this->sheets = [];
// Parse Workbook Global Substream
while ($this->pos < $this->dataSize) {
$code = self::getUInt2d($this->data, $this->pos);
match ($code) {
self::XLS_TYPE_BOF => $this->readBof(),
self::XLS_TYPE_SHEET => $this->readSheet(),
self::XLS_TYPE_EOF => $this->readDefault(),
self::XLS_TYPE_CODEPAGE => $this->readCodepage(),
default => $this->readDefault(),
};
if ($code === self::XLS_TYPE_EOF) {
break;
}
}
foreach ($this->sheets as $sheet) {
if ($sheet['sheetType'] != 0x00) {
// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
continue;
}
$worksheetNames[] = $sheet['name'];
}
return $worksheetNames;
}
/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
*/
public function listWorksheetInfo(string $filename): array
{
File::assertFile($filename);
$worksheetInfo = [];
// Read the OLE file
$this->loadOLE($filename);
// total byte size of Excel data (workbook global substream + sheet substreams)
$this->dataSize = strlen($this->data);
// initialize
$this->pos = 0;
$this->sheets = [];
// Parse Workbook Global Substream
while ($this->pos < $this->dataSize) {
$code = self::getUInt2d($this->data, $this->pos);
match ($code) {
self::XLS_TYPE_BOF => $this->readBof(),
self::XLS_TYPE_SHEET => $this->readSheet(),
self::XLS_TYPE_EOF => $this->readDefault(),
self::XLS_TYPE_CODEPAGE => $this->readCodepage(),
default => $this->readDefault(),
};
if ($code === self::XLS_TYPE_EOF) {
break;
}
}
// Parse the individual sheets
foreach ($this->sheets as $sheet) {
if ($sheet['sheetType'] != 0x00) {
// 0x00: Worksheet
// 0x02: Chart
// 0x06: Visual Basic module
continue;
}
$tmpInfo = [];
$tmpInfo['worksheetName'] = $sheet['name'];
$tmpInfo['lastColumnLetter'] = 'A';
$tmpInfo['lastColumnIndex'] = 0;
$tmpInfo['totalRows'] = 0;
$tmpInfo['totalColumns'] = 0;
$this->pos = $sheet['offset'];
while ($this->pos <= $this->dataSize - 4) {
$code = self::getUInt2d($this->data, $this->pos);
switch ($code) {
case self::XLS_TYPE_RK:
case self::XLS_TYPE_LABELSST:
case self::XLS_TYPE_NUMBER:
case self::XLS_TYPE_FORMULA:
case self::XLS_TYPE_BOOLERR:
case self::XLS_TYPE_LABEL:
$length = self::getUInt2d($this->data, $this->pos + 2);
$recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
// move stream pointer to next record
$this->pos += 4 + $length;
$rowIndex = self::getUInt2d($recordData, 0) + 1;
$columnIndex = self::getUInt2d($recordData, 2);
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
break;
case self::XLS_TYPE_BOF:
$this->readBof();
break;
case self::XLS_TYPE_EOF:
$this->readDefault();
break 2;
default:
$this->readDefault();
break;
}
}
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
$worksheetInfo[] = $tmpInfo;
}
return $worksheetInfo;
}
/**
* Loads PhpSpreadsheet from file.
*/
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Read the OLE file
$this->loadOLE($filename);
// Initialisations
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->removeSheetByIndex(0); // remove 1st sheet
if (!$this->readDataOnly) {
$this->spreadsheet->removeCellStyleXfByIndex(0); // remove the default style
$this->spreadsheet->removeCellXfByIndex(0); // remove the default style
}
// Read the summary information stream (containing meta data)
$this->readSummaryInformation();
// Read the Additional document summary information stream (containing application-specific meta data)
$this->readDocumentSummaryInformation();
// total byte size of Excel data (workbook global substream + sheet substreams)
$this->dataSize = strlen($this->data);
// initialize
$this->pos = 0;
$this->codepage = $this->codepage ?: CodePage::DEFAULT_CODE_PAGE;
$this->formats = [];
$this->objFonts = [];
$this->palette = [];
$this->sheets = [];
$this->externalBooks = [];
$this->ref = [];
$this->definedname = [];
$this->sst = [];
$this->drawingGroupData = '';
$this->xfIndex = 0;
$this->mapCellXfIndex = [];
$this->mapCellStyleXfIndex = [];
// Parse Workbook Global Substream
while ($this->pos < $this->dataSize) {
$code = self::getUInt2d($this->data, $this->pos);
match ($code) {
self::XLS_TYPE_BOF => $this->readBof(),
self::XLS_TYPE_FILEPASS => $this->readFilepass(),
self::XLS_TYPE_CODEPAGE => $this->readCodepage(),
self::XLS_TYPE_DATEMODE => $this->readDateMode(),
self::XLS_TYPE_FONT => $this->readFont(),
self::XLS_TYPE_FORMAT => $this->readFormat(),
self::XLS_TYPE_XF => $this->readXf(),
self::XLS_TYPE_XFEXT => $this->readXfExt(),
self::XLS_TYPE_STYLE => $this->readStyle(),
self::XLS_TYPE_PALETTE => $this->readPalette(),
self::XLS_TYPE_SHEET => $this->readSheet(),
self::XLS_TYPE_EXTERNALBOOK => $this->readExternalBook(),
self::XLS_TYPE_EXTERNNAME => $this->readExternName(),
self::XLS_TYPE_EXTERNSHEET => $this->readExternSheet(),
self::XLS_TYPE_DEFINEDNAME => $this->readDefinedName(),
self::XLS_TYPE_MSODRAWINGGROUP => $this->readMsoDrawingGroup(),
self::XLS_TYPE_SST => $this->readSst(),
self::XLS_TYPE_EOF => $this->readDefault(),
default => $this->readDefault(),
};
if ($code === self::XLS_TYPE_EOF) {
break;
}
}
// Resolve indexed colors for font, fill, and border colors
// Cannot be resolved already in XF record, because PALETTE record comes afterwards
if (!$this->readDataOnly) {
foreach ($this->objFonts as $objFont) {
if (isset($objFont->colorIndex)) {
$color = Xls\Color::map($objFont->colorIndex, $this->palette, $this->version);
$objFont->getColor()->setRGB($color['rgb']);
}
}
foreach ($this->spreadsheet->getCellXfCollection() as $objStyle) {
// fill start and end color
$fill = $objStyle->getFill();
if (isset($fill->startcolorIndex)) {
$startColor = Xls\Color::map($fill->startcolorIndex, $this->palette, $this->version);
$fill->getStartColor()->setRGB($startColor['rgb']);
}
if (isset($fill->endcolorIndex)) {
$endColor = Xls\Color::map($fill->endcolorIndex, $this->palette, $this->version);
$fill->getEndColor()->setRGB($endColor['rgb']);
}
// border colors
$top = $objStyle->getBorders()->getTop();
$right = $objStyle->getBorders()->getRight();
$bottom = $objStyle->getBorders()->getBottom();
$left = $objStyle->getBorders()->getLeft();
$diagonal = $objStyle->getBorders()->getDiagonal();
if (isset($top->colorIndex)) {
$borderTopColor = Xls\Color::map($top->colorIndex, $this->palette, $this->version);
$top->getColor()->setRGB($borderTopColor['rgb']);
}
if (isset($right->colorIndex)) {
$borderRightColor = Xls\Color::map($right->colorIndex, $this->palette, $this->version);
$right->getColor()->setRGB($borderRightColor['rgb']);
}
if (isset($bottom->colorIndex)) {
$borderBottomColor = Xls\Color::map($bottom->colorIndex, $this->palette, $this->version);
$bottom->getColor()->setRGB($borderBottomColor['rgb']);
}
if (isset($left->colorIndex)) {
$borderLeftColor = Xls\Color::map($left->colorIndex, $this->palette, $this->version);
$left->getColor()->setRGB($borderLeftColor['rgb']);
}
if (isset($diagonal->colorIndex)) {
$borderDiagonalColor = Xls\Color::map($diagonal->colorIndex, $this->palette, $this->version);
$diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
}
}
}
// treat MSODRAWINGGROUP records, workbook-level Escher
$escherWorkbook = null;
if (!$this->readDataOnly && $this->drawingGroupData) {
$escher = new Escher();
$reader = new Xls\Escher($escher);
$escherWorkbook = $reader->load($this->drawingGroupData);
}
// Parse the individual sheets
$this->activeSheetSet = false;
foreach ($this->sheets as $sheet) {
$selectedCells = '';
if ($sheet['sheetType'] != 0x00) {
// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
continue;
}
// check if sheet should be skipped
if (isset($this->loadSheetsOnly) && !in_array($sheet['name'], $this->loadSheetsOnly)) {
continue;
}
// add sheet to PhpSpreadsheet object
$this->phpSheet = $this->spreadsheet->createSheet();
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
// cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
// name in line with the formula, not the reverse
$this->phpSheet->setTitle($sheet['name'], false, false);
$this->phpSheet->setSheetState($sheet['sheetState']);
$this->pos = $sheet['offset'];
// Initialize isFitToPages. May change after reading SHEETPR record.
$this->isFitToPages = false;
// Initialize drawingData
$this->drawingData = '';
// Initialize objs
$this->objs = [];
// Initialize shared formula parts
$this->sharedFormulaParts = [];
// Initialize shared formulas
$this->sharedFormulas = [];
// Initialize text objs
$this->textObjects = [];
// Initialize cell annotations
$this->cellNotes = [];
$this->textObjRef = -1;
while ($this->pos <= $this->dataSize - 4) {
$code = self::getUInt2d($this->data, $this->pos);
switch ($code) {
case self::XLS_TYPE_BOF:
$this->readBof();
break;
case self::XLS_TYPE_PRINTGRIDLINES:
$this->readPrintGridlines();
break;
case self::XLS_TYPE_DEFAULTROWHEIGHT:
$this->readDefaultRowHeight();
break;
case self::XLS_TYPE_SHEETPR:
$this->readSheetPr();
break;
case self::XLS_TYPE_HORIZONTALPAGEBREAKS:
$this->readHorizontalPageBreaks();
break;
case self::XLS_TYPE_VERTICALPAGEBREAKS:
$this->readVerticalPageBreaks();
break;
case self::XLS_TYPE_HEADER:
$this->readHeader();
break;
case self::XLS_TYPE_FOOTER:
$this->readFooter();
break;
case self::XLS_TYPE_HCENTER:
$this->readHcenter();
break;
case self::XLS_TYPE_VCENTER:
$this->readVcenter();
break;
case self::XLS_TYPE_LEFTMARGIN:
$this->readLeftMargin();
break;
case self::XLS_TYPE_RIGHTMARGIN:
$this->readRightMargin();
break;
case self::XLS_TYPE_TOPMARGIN:
$this->readTopMargin();
break;
case self::XLS_TYPE_BOTTOMMARGIN:
$this->readBottomMargin();
break;
case self::XLS_TYPE_PAGESETUP:
$this->readPageSetup();
break;
case self::XLS_TYPE_PROTECT:
$this->readProtect();
break;
case self::XLS_TYPE_SCENPROTECT:
$this->readScenProtect();
break;
case self::XLS_TYPE_OBJECTPROTECT:
$this->readObjectProtect();
break;
case self::XLS_TYPE_PASSWORD:
$this->readPassword();
break;
case self::XLS_TYPE_DEFCOLWIDTH:
$this->readDefColWidth();
break;
case self::XLS_TYPE_COLINFO:
$this->readColInfo();
break;
case self::XLS_TYPE_DIMENSION:
$this->readDefault();
break;
case self::XLS_TYPE_ROW:
$this->readRow();
break;
case self::XLS_TYPE_DBCELL:
$this->readDefault();
break;
case self::XLS_TYPE_RK:
$this->readRk();
break;
case self::XLS_TYPE_LABELSST:
$this->readLabelSst();
break;
case self::XLS_TYPE_MULRK:
$this->readMulRk();
break;
case self::XLS_TYPE_NUMBER:
$this->readNumber();
break;
case self::XLS_TYPE_FORMULA:
$this->readFormula();
break;
case self::XLS_TYPE_SHAREDFMLA:
$this->readSharedFmla();
break;
case self::XLS_TYPE_BOOLERR:
$this->readBoolErr();
break;
case self::XLS_TYPE_MULBLANK:
$this->readMulBlank();
break;
case self::XLS_TYPE_LABEL:
$this->readLabel();
break;
case self::XLS_TYPE_BLANK:
$this->readBlank();
break;
case self::XLS_TYPE_MSODRAWING:
$this->readMsoDrawing();
break;
case self::XLS_TYPE_OBJ:
$this->readObj();
break;
case self::XLS_TYPE_WINDOW2:
$this->readWindow2();
break;
case self::XLS_TYPE_PAGELAYOUTVIEW:
$this->readPageLayoutView();
break;
case self::XLS_TYPE_SCL:
$this->readScl();
break;
case self::XLS_TYPE_PANE:
$this->readPane();
break;
case self::XLS_TYPE_SELECTION:
$selectedCells = $this->readSelection();
break;
case self::XLS_TYPE_MERGEDCELLS:
$this->readMergedCells();
break;
case self::XLS_TYPE_HYPERLINK:
$this->readHyperLink();
break;
case self::XLS_TYPE_DATAVALIDATIONS:
$this->readDataValidations();
break;
case self::XLS_TYPE_DATAVALIDATION:
$this->readDataValidation();
break;
case self::XLS_TYPE_CFHEADER:
$cellRangeAddresses = $this->readCFHeader();
break;
case self::XLS_TYPE_CFRULE:
$this->readCFRule($cellRangeAddresses ?? []);
break;
case self::XLS_TYPE_SHEETLAYOUT:
$this->readSheetLayout();
break;
case self::XLS_TYPE_SHEETPROTECTION:
$this->readSheetProtection();
break;
case self::XLS_TYPE_RANGEPROTECTION:
$this->readRangeProtection();
break;
case self::XLS_TYPE_NOTE:
$this->readNote();
break;
case self::XLS_TYPE_TXO:
$this->readTextObject();
break;
case self::XLS_TYPE_CONTINUE:
$this->readContinue();
break;
case self::XLS_TYPE_EOF:
$this->readDefault();
break 2;
default:
$this->readDefault();
break;
}
}
// treat MSODRAWING records, sheet-level Escher
if (!$this->readDataOnly && $this->drawingData) {
$escherWorksheet = new Escher();
$reader = new Xls\Escher($escherWorksheet);
$escherWorksheet = $reader->load($this->drawingData);
// get all spContainers in one long array, so they can be mapped to OBJ records
/** @var SpContainer[] $allSpContainers */
$allSpContainers = method_exists($escherWorksheet, 'getDgContainer') ? $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers() : [];
}
// treat OBJ records
foreach ($this->objs as $n => $obj) {
// the first shape container never has a corresponding OBJ record, hence $n + 1
if (isset($allSpContainers[$n + 1])) {
$spContainer = $allSpContainers[$n + 1];
// we skip all spContainers that are a part of a group shape since we cannot yet handle those
if ($spContainer->getNestingLevel() > 1) {
continue;
}
// calculate the width and height of the shape
/** @var int $startRow */
[$startColumn, $startRow] = Coordinate::coordinateFromString($spContainer->getStartCoordinates());
/** @var int $endRow */
[$endColumn, $endRow] = Coordinate::coordinateFromString($spContainer->getEndCoordinates());