Skip to content

Commit 0fa4dd3

Browse files
committed
replace excel with spreadsheet as the underlying lib supports different spreadsheet formats
1 parent 038e40f commit 0fa4dd3

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/SpreadsheetReader.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
*
3535
* @author David de Boer <[email protected]>
3636
*
37-
* @see http://phpexcel.codeplex.com/
38-
* @see https://github.com/logiQ/PHPSpreadsheet
37+
* @see https://github.com/PHPOffice/PhpSpreadsheet
3938
*/
4039
class SpreadsheetReader implements CountableReader, \SeekableIterator
4140
{
@@ -71,29 +70,29 @@ class SpreadsheetReader implements CountableReader, \SeekableIterator
7170
* @param \SplFileObject $file Spreadsheet file
7271
* @param int $headerRowNumber Optional number of header row
7372
* @param int $activeSheet Index of active sheet to read from
74-
* @param bool $readOnly If set to false, the reader take care of the excel formatting (slow)
73+
* @param bool $readOnly If set to false, the reader take care of the spreadsheet formatting (slow)
7574
* @param int $maxRows Maximum number of rows to read
7675
*/
7776
public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null)
7877
{
7978
// phpcs:enable Generic.Files.LineLength.MaxExceeded
8079
$reader = IOFactory::createReaderForFile($file->getPathName());
8180
$reader->setReadDataOnly($readOnly);
82-
/** @var Spreadsheet $excel */
83-
$excel = $reader->load($file->getPathname());
81+
/** @var Spreadsheet $spreadsheet */
82+
$spreadsheet = $reader->load($file->getPathname());
8483

8584
if (null !== $activeSheet) {
86-
$excel->setActiveSheetIndex($activeSheet);
85+
$spreadsheet->setActiveSheetIndex($activeSheet);
8786
}
8887

8988
/** @var Worksheet $sheet */
90-
$sheet = $excel->getActiveSheet();
89+
$sheet = $spreadsheet->getActiveSheet();
9190

9291
if ($maxRows && $maxRows < $sheet->getHighestDataRow()) {
9392
$maxColumn = $sheet->getHighestDataColumn();
9493
$this->worksheet = $sheet->rangeToArray('A1:'.$maxColumn.$maxRows);
9594
} else {
96-
$this->worksheet = $excel->getActiveSheet()->toArray();
95+
$this->worksheet = $spreadsheet->getActiveSheet()->toArray();
9796
}
9897

9998
if (null !== $headerRowNumber) {
@@ -127,7 +126,7 @@ public function current()
127126
{
128127
$row = $this->worksheet[$this->pointer];
129128

130-
// If the excel file has column headers, use them to construct an associative
129+
// If the spreadsheet file has column headers, use them to construct an associative
131130
// array for the columns in this line
132131
if (count($this->columnHeaders) === count($row)) {
133132
return array_combine(array_values($this->columnHeaders), $row);

src/SpreadsheetWriter.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SpreadsheetWriter implements Writer
3838
/**
3939
* @var Spreadsheet
4040
*/
41-
protected $excel;
41+
protected $spreadsheet;
4242

4343
/**
4444
* @var string
@@ -86,7 +86,7 @@ public function __construct(\SplFileObject $file, $sheet = null, $type = 'Xlsx',
8686
*/
8787
public function finish()
8888
{
89-
$writer = IOFactory::createWriter($this->excel, $this->type);
89+
$writer = IOFactory::createWriter($this->spreadsheet, $this->type);
9090
$writer->save($this->filename);
9191
}
9292

@@ -99,16 +99,16 @@ public function prepare()
9999
{
100100
$reader = IOFactory::createReader($this->type);
101101
if ($reader->canRead($this->filename)) {
102-
$this->excel = $reader->load($this->filename);
102+
$this->spreadsheet = $reader->load($this->filename);
103103
} else {
104-
$this->excel = new Spreadsheet();
104+
$this->spreadsheet = new Spreadsheet();
105105
}
106106

107107
if (null !== $this->sheet) {
108-
if (!$this->excel->sheetNameExists($this->sheet)) {
109-
$this->excel->createSheet()->setTitle($this->sheet);
108+
if (!$this->spreadsheet->sheetNameExists($this->sheet)) {
109+
$this->spreadsheet->createSheet()->setTitle($this->sheet);
110110
}
111-
$this->excel->setActiveSheetIndexByName($this->sheet);
111+
$this->spreadsheet->setActiveSheetIndexByName($this->sheet);
112112
}
113113
}
114114

@@ -127,15 +127,15 @@ public function writeItem(array $item)
127127
$headers = array_keys($item);
128128

129129
for ($i = 0; $i < $count; $i++) {
130-
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $headers[$i]);
130+
$this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $headers[$i]);
131131
}
132132
$this->row++;
133133
}
134134

135135
$values = array_values($item);
136136

137137
for ($i = 0; $i < $count; $i++) {
138-
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $values[$i]);
138+
$this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $values[$i]);
139139
}
140140

141141
$this->row++;

0 commit comments

Comments
 (0)