| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | use PhpOffice\PhpSpreadsheet\IOFactory; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Reader\IReadFilter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | error_reporting(E_ALL); | 
					
						
							|  |  |  | set_time_limit(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | date_default_timezone_set('Europe/London'); | 
					
						
							|  |  |  | ?>
 | 
					
						
							|  |  |  | <html> | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |     <head> | 
					
						
							|  |  |  |         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         <title>PhpSpreadsheet Reader Example #11</title>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     </head> | 
					
						
							|  |  |  |     <body> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         <h1>PhpSpreadsheet Reader Example #11</h1>
 | 
					
						
							|  |  |  |         <h2>Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 1)</h2> | 
					
						
							|  |  |  |         <?php | 
					
						
							|  |  |  |         require_once __DIR__ . '/../../../src/Bootstrap.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $inputFileType = 'Xls'; | 
					
						
							|  |  |  |         $inputFileName = './sampleData/example2.xls'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /**  Define a Read Filter class implementing IReadFilter  */ | 
					
						
							|  |  |  |         class chunkReadFilter implements IReadFilter | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             private $_startRow = 0; | 
					
						
							|  |  |  |             private $_endRow = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /** | 
					
						
							|  |  |  |              * We expect a list of the rows that we want to read to be passed into the constructor. | 
					
						
							|  |  |  |              * | 
					
						
							|  |  |  |              * @param mixed $startRow | 
					
						
							|  |  |  |              * @param mixed $chunkSize | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             public function __construct($startRow, $chunkSize) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 $this->_startRow = $startRow; | 
					
						
							|  |  |  |                 $this->_endRow = $startRow + $chunkSize; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             public function readCell($column, $row, $worksheetName = '') | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 //  Only read the heading row, and the rows that were configured in the constructor
 | 
					
						
							|  |  |  |                 if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |         echo 'Loading file ', pathinfo($inputFileName, PATHINFO_BASENAME), ' using IOFactory with a defined reader type of ', $inputFileType, '<br />'; | 
					
						
							|  |  |  |         /*  Create a new Reader of the type defined in $inputFileType  * */ | 
					
						
							|  |  |  |         $reader = IOFactory::createReader($inputFileType); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |         echo '<hr />'; | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |         /*  Define how many rows we want for each "chunk"  * */ | 
					
						
							|  |  |  |         $chunkSize = 20; | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |         /*  Loop to read our worksheet in "chunk size" blocks  * */ | 
					
						
							|  |  |  |         for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { | 
					
						
							|  |  |  |             echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ', $startRow, ' to ', ($startRow + $chunkSize - 1), '<br />'; | 
					
						
							|  |  |  |             /*  Create a new Instance of our Read Filter, passing in the limits on which rows we want to read  * */ | 
					
						
							|  |  |  |             $chunkFilter = new chunkReadFilter($startRow, $chunkSize); | 
					
						
							|  |  |  |             /*  Tell the Reader that we want to use the new Read Filter that we've just Instantiated  * */ | 
					
						
							|  |  |  |             $reader->setReadFilter($chunkFilter); | 
					
						
							|  |  |  |             /*  Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object  * */ | 
					
						
							|  |  |  |             $spreadsheet = $reader->load($inputFileName); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |             //	Do some processing here
 | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 10:03:24 +00:00
										 |  |  |             $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true); | 
					
						
							|  |  |  |             var_dump($sheetData); | 
					
						
							|  |  |  |             echo '<br /><br />'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ?>
 | 
					
						
							|  |  |  |     <body> | 
					
						
							| 
									
										
										
										
											2010-10-13 19:37:19 +00:00
										 |  |  | </html> |