visualizer
/
vendor
/
phpoffice
/
phpexcel
/
Documentation
/
Examples
/
Reader
/
exampleReader14.php
exampleReader14.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.1.1, at vendor/phpoffice/phpexcel/Documentation/Examples/Reader/exampleReader14.php
| 1 | <?php |
| 2 | |
| 3 | ?> |
| 4 | <html> |
| 5 | <head> |
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 7 | |
| 8 | <title>PHPExcel Reader Example #15</title> |
| 9 | |
| 10 | </head> |
| 11 | <body> |
| 12 | |
| 13 | <h1>PHPExcel Reader Example #14</h1> |
| 14 | <h2>Reading a Large CSV file in "Chunks" to split across multiple Worksheets</h2> |
| 15 | <?php |
| 16 | |
| 17 | /** Include path **/ |
| 18 | set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/'); |
| 19 | |
| 20 | /** PHPExcel_IOFactory */ |
| 21 | include 'PHPExcel/IOFactory.php'; |
| 22 | |
| 23 | |
| 24 | $inputFileType = 'CSV'; |
| 25 | $inputFileName = './sampleData/example2.csv'; |
| 26 | |
| 27 | /** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */ |
| 28 | class chunkReadFilter implements PHPExcel_Reader_IReadFilter |
| 29 | { |
| 30 | private $_startRow = 0; |
| 31 | |
| 32 | private $_endRow = 0; |
| 33 | |
| 34 | /** Set the list of rows that we want to read */ |
| 35 | public function setRows($startRow, $chunkSize) { |
| 36 | $this->_startRow = $startRow; |
| 37 | $this->_endRow = $startRow + $chunkSize; |
| 38 | } |
| 39 | |
| 40 | public function readCell($column, $row, $worksheetName = '') { |
| 41 | // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow |
| 42 | if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) { |
| 43 | return true; |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />'; |
| 51 | /** Create a new Reader of the type defined in $inputFileType **/ |
| 52 | $objReader = PHPExcel_IOFactory::createReader($inputFileType); |
| 53 | |
| 54 | |
| 55 | echo '<hr />'; |
| 56 | |
| 57 | |
| 58 | /** Define how many rows we want to read for each "chunk" **/ |
| 59 | $chunkSize = 100; |
| 60 | /** Create a new Instance of our Read Filter **/ |
| 61 | $chunkFilter = new chunkReadFilter(); |
| 62 | |
| 63 | /** Tell the Reader that we want to use the Read Filter that we've Instantiated **/ |
| 64 | /** and that we want to store it in contiguous rows/columns **/ |
| 65 | $objReader->setReadFilter($chunkFilter) |
| 66 | ->setContiguous(true); |
| 67 | |
| 68 | |
| 69 | /** Instantiate a new PHPExcel object manually **/ |
| 70 | $objPHPExcel = new PHPExcel(); |
| 71 | |
| 72 | /** Set a sheet index **/ |
| 73 | $sheet = 0; |
| 74 | /** Loop to read our worksheet in "chunk size" blocks **/ |
| 75 | /** $startRow is set to 2 initially because we always read the headings in row #1 **/ |
| 76 | for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { |
| 77 | echo 'Loading WorkSheet #',($sheet+1),' using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'<br />'; |
| 78 | /** Tell the Read Filter, the limits on which rows we want to read this iteration **/ |
| 79 | $chunkFilter->setRows($startRow,$chunkSize); |
| 80 | |
| 81 | /** Increment the worksheet index pointer for the Reader **/ |
| 82 | $objReader->setSheetIndex($sheet); |
| 83 | /** Load only the rows that match our filter into a new worksheet in the PHPExcel Object **/ |
| 84 | $objReader->loadIntoExisting($inputFileName,$objPHPExcel); |
| 85 | /** Set the worksheet title (to reference the "sheet" of data that we've loaded) **/ |
| 86 | /** and increment the sheet index as well **/ |
| 87 | $objPHPExcel->getActiveSheet()->setTitle('Country Data #'.(++$sheet)); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | echo '<hr />'; |
| 92 | |
| 93 | echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />'; |
| 94 | $loadedSheetNames = $objPHPExcel->getSheetNames(); |
| 95 | foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { |
| 96 | echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'</b><br />'; |
| 97 | $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); |
| 98 | $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true); |
| 99 | var_dump($sheetData); |
| 100 | echo '<br />'; |
| 101 | } |
| 102 | |
| 103 | ?> |
| 104 | <body> |
| 105 | </html> |