1
|
|
|
<?php |
2
|
|
|
namespace Mathielen\DataImport\Reader; |
3
|
|
|
|
4
|
|
|
class ExcelReader extends \Ddeboer\DataImport\Reader\ExcelReader |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
private $activeSheet; |
8
|
|
|
private $file; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var \PHPExcel_Reader_IReader |
12
|
|
|
*/ |
13
|
|
|
private $reader; |
14
|
|
|
private $maxRows = null; |
15
|
|
|
private $maxCol = null; |
16
|
|
|
|
17
|
4 |
|
public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null, $maxCol = null) |
18
|
|
|
{ |
19
|
4 |
|
$this->file = $file; |
20
|
4 |
|
$this->activeSheet = $activeSheet; |
21
|
|
|
|
22
|
4 |
|
$this->reader = \PHPExcel_IOFactory::createReaderForFile($file->getPathName()); |
23
|
4 |
|
$this->reader->setReadDataOnly($readOnly); |
24
|
|
|
|
25
|
4 |
|
if (!is_null($headerRowNumber)) { |
26
|
2 |
|
$this->headerRowNumber = $headerRowNumber; |
27
|
2 |
|
$headerReader = clone $this->reader; |
28
|
2 |
|
$headerReader->setReadFilter(new ReadFilter($headerRowNumber+1)); |
29
|
|
|
|
30
|
|
|
/** @var \PHPExcel $excel */ |
31
|
2 |
|
$excel = $headerReader->load($file->getPathname()); |
32
|
|
|
|
33
|
2 |
|
if (null !== $activeSheet) { |
34
|
|
|
$excel->setActiveSheetIndex($activeSheet); |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
$rows = $excel->getActiveSheet()->toArray(); |
38
|
2 |
|
$this->columnHeaders = $rows[$headerRowNumber]; |
39
|
|
|
|
40
|
|
|
//set max col from header length if not already given |
41
|
2 |
|
if (is_null($maxCol)) { |
42
|
2 |
|
$maxCol = \PHPExcel_Cell::stringFromColumnIndex(count($this->columnHeaders)-1); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$this->setBoundaries($maxRows, $maxCol); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
4 |
|
public function setBoundaries($maxRows = null, $maxCol = null) |
50
|
|
|
{ |
51
|
4 |
|
$this->setMaxRows($maxRows); |
52
|
4 |
|
$this->setMaxCol($maxCol); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
4 |
|
public function setMaxRows($maxRows = null) |
56
|
|
|
{ |
57
|
4 |
|
$this->maxRows = $maxRows; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
4 |
|
public function setMaxCol($maxCol = null) |
61
|
|
|
{ |
62
|
4 |
|
$this->maxCol = $maxCol; |
63
|
4 |
|
} |
64
|
|
|
|
65
|
2 |
|
public function count() |
66
|
|
|
{ |
67
|
2 |
|
if (is_null($this->count)) { |
68
|
2 |
|
$countReader = clone $this->reader; |
69
|
2 |
|
$countReader->setReadFilter(new ReadFilter($this->maxRows, 'A')); |
70
|
|
|
|
71
|
|
|
/** @var \PHPExcel $excel */ |
72
|
2 |
|
$excel = $countReader->load($this->file->getPathname()); |
73
|
|
|
|
74
|
2 |
|
if (null !== $this->activeSheet) { |
75
|
|
|
$excel->setActiveSheetIndex($this->activeSheet); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
$maxRowMaxCol = $excel->getActiveSheet()->getHighestRowAndColumn(); |
79
|
2 |
|
$this->count = $maxRowMaxCol['row']; |
80
|
|
|
|
81
|
2 |
|
if (null !== $this->headerRowNumber) { |
82
|
2 |
|
$this->count--; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return $this->count; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function rewind() |
90
|
|
|
{ |
91
|
2 |
|
if (is_null($this->worksheet)) { |
92
|
2 |
|
$this->reader->setReadFilter(new ReadFilter($this->maxRows, $this->maxCol)); |
93
|
|
|
|
94
|
|
|
/** @var \PHPExcel $excel */ |
95
|
2 |
|
$excel = $this->reader->load($this->file->getPathname()); |
96
|
|
|
|
97
|
2 |
|
if (null !== $this->activeSheet) { |
98
|
|
|
$excel->setActiveSheetIndex($this->activeSheet); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$this->worksheet = $excel->getActiveSheet()->toArray(); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
parent::rewind(); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
class ReadFilter implements \PHPExcel_Reader_IReadFilter |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
|
112
|
|
|
private $maxRows; |
113
|
|
|
private $maxColIdx; |
114
|
|
|
|
115
|
4 |
|
public function __construct($maxRows = null, $maxCol = null) |
116
|
|
|
{ |
117
|
4 |
|
$this->maxRows = $maxRows; |
118
|
4 |
|
$this->maxColIdx = $maxCol?\PHPExcel_Cell::columnIndexFromString($maxCol):null; |
119
|
4 |
|
} |
120
|
|
|
|
121
|
4 |
|
public function readCell($column, $row, $worksheetName = '') |
122
|
|
|
{ |
123
|
4 |
|
if (isset($this->maxRows) && $row > $this->maxRows) { |
124
|
2 |
|
return false; |
125
|
|
|
} |
126
|
4 |
|
if (isset($this->maxColIdx) && \PHPExcel_Cell::columnIndexFromString($column) > $this->maxColIdx) { |
127
|
2 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.