exampleReader14.php 3.4 KB

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