XLS.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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->rewind();
  55. $this->next();
  56. $this->Index--;
  57. }
  58. return $this->currentRow;
  59. }
  60. /**
  61. * Move forward to next element.
  62. */
  63. public function next() {
  64. $this->currentRow = array();
  65. if( ! $this->sheetInfo) {
  66. $this->Sheets();
  67. }
  68. $this->index++;
  69. $cell = $this->handle->getCell();
  70. for($i = 0; $i < $this->sheetInfo['totalColumns']; $i++) {
  71. $this->currentRow[$i] = isset($cell[$i]) ? $cell[$i] : '';
  72. }
  73. return $this->currentRow;
  74. }
  75. /**
  76. * Return the identifying key of the current element.
  77. * @return mixed
  78. */
  79. public function key() {
  80. return $this->index;
  81. }
  82. /**
  83. * Check if there is a current element after calls to rewind() or next().
  84. * @return boolean
  85. */
  86. public function valid() {
  87. if ($this->error) {
  88. return false;
  89. }
  90. return ($this->index <= $this->count());
  91. }
  92. /**
  93. * return the count of the contained items
  94. */
  95. public function count() {
  96. if ($this->error) {
  97. return 0;
  98. }
  99. if( ! isset($this->rowCount)){
  100. $this->Sheets();
  101. }
  102. return $this->rowCount;
  103. }
  104. }