SpreadsheetReader_XLS.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Class for parsing XLS files
  4. *
  5. * @author Martins Pilsetnieks
  6. */
  7. class SpreadsheetReader_XLS implements Iterator, Countable
  8. {
  9. /**
  10. * @var array Options array, pre-populated with the default values.
  11. */
  12. private $Options = array(
  13. );
  14. /**
  15. * @var resource File handle
  16. */
  17. private $Handle = false;
  18. private $Index = 0;
  19. private $Error = false;
  20. /**
  21. * @var array Sheet information
  22. */
  23. private $Sheets = false;
  24. private $SheetIndexes = array();
  25. /**
  26. * @var int Current sheet index
  27. */
  28. private $CurrentSheet = 0;
  29. /**
  30. * @var array Content of the current row
  31. */
  32. private $CurrentRow = array();
  33. /**
  34. * @var int Column count in the sheet
  35. */
  36. private $ColumnCount = 0;
  37. /**
  38. * @var int Row count in the sheet
  39. */
  40. private $RowCount = 0;
  41. /**
  42. * @var array Template to use for empty rows. Retrieved rows are merged
  43. * with this so that empty cells are added, too
  44. */
  45. private $EmptyRow = array();
  46. /**
  47. * @param string Path to file
  48. * @param array Options
  49. */
  50. public function __construct($Filepath, array $Options = null)
  51. {
  52. if (!is_readable($Filepath))
  53. {
  54. throw new Exception('SpreadsheetReader_XLS: File not readable ('.$Filepath.')');
  55. }
  56. if (!class_exists('Spreadsheet_Excel_Reader'))
  57. {
  58. throw new Exception('SpreadsheetReader_XLS: Spreadsheet_Excel_Reader class not available');
  59. }
  60. $this -> Handle = new Spreadsheet_Excel_Reader($Filepath, false, 'UTF-8');
  61. if (function_exists('mb_convert_encoding'))
  62. {
  63. $this -> Handle -> setUTFEncoder('mb');
  64. }
  65. if (empty($this -> Handle -> sheets))
  66. {
  67. $this -> Error = true;
  68. return null;
  69. }
  70. $this -> ChangeSheet(0);
  71. }
  72. public function __destruct()
  73. {
  74. unset($this -> Handle);
  75. }
  76. /**
  77. * Retrieves an array with information about sheets in the current file
  78. *
  79. * @return array List of sheets (key is sheet index, value is name)
  80. */
  81. public function Sheets()
  82. {
  83. if ($this -> Sheets === false)
  84. {
  85. $this -> Sheets = array();
  86. $this -> SheetIndexes = array_keys($this -> Handle -> sheets);
  87. foreach ($this -> SheetIndexes as $SheetIndex)
  88. {
  89. $this -> Sheets[] = $this -> Handle -> boundsheets[$SheetIndex]['name'];
  90. }
  91. }
  92. return $this -> Sheets;
  93. }
  94. /**
  95. * Changes the current sheet in the file to another
  96. *
  97. * @param int Sheet index
  98. *
  99. * @return bool True if sheet was successfully changed, false otherwise.
  100. */
  101. public function ChangeSheet($Index)
  102. {
  103. $Index = (int)$Index;
  104. $Sheets = $this -> Sheets();
  105. if (isset($this -> Sheets[$Index]))
  106. {
  107. $this -> rewind();
  108. $this -> CurrentSheet = $this -> SheetIndexes[$Index];
  109. $this -> ColumnCount = $this -> Handle -> sheets[$this -> CurrentSheet]['numCols'];
  110. $this -> RowCount = $this -> Handle -> sheets[$this -> CurrentSheet]['numRows'];
  111. // For the case when Spreadsheet_Excel_Reader doesn't have the row count set correctly.
  112. if (!$this -> RowCount && count($this -> Handle -> sheets[$this -> CurrentSheet]['cells']))
  113. {
  114. end($this -> Handle -> sheets[$this -> CurrentSheet]['cells']);
  115. $this -> RowCount = (int)key($this -> Handle -> sheets[$this -> CurrentSheet]['cells']);
  116. }
  117. if ($this -> ColumnCount)
  118. {
  119. $this -> EmptyRow = array_fill(1, $this -> ColumnCount, '');
  120. }
  121. else
  122. {
  123. $this -> EmptyRow = array();
  124. }
  125. }
  126. return false;
  127. }
  128. public function __get($Name)
  129. {
  130. switch ($Name)
  131. {
  132. case 'Error':
  133. return $this -> Error;
  134. break;
  135. }
  136. return null;
  137. }
  138. // !Iterator interface methods
  139. /**
  140. * Rewind the Iterator to the first element.
  141. * Similar to the reset() function for arrays in PHP
  142. */
  143. public function rewind()
  144. {
  145. $this -> Index = 0;
  146. }
  147. /**
  148. * Return the current element.
  149. * Similar to the current() function for arrays in PHP
  150. *
  151. * @return mixed current element from the collection
  152. */
  153. public function current()
  154. {
  155. if ($this -> Index == 0)
  156. {
  157. $this -> next();
  158. }
  159. return $this -> CurrentRow;
  160. }
  161. /**
  162. * Move forward to next element.
  163. * Similar to the next() function for arrays in PHP
  164. */
  165. public function next()
  166. {
  167. // Internal counter is advanced here instead of the if statement
  168. // because apparently it's fully possible that an empty row will not be
  169. // present at all
  170. $this -> Index++;
  171. if ($this -> Error)
  172. {
  173. return array();
  174. }
  175. elseif (isset($this -> Handle -> sheets[$this -> CurrentSheet]['cells'][$this -> Index]))
  176. {
  177. $this -> CurrentRow = $this -> Handle -> sheets[$this -> CurrentSheet]['cells'][$this -> Index];
  178. if (!$this -> CurrentRow)
  179. {
  180. return array();
  181. }
  182. $this -> CurrentRow = $this -> CurrentRow + $this -> EmptyRow;
  183. ksort($this -> CurrentRow);
  184. $this -> CurrentRow = array_values($this -> CurrentRow);
  185. return $this -> CurrentRow;
  186. }
  187. else
  188. {
  189. $this -> CurrentRow = $this -> EmptyRow;
  190. return $this -> CurrentRow;
  191. }
  192. }
  193. /**
  194. * Return the identifying key of the current element.
  195. * Similar to the key() function for arrays in PHP
  196. *
  197. * @return mixed either an integer or a string
  198. */
  199. public function key()
  200. {
  201. return $this -> Index;
  202. }
  203. /**
  204. * Check if there is a current element after calls to rewind() or next().
  205. * Used to check if we've iterated to the end of the collection
  206. *
  207. * @return boolean FALSE if there's nothing more to iterate over
  208. */
  209. public function valid()
  210. {
  211. if ($this -> Error)
  212. {
  213. return false;
  214. }
  215. return ($this -> Index <= $this -> RowCount);
  216. }
  217. // !Countable interface method
  218. /**
  219. * Ostensibly should return the count of the contained items but this just returns the number
  220. * of rows read so far. It's not really correct but at least coherent.
  221. */
  222. public function count()
  223. {
  224. if ($this -> Error)
  225. {
  226. return 0;
  227. }
  228. return $this -> RowCount;
  229. }
  230. }
  231. ?>