1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Samples\Sample12; |
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
|
|
|
* Set the list of rows that we want to read. |
21
|
|
|
* |
22
|
|
|
* @param mixed $startRow |
23
|
|
|
* @param mixed $chunkSize |
24
|
|
|
*/ |
25
|
|
|
public function setRows($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 are configured in $this->_startRow and $this->_endRow |
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 to read for each "chunk" |
47
|
|
|
$chunkSize = 20; |
48
|
|
|
// Create a new Instance of our Read Filter |
49
|
|
|
$chunkFilter = new ChunkReadFilter(); |
50
|
|
|
|
51
|
|
|
// Tell the Reader that we want to use the Read Filter that we've Instantiated |
52
|
|
|
$reader->setReadFilter($chunkFilter); |
53
|
|
|
|
54
|
|
|
// Loop to read our worksheet in "chunk size" blocks |
55
|
|
|
for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { |
56
|
|
|
$helper->log('Loading WorkSheet using configurable filter for headings row 1 and for rows ' . $startRow . ' to ' . ($startRow + $chunkSize - 1)); |
57
|
|
|
// Tell the Read Filter, the limits on which rows we want to read this iteration |
58
|
|
|
$chunkFilter->setRows($startRow, $chunkSize); |
59
|
|
|
// Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object |
60
|
|
|
$spreadsheet = $reader->load($inputFileName); |
61
|
|
|
|
62
|
|
|
// Do some processing here |
63
|
|
|
|
64
|
|
|
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true); |
65
|
|
|
var_dump($sheetData); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
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.