Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/Reader/ExcelReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
class ExcelReader implements CountableReader, \SeekableIterator
{
/**
* @var array
* @var \PHPExcel_Worksheet
*/
protected $worksheet;

/**
* @var string
*/
protected $maxColumn;

/**
* @var int
*/
protected $maxRow;

/**
* @var integer
*/
Expand All @@ -27,7 +37,7 @@ class ExcelReader implements CountableReader, \SeekableIterator
/**
* @var integer
*/
protected $pointer = 0;
protected $pointer = 1;

/**
* @var array
Expand Down Expand Up @@ -58,7 +68,9 @@ public function __construct(\SplFileObject $file, $headerRowNumber = null, $acti
$excel->setActiveSheetIndex($activeSheet);
}

$this->worksheet = $excel->getActiveSheet()->toArray();
$this->worksheet = $excel->getActiveSheet();
$this->maxColumn = $this->worksheet->getHighestColumn();
$this->maxRow = $this->worksheet->getHighestRow();

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

// If the CSV has column headers, use them to construct an associative
// array for the columns in this line
Expand Down Expand Up @@ -120,7 +132,7 @@ public function setColumnHeaders(array $columnHeaders)
public function rewind()
{
if (null === $this->headerRowNumber) {
$this->pointer = 0;
$this->pointer = 1;
} else {
$this->pointer = $this->headerRowNumber + 1;
}
Expand All @@ -133,8 +145,11 @@ public function rewind()
*/
public function setHeaderRowNumber($rowNumber)
{
$rowNumber++;
$this->headerRowNumber = $rowNumber;
$this->columnHeaders = $this->worksheet[$rowNumber];
$res = $this->worksheet->rangeToArray(sprintf('A%d:%s%d',$rowNumber,$this->maxColumn,$rowNumber));
$this->setColumnHeaders(current($res));
$this->pointer = $rowNumber;
}

/**
Expand All @@ -150,7 +165,7 @@ public function next()
*/
public function valid()
{
return isset($this->worksheet[$this->pointer]);
return ($this->pointer < $this->maxRow);
}

/**
Expand All @@ -174,12 +189,12 @@ public function seek($pointer)
*/
public function count()
{
$count = count($this->worksheet);
$maxRow = $this->maxRow;
if (null !== $this->headerRowNumber) {
$count--;
$maxRow -= $this->headerRowNumber;
}

return $count;
return $maxRow;
}

/**
Expand Down