exampleReader11.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. error_reporting(E_ALL);
  3. set_time_limit(0);
  4. date_default_timezone_set('Europe/London');
  5. ?>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  9. <title>PHPExcel Reader Example #11</title>
  10. </head>
  11. <body>
  12. <h1>PHPExcel Reader Example #11</h1>
  13. <h2>Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 1)</h2>
  14. <?php
  15. /** Include path **/
  16. set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
  17. /** PHPExcel_IOFactory */
  18. include 'PHPExcel/IOFactory.php';
  19. $inputFileType = 'Excel5';
  20. // $inputFileType = 'Excel2007';
  21. // $inputFileType = 'Excel2003XML';
  22. // $inputFileType = 'OOCalc';
  23. // $inputFileType = 'Gnumeric';
  24. $inputFileName = './sampleData/example2.xls';
  25. /** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
  26. class chunkReadFilter implements PHPExcel_Reader_IReadFilter
  27. {
  28. private $_startRow = 0;
  29. private $_endRow = 0;
  30. /** We expect a list of the rows that we want to read to be passed into the constructor */
  31. public function __construct($startRow, $chunkSize) {
  32. $this->_startRow = $startRow;
  33. $this->_endRow = $startRow + $chunkSize;
  34. }
  35. public function readCell($column, $row, $worksheetName = '') {
  36. // Only read the heading row, and the rows that were configured in the constructor
  37. if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. }
  43. echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
  44. /** Create a new Reader of the type defined in $inputFileType **/
  45. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  46. echo '<hr />';
  47. /** Define how many rows we want for each "chunk" **/
  48. $chunkSize = 20;
  49. /** Loop to read our worksheet in "chunk size" blocks **/
  50. for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
  51. echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'<br />';
  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. $objReader->setReadFilter($chunkFilter);
  56. /** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
  57. $objPHPExcel = $objReader->load($inputFileName);
  58. // Do some processing here
  59. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  60. var_dump($sheetData);
  61. echo '<br /><br />';
  62. }
  63. ?>
  64. <body>
  65. </html>