1 | <?php |
||
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) |
|
59 | |||
60 | 4 | public function setMaxCol($maxCol = null) |
|
64 | |||
65 | 2 | public function count() |
|
66 | { |
||
88 | |||
89 | 2 | public function rewind() |
|
106 | |||
107 | } |
||
108 | |||
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.