| 
									
										
										
										
											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; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Spreadsheet; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 13:09:32 +00:00
										 |  |  | error_reporting(E_ALL); | 
					
						
							|  |  |  | set_time_limit(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | date_default_timezone_set('Europe/London'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | ?>
 | 
					
						
							|  |  |  | <html> | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 13:09:32 +00:00
										 |  |  | <title>PhpSpreadsheet Reader Example #14</title>
 | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | </head> | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 13:09:32 +00:00
										 |  |  | <h1>PhpSpreadsheet Reader Example #14</h1>
 | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | <h2>Reading a Large CSV file in "Chunks" to split across multiple Worksheets</h2> | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 13:09:32 +00:00
										 |  |  | require_once __DIR__ . '/../../../src/Bootstrap.php'; | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 08:39:23 +00:00
										 |  |  | $inputFileType = 'Csv'; | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | $inputFileName = './sampleData/example2.csv'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | /**  Define a Read Filter class implementing IReadFilter  */ | 
					
						
							|  |  |  | class chunkReadFilter implements IReadFilter | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     private $_startRow = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private $_endRow = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 08:39:23 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Set the list of rows that we want to read. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $startRow | 
					
						
							|  |  |  |      * @param mixed $chunkSize | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  |     public function setRows($startRow, $chunkSize) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->_startRow = $startRow; | 
					
						
							|  |  |  |         $this->_endRow = $startRow + $chunkSize; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function readCell($column, $row, $worksheetName = '') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         //  Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
 | 
					
						
							|  |  |  |         if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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  **/ | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | $reader = IOFactory::createReader($inputFileType); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | echo '<hr />'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*  Define how many rows we want to read for each "chunk"  **/ | 
					
						
							|  |  |  | $chunkSize = 100; | 
					
						
							|  |  |  | /*  Create a new Instance of our Read Filter  **/ | 
					
						
							|  |  |  | $chunkFilter = new chunkReadFilter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*  Tell the Reader that we want to use the Read Filter that we've Instantiated  **/ | 
					
						
							|  |  |  | /*    and that we want to store it in contiguous rows/columns  **/ | 
					
						
							| 
									
										
										
										
											2016-12-03 13:32:54 +00:00
										 |  |  | $reader->setReadFilter($chunkFilter) | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  |           ->setContiguous(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 13:09:32 +00:00
										 |  |  | /*  Instantiate a new PhpSpreadsheet object manually  **/ | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | $spreadsheet = new Spreadsheet(); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*  Set a sheet index  **/ | 
					
						
							|  |  |  | $sheet = 0; | 
					
						
							|  |  |  | /*  Loop to read our worksheet in "chunk size" blocks  **/ | 
					
						
							|  |  |  | /**  $startRow is set to 2 initially because we always read the headings in row #1  **/ | 
					
						
							|  |  |  | for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { | 
					
						
							| 
									
										
										
										
											2017-01-05 03:14:44 +00:00
										 |  |  |     echo 'Loading WorkSheet #', ($sheet + 1), ' using configurable filter for headings row 1 and for rows ', $startRow, ' to ', ($startRow + $chunkSize - 1), '<br />'; | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  |     /*  Tell the Read Filter, the limits on which rows we want to read this iteration  **/ | 
					
						
							|  |  |  |     $chunkFilter->setRows($startRow, $chunkSize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*  Increment the worksheet index pointer for the Reader  **/ | 
					
						
							| 
									
										
										
										
											2016-12-03 13:32:54 +00:00
										 |  |  |     $reader->setSheetIndex($sheet); | 
					
						
							| 
									
										
										
										
											2017-03-24 13:09:32 +00:00
										 |  |  |     /*  Load only the rows that match our filter into a new worksheet in the PhpSpreadsheet Object  **/ | 
					
						
							| 
									
										
										
										
											2016-12-03 13:32:54 +00:00
										 |  |  |     $reader->loadIntoExisting($inputFileName, $spreadsheet); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  |     /*  Set the worksheet title (to reference the "sheet" of data that we've loaded)  **/ | 
					
						
							|  |  |  |     /*    and increment the sheet index as well  **/ | 
					
						
							| 
									
										
										
										
											2016-11-27 15:51:44 +00:00
										 |  |  |     $spreadsheet->getActiveSheet()->setTitle('Country Data #' . (++$sheet)); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo '<hr />'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 03:14:44 +00:00
										 |  |  | echo $spreadsheet->getSheetCount(), ' worksheet', (($spreadsheet->getSheetCount() == 1) ? '' : 's'), ' loaded<br /><br />'; | 
					
						
							| 
									
										
										
										
											2016-11-27 15:51:44 +00:00
										 |  |  | $loadedSheetNames = $spreadsheet->getSheetNames(); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  | foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) { | 
					
						
							| 
									
										
										
										
											2017-01-05 03:14:44 +00:00
										 |  |  |     echo '<b>Worksheet #', $sheetIndex, ' -> ', $loadedSheetName, '</b><br />'; | 
					
						
							| 
									
										
										
										
											2016-11-27 15:51:44 +00:00
										 |  |  |     $spreadsheet->setActiveSheetIndexByName($loadedSheetName); | 
					
						
							|  |  |  |     $sheetData = $spreadsheet->getActiveSheet()->toArray(null, false, false, true); | 
					
						
							| 
									
										
										
										
											2016-11-27 06:45:15 +00:00
										 |  |  |     var_dump($sheetData); | 
					
						
							|  |  |  |     echo '<br />'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 | 
					
						
							|  |  |  | <body> | 
					
						
							| 
									
										
										
										
											2010-10-20 11:30:09 +00:00
										 |  |  | </html> |