XLS.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. class PHPExcel_Reader_XLS implements Iterator, Countable {
  3. private $handle = false;
  4. private $index = 0;
  5. private $rowCount = null;
  6. private $currentSheet = 0;
  7. private $currentRow = null;
  8. public $error = false;
  9. public function __construct($filePath) {
  10. if ( ! file_exists($filePath)) {
  11. throw new Exception("Could not open " . $filePath . " for reading! File does not exist.");
  12. }
  13. try {
  14. $this->handle = new PHPExcel_Reader_Excel5($filePath);
  15. return true;
  16. } catch (Exception $e) {
  17. $this->error = true;
  18. return false;
  19. }
  20. }
  21. public function __destruct() {
  22. unset($this->handle);
  23. }
  24. /**
  25. * Retrieves an array with information about sheets in the current file
  26. *
  27. * @return array List of sheets (key is sheet index, value is name)
  28. */
  29. public function Sheets() {
  30. $this->sheetInfo = $this->handle->getWorksheetInfo();
  31. $this->rowCount = $this->sheetInfo['totalRows'];
  32. return $this->sheetInfo;
  33. }
  34. /**
  35. * Changes the current sheet in the file to another
  36. * @param $index int
  37. * @return bool
  38. */
  39. public function ChangeSheet($index) {
  40. return $this->handle->ChangeSheet($index);
  41. }
  42. /**
  43. * Rewind the Iterator to the first element.
  44. */
  45. public function rewind() {
  46. $this->index = 0;
  47. }
  48. /**
  49. * Return the current element.
  50. * @return mixed
  51. */
  52. public function current() {
  53. if ($this->index == 0 && ! isset($this->currentRow)) {
  54. $this->next();
  55. $this->index = 0;
  56. }
  57. return $this->currentRow;
  58. }
  59. /**
  60. * Move forward to next element.
  61. */
  62. public function next() {
  63. $this->currentRow = array();
  64. if( ! $this->sheetInfo) {
  65. $this->Sheets();
  66. }
  67. $this->index++;
  68. $cell = $this->handle->getCell();
  69. for($i = 0; $i < $this->sheetInfo['totalColumns']; $i++) {
  70. $this->currentRow[$i] = isset($cell[$i]) ? $cell[$i] : '';
  71. }
  72. return $this->currentRow;
  73. }
  74. /**
  75. * Return the identifying key of the current element.
  76. * @return mixed
  77. */
  78. public function key() {
  79. return $this->index;
  80. }
  81. /**
  82. * Check if there is a current element after calls to rewind() or next().
  83. * @return boolean
  84. */
  85. public function valid() {
  86. if ($this->error) {
  87. return false;
  88. }
  89. return ($this->index < $this->count());
  90. }
  91. /**
  92. * return the count of the contained items
  93. */
  94. public function count() {
  95. if ($this->error) {
  96. return 0;
  97. }
  98. if( ! isset($this->rowCount)){
  99. $this->Sheets();
  100. }
  101. return $this->rowCount;
  102. }
  103. }