forked from nuovo/spreadsheet-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpreadsheetReader_XLS.php
266 lines (231 loc) · 5.78 KB
/
SpreadsheetReader_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
<?php
/**
* Class for parsing XLS files
*
* @author Martins Pilsetnieks
*/
class SpreadsheetReader_XLS implements Iterator, Countable
{
/**
* @var array Options array, pre-populated with the default values.
*/
private $Options = array(
);
/**
* @var resource File handle
*/
private $Handle = false;
private $Index = 0;
private $Error = false;
/**
* @var array Sheet information
*/
private $Sheets = false;
private $SheetIndexes = array();
/**
* @var int Current sheet index
*/
private $CurrentSheet = 0;
/**
* @var array Content of the current row
*/
private $CurrentRow = array();
/**
* @var int Column count in the sheet
*/
private $ColumnCount = 0;
/**
* @var int Row count in the sheet
*/
private $RowCount = 0;
/**
* @var array Template to use for empty rows. Retrieved rows are merged
* with this so that empty cells are added, too
*/
private $EmptyRow = array();
/**
* @param string Path to file
* @param array Options
*/
public function __construct($Filepath, array $Options = null)
{
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader_XLS: File not readable ('.$Filepath.')');
}
if (!class_exists('Spreadsheet_Excel_Reader'))
{
throw new Exception('SpreadsheetReader_XLS: Spreadsheet_Excel_Reader class not available');
}
$this -> Handle = new Spreadsheet_Excel_Reader($Filepath, false, 'UTF-8');
if (function_exists('mb_convert_encoding'))
{
$this -> Handle -> setUTFEncoder('mb');
}
if (empty($this -> Handle -> sheets))
{
$this -> Error = true;
return null;
}
$this -> ChangeSheet(0);
}
public function __destruct()
{
unset($this -> Handle);
}
/**
* Retrieves an array with information about sheets in the current file
*
* @return array List of sheets (key is sheet index, value is name)
*/
public function Sheets()
{
if ($this -> Sheets === false)
{
$this -> Sheets = array();
$this -> SheetIndexes = array_keys($this -> Handle -> sheets);
foreach ($this -> SheetIndexes as $SheetIndex)
{
$this -> Sheets[] = $this -> Handle -> boundsheets[$SheetIndex]['name'];
}
}
return $this -> Sheets;
}
/**
* Changes the current sheet in the file to another
*
* @param int Sheet index
*
* @return bool True if sheet was successfully changed, false otherwise.
*/
public function ChangeSheet($Index)
{
$Index = (int)$Index;
$Sheets = $this -> Sheets();
if (isset($this -> Sheets[$Index]))
{
$this -> rewind();
$this -> CurrentSheet = $this -> SheetIndexes[$Index];
$this -> ColumnCount = $this -> Handle -> sheets[$this -> CurrentSheet]['numCols'];
$this -> RowCount = $this -> Handle -> sheets[$this -> CurrentSheet]['numRows'];
// For the case when Spreadsheet_Excel_Reader doesn't have the row count set correctly.
if (!$this -> RowCount && count($this -> Handle -> sheets[$this -> CurrentSheet]['cells']))
{
end($this -> Handle -> sheets[$this -> CurrentSheet]['cells']);
$this -> RowCount = (int)key($this -> Handle -> sheets[$this -> CurrentSheet]['cells']);
}
if ($this -> ColumnCount)
{
$this -> EmptyRow = array_fill(1, $this -> ColumnCount, '');
}
else
{
$this -> EmptyRow = array();
}
}
return false;
}
public function __get($Name)
{
switch ($Name)
{
case 'Error':
return $this -> Error;
break;
}
return null;
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
$this -> Index = 0;
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Index == 0)
{
$this -> next();
}
return $this -> CurrentRow;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
// Internal counter is advanced here instead of the if statement
// because apparently it's fully possible that an empty row will not be
// present at all
$this -> Index++;
if ($this -> Error)
{
return array();
}
elseif (isset($this -> Handle -> sheets[$this -> CurrentSheet]['cells'][$this -> Index]))
{
$this -> CurrentRow = $this -> Handle -> sheets[$this -> CurrentSheet]['cells'][$this -> Index];
if (!$this -> CurrentRow)
{
return array();
}
$this -> CurrentRow = $this -> CurrentRow + $this -> EmptyRow;
ksort($this -> CurrentRow);
$this -> CurrentRow = array_values($this -> CurrentRow);
return $this -> CurrentRow;
}
else
{
$this -> CurrentRow = $this -> EmptyRow;
return $this -> CurrentRow;
}
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
return $this -> Index;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
if ($this -> Error)
{
return false;
}
return ($this -> Index <= $this -> RowCount);
}
// !Countable interface method
/**
* Ostensibly should return the count of the contained items but this just returns the number
* of rows read so far. It's not really correct but at least coherent.
*/
public function count()
{
if ($this -> Error)
{
return 0;
}
return $this -> RowCount;
}
}
?>