1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Sample; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter; |
7
|
|
|
|
8
|
|
|
require __DIR__ . '/../Header.php'; |
9
|
|
|
|
10
|
|
|
$inputFileType = 'Xls'; |
11
|
|
|
$inputFileName = __DIR__ . '/sampleData/example2.xls'; |
12
|
|
|
|
13
|
|
|
/** Define a Read Filter class implementing IReadFilter */ |
14
|
|
View Code Duplication |
class ChunkReadFilter implements IReadFilter |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
private $_startRow = 0; |
17
|
|
|
private $_endRow = 0; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* We expect a list of the rows that we want to read to be passed into the constructor. |
21
|
|
|
* |
22
|
|
|
* @param mixed $startRow |
23
|
|
|
* @param mixed $chunkSize |
24
|
|
|
*/ |
25
|
|
|
public function __construct($startRow, $chunkSize) |
26
|
|
|
{ |
27
|
|
|
$this->_startRow = $startRow; |
28
|
|
|
$this->_endRow = $startRow + $chunkSize; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function readCell($column, $row, $worksheetName = '') |
32
|
|
|
{ |
33
|
|
|
// Only read the heading row, and the rows that were configured in the constructor |
34
|
|
|
if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) { |
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$helper->log('Loading file ' . pathinfo($inputFileName, PATHINFO_BASENAME) . ' using IOFactory with a defined reader type of ' . $inputFileType); |
43
|
|
|
/* Create a new Reader of the type defined in $inputFileType * */ |
44
|
|
|
$reader = IOFactory::createReader($inputFileType); |
45
|
|
|
|
46
|
|
|
/* Define how many rows we want for each "chunk" * */ |
47
|
|
|
$chunkSize = 20; |
48
|
|
|
|
49
|
|
|
/* Loop to read our worksheet in "chunk size" blocks * */ |
50
|
|
|
for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { |
51
|
|
|
$helper->log('Loading WorkSheet using configurable filter for headings row 1 and for rows ' . $startRow . ' to ' . ($startRow + $chunkSize - 1)); |
52
|
|
|
/* Create a new Instance of our Read Filter, passing in the limits on which rows we want to read * */ |
53
|
|
|
$chunkFilter = new ChunkReadFilter($startRow, $chunkSize); |
54
|
|
|
/* Tell the Reader that we want to use the new Read Filter that we've just Instantiated * */ |
55
|
|
|
$reader->setReadFilter($chunkFilter); |
56
|
|
|
/* Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object * */ |
57
|
|
|
$spreadsheet = $reader->load($inputFileName); |
58
|
|
|
|
59
|
|
|
// Do some processing here |
60
|
|
|
|
61
|
|
|
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true); |
62
|
|
|
var_dump($sheetData); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.