exampleReader10.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 #10</title>
  10. </head>
  11. <body>
  12. <h1>PHPExcel Reader Example #10</h1>
  13. <h2>Simple File Reader Using a Configurable 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. private $_startRow = 0;
  29. private $_endRow = 0;
  30. private $_columns = array();
  31. public function __construct($startRow, $endRow, $columns) {
  32. $this->_startRow = $startRow;
  33. $this->_endRow = $endRow;
  34. $this->_columns = $columns;
  35. }
  36. public function readCell($column, $row, $worksheetName = '') {
  37. if ($row >= $this->_startRow && $row <= $this->_endRow) {
  38. if (in_array($column,$this->_columns)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. }
  45. $filterSubset = new MyReadFilter(9,15,range('G','K'));
  46. echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
  47. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  48. echo 'Loading Sheet "',$sheetname,'" only<br />';
  49. $objReader->setLoadSheetsOnly($sheetname);
  50. echo 'Loading Sheet using configurable filter<br />';
  51. $objReader->setReadFilter($filterSubset);
  52. $objPHPExcel = $objReader->load($inputFileName);
  53. echo '<hr />';
  54. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  55. var_dump($sheetData);
  56. ?>
  57. <body>
  58. </html>