Skip to content

Commit 09adc5e

Browse files
committed
Load one row at a time
When the excel sheet being read has many rows (in my case 6700+) we run out of memory. This way we load a row per iteration.
1 parent e786be6 commit 09adc5e

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/Reader/ExcelReader.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515
class ExcelReader implements CountableReader, \SeekableIterator
1616
{
1717
/**
18-
* @var array
18+
* @var \PHPExcel_Worksheet
1919
*/
2020
protected $worksheet;
2121

22+
/**
23+
* @var string
24+
*/
25+
protected $maxColumn;
26+
27+
/**
28+
* @var int
29+
*/
30+
protected $maxRow;
31+
2232
/**
2333
* @var integer
2434
*/
@@ -27,7 +37,7 @@ class ExcelReader implements CountableReader, \SeekableIterator
2737
/**
2838
* @var integer
2939
*/
30-
protected $pointer = 0;
40+
protected $pointer = 1;
3141

3242
/**
3343
* @var array
@@ -58,7 +68,9 @@ public function __construct(\SplFileObject $file, $headerRowNumber = null, $acti
5868
$excel->setActiveSheetIndex($activeSheet);
5969
}
6070

61-
$this->worksheet = $excel->getActiveSheet()->toArray();
71+
$this->worksheet = $excel->getActiveSheet();
72+
$this->maxColumn = $this->worksheet->getHighestColumn();
73+
$this->maxRow = $this->worksheet->getHighestRow();
6274

6375
if (null !== $headerRowNumber) {
6476
$this->setHeaderRowNumber($headerRowNumber);
@@ -74,7 +86,7 @@ public function __construct(\SplFileObject $file, $headerRowNumber = null, $acti
7486
*/
7587
public function current()
7688
{
77-
$row = $this->worksheet[$this->pointer];
89+
$row = current($this->worksheet->rangeToArray(sprintf('A%d:%s%d',$this->pointer,$this->maxColumn,$this->pointer)));
7890

7991
// If the CSV has column headers, use them to construct an associative
8092
// array for the columns in this line
@@ -120,7 +132,7 @@ public function setColumnHeaders(array $columnHeaders)
120132
public function rewind()
121133
{
122134
if (null === $this->headerRowNumber) {
123-
$this->pointer = 0;
135+
$this->pointer = 1;
124136
} else {
125137
$this->pointer = $this->headerRowNumber + 1;
126138
}
@@ -133,8 +145,11 @@ public function rewind()
133145
*/
134146
public function setHeaderRowNumber($rowNumber)
135147
{
148+
$rowNumber++;
136149
$this->headerRowNumber = $rowNumber;
137-
$this->columnHeaders = $this->worksheet[$rowNumber];
150+
$res = $this->worksheet->rangeToArray(sprintf('A%d:%s%d',$rowNumber,$this->maxColumn,$rowNumber));
151+
$this->columnHeaders = current($res);
152+
$this->pointer = $rowNumber;
138153
}
139154

140155
/**
@@ -150,7 +165,7 @@ public function next()
150165
*/
151166
public function valid()
152167
{
153-
return isset($this->worksheet[$this->pointer]);
168+
return ($this->pointer < $this->maxRow);
154169
}
155170

156171
/**
@@ -174,12 +189,12 @@ public function seek($pointer)
174189
*/
175190
public function count()
176191
{
177-
$count = count($this->worksheet);
192+
$maxRow = $this->maxRow;
178193
if (null !== $this->headerRowNumber) {
179-
$count--;
194+
$maxRow -= $this->headerRowNumber;
180195
}
181196

182-
return $count;
197+
return $maxRow;
183198
}
184199

185200
/**

0 commit comments

Comments
 (0)