1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader\Xls; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xls; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
8
|
|
|
|
9
|
|
|
class ListFunctions extends Xls |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object. |
13
|
|
|
*/ |
14
|
6 |
|
protected function listWorksheetNames2(string $filename, Xls $xls): array |
15
|
|
|
{ |
16
|
6 |
|
File::assertFile($filename); |
17
|
|
|
|
18
|
6 |
|
$worksheetNames = []; |
19
|
|
|
|
20
|
|
|
// Read the OLE file |
21
|
6 |
|
$xls->loadOLE($filename); |
22
|
|
|
|
23
|
|
|
// total byte size of Excel data (workbook global substream + sheet substreams) |
24
|
6 |
|
$xls->dataSize = strlen($xls->data); |
25
|
|
|
|
26
|
6 |
|
$xls->pos = 0; |
27
|
6 |
|
$xls->sheets = []; |
28
|
|
|
|
29
|
|
|
// Parse Workbook Global Substream |
30
|
6 |
|
while ($xls->pos < $xls->dataSize) { |
31
|
6 |
|
$code = self::getUInt2d($xls->data, $xls->pos); |
32
|
|
|
|
33
|
|
|
match ($code) { |
34
|
6 |
|
self::XLS_TYPE_BOF => $xls->readBof(), |
35
|
6 |
|
self::XLS_TYPE_SHEET => $xls->readSheet(), |
36
|
6 |
|
self::XLS_TYPE_EOF => $xls->readDefault(), |
37
|
6 |
|
self::XLS_TYPE_CODEPAGE => $xls->readCodepage(), |
38
|
6 |
|
default => $xls->readDefault(), |
39
|
|
|
}; |
40
|
|
|
|
41
|
6 |
|
if ($code === self::XLS_TYPE_EOF) { |
42
|
6 |
|
break; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
foreach ($xls->sheets as $sheet) { |
47
|
6 |
|
if ($sheet['sheetType'] != 0x00) { |
48
|
|
|
// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
$worksheetNames[] = $sheet['name']; |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
return $worksheetNames; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
60
|
|
|
*/ |
61
|
4 |
|
protected function listWorksheetInfo2(string $filename, Xls $xls): array |
62
|
|
|
{ |
63
|
4 |
|
File::assertFile($filename); |
64
|
|
|
|
65
|
4 |
|
$worksheetInfo = []; |
66
|
|
|
|
67
|
|
|
// Read the OLE file |
68
|
4 |
|
$xls->loadOLE($filename); |
69
|
|
|
|
70
|
|
|
// total byte size of Excel data (workbook global substream + sheet substreams) |
71
|
4 |
|
$xls->dataSize = strlen($xls->data); |
72
|
|
|
|
73
|
|
|
// initialize |
74
|
4 |
|
$xls->pos = 0; |
75
|
4 |
|
$xls->sheets = []; |
76
|
|
|
|
77
|
|
|
// Parse Workbook Global Substream |
78
|
4 |
|
while ($xls->pos < $xls->dataSize) { |
79
|
4 |
|
$code = self::getUInt2d($xls->data, $xls->pos); |
80
|
|
|
|
81
|
|
|
match ($code) { |
82
|
4 |
|
self::XLS_TYPE_BOF => $xls->readBof(), |
83
|
4 |
|
self::XLS_TYPE_SHEET => $xls->readSheet(), |
84
|
4 |
|
self::XLS_TYPE_EOF => $xls->readDefault(), |
85
|
4 |
|
self::XLS_TYPE_CODEPAGE => $xls->readCodepage(), |
86
|
4 |
|
default => $xls->readDefault(), |
87
|
|
|
}; |
88
|
|
|
|
89
|
4 |
|
if ($code === self::XLS_TYPE_EOF) { |
90
|
4 |
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Parse the individual sheets |
95
|
4 |
|
foreach ($xls->sheets as $sheet) { |
96
|
4 |
|
if ($sheet['sheetType'] != 0x00) { |
97
|
|
|
// 0x00: Worksheet |
98
|
|
|
// 0x02: Chart |
99
|
|
|
// 0x06: Visual Basic module |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
$tmpInfo = []; |
104
|
4 |
|
$tmpInfo['worksheetName'] = $sheet['name']; |
105
|
4 |
|
$tmpInfo['lastColumnLetter'] = 'A'; |
106
|
4 |
|
$tmpInfo['lastColumnIndex'] = 0; |
107
|
4 |
|
$tmpInfo['totalRows'] = 0; |
108
|
4 |
|
$tmpInfo['totalColumns'] = 0; |
109
|
|
|
|
110
|
4 |
|
$xls->pos = $sheet['offset']; |
111
|
|
|
|
112
|
4 |
|
while ($xls->pos <= $xls->dataSize - 4) { |
113
|
4 |
|
$code = self::getUInt2d($xls->data, $xls->pos); |
114
|
|
|
|
115
|
|
|
switch ($code) { |
116
|
4 |
|
case self::XLS_TYPE_RK: |
117
|
4 |
|
case self::XLS_TYPE_LABELSST: |
118
|
4 |
|
case self::XLS_TYPE_NUMBER: |
119
|
4 |
|
case self::XLS_TYPE_FORMULA: |
120
|
4 |
|
case self::XLS_TYPE_BOOLERR: |
121
|
4 |
|
case self::XLS_TYPE_LABEL: |
122
|
4 |
|
$length = self::getUInt2d($xls->data, $xls->pos + 2); |
123
|
4 |
|
$recordData = $xls->readRecordData($xls->data, $xls->pos + 4, $length); |
124
|
|
|
|
125
|
|
|
// move stream pointer to next record |
126
|
4 |
|
$xls->pos += 4 + $length; |
127
|
|
|
|
128
|
4 |
|
$rowIndex = self::getUInt2d($recordData, 0) + 1; |
129
|
4 |
|
$columnIndex = self::getUInt2d($recordData, 2); |
130
|
|
|
|
131
|
4 |
|
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex); |
132
|
4 |
|
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex); |
133
|
|
|
|
134
|
4 |
|
break; |
135
|
4 |
|
case self::XLS_TYPE_BOF: |
136
|
4 |
|
$xls->readBof(); |
137
|
|
|
|
138
|
4 |
|
break; |
139
|
4 |
|
case self::XLS_TYPE_EOF: |
140
|
4 |
|
$xls->readDefault(); |
141
|
|
|
|
142
|
4 |
|
break 2; |
143
|
|
|
default: |
144
|
4 |
|
$xls->readDefault(); |
145
|
|
|
|
146
|
4 |
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
4 |
|
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1); |
151
|
4 |
|
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1; |
152
|
|
|
|
153
|
4 |
|
$worksheetInfo[] = $tmpInfo; |
154
|
|
|
} |
155
|
|
|
|
156
|
4 |
|
return $worksheetInfo; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|