|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Reader; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Reads Excel files with the help of PHPExcel |
|
7
|
|
|
* |
|
8
|
|
|
* PHPExcel must be installed. |
|
9
|
|
|
* |
|
10
|
|
|
* @author David de Boer <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @link http://phpexcel.codeplex.com/ |
|
13
|
|
|
* @link https://github.com/logiQ/PHPExcel |
|
14
|
|
|
*/ |
|
15
|
|
|
class ExcelReader implements CountableReader, \SeekableIterator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $worksheet; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var integer |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $headerRowNumber; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var integer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $pointer = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $columnHeaders; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Total number of rows |
|
39
|
|
|
* |
|
40
|
|
|
* @var integer |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $count; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \SplFileObject $file Excel file |
|
46
|
|
|
* @param integer $headerRowNumber Optional number of header row |
|
47
|
|
|
* @param integer $activeSheet Index of active sheet to read from |
|
48
|
|
|
* @param boolean $readOnly If set to false, the reader take care of the excel formatting (slow) |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true) |
|
51
|
|
|
{ |
|
52
|
6 |
|
$reader = \PHPExcel_IOFactory::createReaderForFile($file->getPathName()); |
|
53
|
6 |
|
$reader->setReadDataOnly($readOnly); |
|
54
|
|
|
/** @var \PHPExcel $excel */ |
|
55
|
6 |
|
$excel = $reader->load($file->getPathname()); |
|
56
|
|
|
|
|
57
|
6 |
|
if (null !== $activeSheet) { |
|
58
|
1 |
|
$excel->setActiveSheetIndex($activeSheet); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
$this->worksheet = $excel->getActiveSheet()->toArray(); |
|
62
|
|
|
|
|
63
|
6 |
|
if (null !== $headerRowNumber) { |
|
64
|
4 |
|
$this->setHeaderRowNumber($headerRowNumber); |
|
65
|
4 |
|
} |
|
66
|
6 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return the current row as an array |
|
70
|
|
|
* |
|
71
|
|
|
* If a header row has been set, an associative array will be returned |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function current() |
|
76
|
|
|
{ |
|
77
|
1 |
|
$row = $this->worksheet[$this->pointer]; |
|
78
|
|
|
|
|
79
|
|
|
// If the CSV has column headers, use them to construct an associative |
|
80
|
|
|
// array for the columns in this line |
|
81
|
1 |
|
if (!empty($this->columnHeaders)) { |
|
82
|
|
|
// Count the number of elements in both: they must be equal. |
|
83
|
|
|
// If not, ignore the row |
|
84
|
1 |
|
if (count($this->columnHeaders) == count($row)) { |
|
85
|
1 |
|
return array_combine(array_values($this->columnHeaders), $row); |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
// Else just return the column values |
|
89
|
|
|
return $row; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get column headers |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getColumnHeaders() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->columnHeaders; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set column headers |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $columnHeaders |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setColumnHeaders(array $columnHeaders) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->columnHeaders = $columnHeaders; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Rewind the file pointer |
|
115
|
|
|
* |
|
116
|
|
|
* If a header row has been set, the pointer is set just below the header |
|
117
|
|
|
* row. That way, when you iterate over the rows, that header row is |
|
118
|
|
|
* skipped. |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function rewind() |
|
121
|
|
|
{ |
|
122
|
1 |
|
if (null === $this->headerRowNumber) { |
|
123
|
|
|
$this->pointer = 0; |
|
124
|
|
|
} else { |
|
125
|
1 |
|
$this->pointer = $this->headerRowNumber + 1; |
|
126
|
|
|
} |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set header row number |
|
131
|
|
|
* |
|
132
|
|
|
* @param integer $rowNumber Number of the row that contains column header names |
|
133
|
|
|
*/ |
|
134
|
4 |
|
public function setHeaderRowNumber($rowNumber) |
|
135
|
|
|
{ |
|
136
|
4 |
|
$this->headerRowNumber = $rowNumber; |
|
137
|
4 |
|
$this->columnHeaders = $this->worksheet[$rowNumber]; |
|
138
|
4 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function next() |
|
144
|
|
|
{ |
|
145
|
1 |
|
$this->pointer++; |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function valid() |
|
152
|
|
|
{ |
|
153
|
1 |
|
return isset($this->worksheet[$this->pointer]); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
|
|
public function key() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->pointer; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
|
|
public function seek($pointer) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->pointer = $pointer; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* {@inheritdoc} |
|
174
|
|
|
*/ |
|
175
|
4 |
|
public function count() |
|
176
|
|
|
{ |
|
177
|
4 |
|
$count = count($this->worksheet); |
|
178
|
4 |
|
if (null !== $this->headerRowNumber) { |
|
179
|
2 |
|
$count--; |
|
180
|
2 |
|
} |
|
181
|
|
|
|
|
182
|
4 |
|
return $count; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function getFields() |
|
189
|
|
|
{ |
|
190
|
1 |
|
return $this->columnHeaders; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get a row |
|
195
|
|
|
* |
|
196
|
|
|
* @param integer $number |
|
197
|
|
|
* |
|
198
|
|
|
* @return array |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getRow($number) |
|
201
|
|
|
{ |
|
202
|
|
|
$this->seek($number); |
|
203
|
|
|
|
|
204
|
|
|
return $this->current(); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|