No Description

exampleReader12.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 #12</title>
  10. </head>
  11. <body>
  12. <h1>PHPExcel Reader Example #12</h1>
  13. <h2>Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 2)</h2>
  14. <?php
  15. /** Set Include path to point at the PHPExcel Classes folder **/
  16. set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
  17. /** Include 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. /** Set the list of rows that we want to read */
  31. public function setRows($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 are configured in $this->_startRow and $this->_endRow
  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 to read for each "chunk" **/
  48. $chunkSize = 20;
  49. /** Create a new Instance of our Read Filter **/
  50. $chunkFilter = new chunkReadFilter();
  51. /** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
  52. $objReader->setReadFilter($chunkFilter);
  53. /** Loop to read our worksheet in "chunk size" blocks **/
  54. for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
  55. echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'<br />';
  56. /** Tell the Read Filter, the limits on which rows we want to read this iteration **/
  57. $chunkFilter->setRows($startRow,$chunkSize);
  58. /** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
  59. $objPHPExcel = $objReader->load($inputFileName);
  60. // Do some processing here
  61. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  62. var_dump($sheetData);
  63. echo '<br /><br />';
  64. }
  65. ?>
  66. <body>
  67. </html>