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
|
6 |
|
$worksheetNames[] = $sheet['name']; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
return $worksheetNames; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
58
|
|
|
*/ |
59
|
4 |
|
protected function listWorksheetInfo2(string $filename, Xls $xls): array |
60
|
|
|
{ |
61
|
4 |
|
File::assertFile($filename); |
62
|
|
|
|
63
|
4 |
|
$worksheetInfo = []; |
64
|
|
|
|
65
|
|
|
// Read the OLE file |
66
|
4 |
|
$xls->loadOLE($filename); |
67
|
|
|
|
68
|
|
|
// total byte size of Excel data (workbook global substream + sheet substreams) |
69
|
4 |
|
$xls->dataSize = strlen($xls->data); |
70
|
|
|
|
71
|
|
|
// initialize |
72
|
4 |
|
$xls->pos = 0; |
73
|
4 |
|
$xls->sheets = []; |
74
|
|
|
|
75
|
|
|
// Parse Workbook Global Substream |
76
|
4 |
|
while ($xls->pos < $xls->dataSize) { |
77
|
4 |
|
$code = self::getUInt2d($xls->data, $xls->pos); |
78
|
|
|
|
79
|
|
|
match ($code) { |
80
|
4 |
|
self::XLS_TYPE_BOF => $xls->readBof(), |
81
|
4 |
|
self::XLS_TYPE_SHEET => $xls->readSheet(), |
82
|
4 |
|
self::XLS_TYPE_EOF => $xls->readDefault(), |
83
|
4 |
|
self::XLS_TYPE_CODEPAGE => $xls->readCodepage(), |
84
|
4 |
|
default => $xls->readDefault(), |
85
|
|
|
}; |
86
|
|
|
|
87
|
4 |
|
if ($code === self::XLS_TYPE_EOF) { |
88
|
4 |
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Parse the individual sheets |
93
|
4 |
|
foreach ($xls->sheets as $sheet) { |
94
|
4 |
|
if ($sheet['sheetType'] !== 0x00) { |
95
|
|
|
// 0x00: Worksheet |
96
|
|
|
// 0x02: Chart |
97
|
|
|
// 0x06: Visual Basic module |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
$tmpInfo = []; |
102
|
4 |
|
$tmpInfo['worksheetName'] = $sheet['name']; |
103
|
4 |
|
$tmpInfo['lastColumnLetter'] = 'A'; |
104
|
4 |
|
$tmpInfo['lastColumnIndex'] = 0; |
105
|
4 |
|
$tmpInfo['totalRows'] = 0; |
106
|
4 |
|
$tmpInfo['totalColumns'] = 0; |
107
|
|
|
|
108
|
4 |
|
$xls->pos = $sheet['offset']; |
109
|
|
|
|
110
|
4 |
|
while ($xls->pos <= $xls->dataSize - 4) { |
111
|
4 |
|
$code = self::getUInt2d($xls->data, $xls->pos); |
112
|
|
|
|
113
|
|
|
switch ($code) { |
114
|
4 |
|
case self::XLS_TYPE_RK: |
115
|
4 |
|
case self::XLS_TYPE_LABELSST: |
116
|
4 |
|
case self::XLS_TYPE_NUMBER: |
117
|
4 |
|
case self::XLS_TYPE_FORMULA: |
118
|
4 |
|
case self::XLS_TYPE_BOOLERR: |
119
|
4 |
|
case self::XLS_TYPE_LABEL: |
120
|
4 |
|
$length = self::getUInt2d($xls->data, $xls->pos + 2); |
121
|
4 |
|
$recordData = $xls->readRecordData($xls->data, $xls->pos + 4, $length); |
122
|
|
|
|
123
|
|
|
// move stream pointer to next record |
124
|
4 |
|
$xls->pos += 4 + $length; |
125
|
|
|
|
126
|
4 |
|
$rowIndex = self::getUInt2d($recordData, 0) + 1; |
127
|
4 |
|
$columnIndex = self::getUInt2d($recordData, 2); |
128
|
|
|
|
129
|
4 |
|
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex); |
130
|
4 |
|
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex); |
131
|
|
|
|
132
|
4 |
|
break; |
133
|
4 |
|
case self::XLS_TYPE_BOF: |
134
|
4 |
|
$xls->readBof(); |
135
|
|
|
|
136
|
4 |
|
break; |
137
|
4 |
|
case self::XLS_TYPE_EOF: |
138
|
4 |
|
$xls->readDefault(); |
139
|
|
|
|
140
|
4 |
|
break 2; |
141
|
|
|
default: |
142
|
4 |
|
$xls->readDefault(); |
143
|
|
|
|
144
|
4 |
|
break; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1); |
149
|
4 |
|
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1; |
150
|
|
|
|
151
|
4 |
|
$worksheetInfo[] = $tmpInfo; |
152
|
|
|
} |
153
|
|
|
|
154
|
4 |
|
return $worksheetInfo; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|