exampleReader09.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 #09</title>
  10. </head>
  11. <body>
  12. <h1>PHPExcel Reader Example #09</h1>
  13. <h2>Simple File Reader Using a Read Filter</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/example1.xls';
  25. $sheetname = 'Data Sheet #3';
  26. class MyReadFilter implements PHPExcel_Reader_IReadFilter
  27. {
  28. public function readCell($column, $row, $worksheetName = '') {
  29. // Read rows 1 to 7 and columns A to E only
  30. if ($row >= 1 && $row <= 7) {
  31. if (in_array($column,range('A','E'))) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. }
  38. $filterSubset = new MyReadFilter();
  39. echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
  40. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  41. echo 'Loading Sheet "',$sheetname,'" only<br />';
  42. $objReader->setLoadSheetsOnly($sheetname);
  43. echo 'Loading Sheet using filter<br />';
  44. $objReader->setReadFilter($filterSubset);
  45. $objPHPExcel = $objReader->load($inputFileName);
  46. echo '<hr />';
  47. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  48. var_dump($sheetData);
  49. ?>
  50. <body>
  51. </html>