SpreadsheetReader_XLS.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class SpreadsheetReader_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. self::classLoad();
  11. $this->handle = new Spreadsheet_Excel_Reader($filePath);
  12. if ($this->handle->error) {
  13. $this->error = true;
  14. return false;
  15. }
  16. }
  17. public function __destruct() {
  18. unset($this->handle);
  19. }
  20. /**
  21. * Retrieves an array with information about sheets in the current file
  22. *
  23. * @return array List of sheets (key is sheet index, value is name)
  24. */
  25. public function Sheets() {
  26. $this->sheetInfo = $this->handle->getWorksheetInfo();
  27. $this->rowCount = $this->sheetInfo['totalRows'];
  28. return $this->sheetInfo;
  29. }
  30. /**
  31. * Changes the current sheet in the file to another
  32. * @param $index int
  33. * @return bool
  34. */
  35. public function ChangeSheet($index) {
  36. return $this->handle->ChangeSheet($index);
  37. }
  38. /**
  39. * Rewind the Iterator to the first element.
  40. */
  41. public function rewind() {
  42. $this->index = 0;
  43. }
  44. /**
  45. * Return the current element.
  46. * @return mixed
  47. */
  48. public function current() {
  49. if ($this->index == 0 && is_null($this->currentRow)) {
  50. $this->next();
  51. $this->index = 0;
  52. }
  53. return $this->currentRow;
  54. }
  55. /**
  56. * Move forward to next element.
  57. */
  58. public function next() {
  59. $this->currentRow = array();
  60. if( ! $this->sheetInfo) {
  61. $this->Sheets();
  62. }
  63. $this->index++;
  64. $cell = $this->handle->getCell();
  65. if(count($cell) < $this->sheetInfo['totalColumns']){
  66. for($i = 0; $i < $this->sheetInfo['totalColumns']; $i++) {
  67. $this->currentRow[$i] = isset($cell[$i]) ? $cell[$i] : '';
  68. }
  69. }
  70. else{
  71. $this->currentRow = $cell;
  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(is_null($this->rowCount)){
  100. $this->Sheets();
  101. }
  102. return $this->rowCount;
  103. }
  104. private static function classLoad() {
  105. if ( ! class_exists('Spreadsheet_Excel_Reader', false)) {
  106. require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'SpreadsheetReader.php';
  107. }
  108. }
  109. }