PHPExcelReader.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * PHPExcelReader class
  4. *
  5. * @version 1.0.0
  6. * @author Janson
  7. */
  8. class PHPExcelReader implements SeekableIterator, Countable {
  9. const TYPE_XLSX = 'XLSX';
  10. const TYPE_XLS = 'XLS';
  11. const TYPE_CSV = 'CSV';
  12. const TYPE_ODS = 'ODS';
  13. private $options = array(
  14. 'Delimiter' => '',
  15. 'Enclosure' => '"'
  16. );
  17. private $index = 0;
  18. private $handle = array();
  19. private $type = false;
  20. /**
  21. * @param string Path to file
  22. * @param string Original filename (in case of an uploaded file), used to determine file type, optional
  23. * @param string MIME type from an upload, used to determine file type, optional
  24. */
  25. public function __construct($filePath, $originalFileName = false, $mimeType = false) {
  26. if ( ! is_readable($filePath)) {
  27. throw new Exception('SpreadsheetReader: File (' . $filePath . ') not readable');
  28. }
  29. $defaultTimeZone = @date_default_timezone_get();
  30. if ($defaultTimeZone) {
  31. date_default_timezone_set($defaultTimeZone);
  32. }
  33. // Checking the other parameters for correctness
  34. // This should be a check for string but we're lenient
  35. if ( ! empty($originalFileName) && ! is_scalar($originalFileName)) {
  36. throw new Exception('SpreadsheetReader: Original file (2nd parameter) path is not a string or a scalar value.');
  37. }
  38. if ( ! empty($mimeType) && ! is_scalar($mimeType)) {
  39. throw new Exception('SpreadsheetReader: Mime type (3nd parameter) path is not a string or a scalar value.');
  40. }
  41. // 1. Determine type
  42. if ( ! $originalFileName) {
  43. $originalFileName = $filePath;
  44. }
  45. $Extension = strtolower(pathinfo($originalFileName, PATHINFO_EXTENSION));
  46. if($mimeType) {
  47. switch ($mimeType) {
  48. case 'text/csv':
  49. case 'text/comma-separated-values':
  50. case 'text/plain':
  51. $this->type = self::TYPE_CSV;
  52. break;
  53. case 'application/vnd.ms-excel':
  54. case 'application/msexcel':
  55. case 'application/x-msexcel':
  56. case 'application/x-ms-excel':
  57. case 'application/vnd.ms-excel':
  58. case 'application/x-excel':
  59. case 'application/x-dos_ms_excel':
  60. case 'application/xls':
  61. case 'application/xlt':
  62. case 'application/x-xls':
  63. // Excel does weird stuff
  64. if (in_array($Extension, array('csv', 'tsv', 'txt'))) {
  65. $this->type = self::TYPE_CSV;
  66. }
  67. else {
  68. $this->type = self::TYPE_XLS;
  69. }
  70. break;
  71. case 'application/vnd.oasis.opendocument.spreadsheet':
  72. case 'application/vnd.oasis.opendocument.spreadsheet-template':
  73. $this->type = self::TYPE_ODS;
  74. break;
  75. case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
  76. case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
  77. case 'application/xlsx':
  78. case 'application/xltx':
  79. $this->type = self::TYPE_XLSX;
  80. break;
  81. case 'application/xml':
  82. // Excel 2004 xml format uses this
  83. break;
  84. }
  85. }
  86. if ( ! $this->type) {
  87. switch ($Extension) {
  88. case 'xlsx':
  89. case 'xltx': // XLSX template
  90. case 'xlsm': // Macro-enabled XLSX
  91. case 'xltm': // Macro-enabled XLSX template
  92. $this->type = self::TYPE_XLSX;
  93. break;
  94. case 'xls':
  95. case 'xlt':
  96. $this->type = self::TYPE_XLS;
  97. break;
  98. case 'ods':
  99. case 'odt':
  100. $this->type = self::TYPE_ODS;
  101. break;
  102. default:
  103. $this->type = self::TYPE_CSV;
  104. break;
  105. }
  106. }
  107. // Pre-checking XLS files, in case they are renamed CSV or XLSX files
  108. if ($this->type == self::TYPE_XLS) {
  109. self::Load(self::TYPE_XLS);
  110. $this->handle = new SpreadsheetReader_XLS($filePath);
  111. if ($this->handle->error) {
  112. $this->handle->__destruct();
  113. if (is_resource($Ziphandle = zip_open($filePath))) {
  114. $this->type = self::TYPE_XLSX;
  115. zip_close($Ziphandle);
  116. }
  117. else {
  118. $this->type = self::TYPE_CSV;
  119. }
  120. }
  121. }
  122. // 2. Create handle
  123. switch ($this->type) {
  124. case self::TYPE_XLSX:
  125. self::Load(self::TYPE_XLSX);
  126. $this->handle = new SpreadsheetReader_XLSX($filePath);
  127. break;
  128. case self::TYPE_CSV:
  129. self::Load(self::TYPE_CSV);
  130. $this->handle = new SpreadsheetReader_CSV($filePath, $this->options, 'GBK');
  131. break;
  132. case self::TYPE_XLS:
  133. // Everything already happens above
  134. break;
  135. case self::TYPE_ODS:
  136. self::Load(self::TYPE_ODS);
  137. $this->handle = new SpreadsheetReader_ODS($filePath, $this->options);
  138. break;
  139. }
  140. }
  141. /**
  142. * get the type of file
  143. * @return string
  144. */
  145. public function getType() {
  146. return $this->type;
  147. }
  148. /**
  149. * Gets information about separate sheets in the given file
  150. *
  151. * @return array Associative array where key is sheet index and value is sheet name
  152. */
  153. public function Sheets() {
  154. return $this->handle->Sheets();
  155. }
  156. /**
  157. * Changes the current sheet to another from the file.
  158. * Note that changing the sheet will rewind the file to the beginning, even if
  159. * the current sheet index is provided.
  160. *
  161. * @param int Sheet index
  162. *
  163. * @return bool True if sheet could be changed to the specified one,
  164. * false if not (for example, if incorrect index was provided.
  165. */
  166. public function ChangeSheet($index) {
  167. return $this->handle->ChangeSheet($index);
  168. }
  169. /**
  170. * Autoloads the required class for the particular spreadsheet type
  171. *
  172. * @param TYPE_* Spreadsheet type, one of TYPE_* constants of this class
  173. */
  174. private static function Load($type) {
  175. if ( ! in_array($type, array(self::TYPE_XLSX, self::TYPE_XLS, self::TYPE_CSV, self::TYPE_ODS))) {
  176. throw new Exception('SpreadsheetReader: Invalid type (' . $type . ')');
  177. }
  178. // 2nd parameter is to prevent autoloading for the class.
  179. // If autoload works, the require line is unnecessary, if it doesn't, it ends badly.
  180. if ( ! class_exists('SpreadsheetReader_' . $type, false)) {
  181. require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'SpreadsheetReader' . DIRECTORY_SEPARATOR . 'SpreadsheetReader_' . $type . '.php');
  182. }
  183. }
  184. // !Iterator interface methods
  185. /**
  186. * Rewind the Iterator to the first element.
  187. * Similar to the reset() function for arrays in PHP
  188. */
  189. public function rewind() {
  190. $this->index = 0;
  191. if ($this->handle) {
  192. $this->handle->rewind();
  193. }
  194. }
  195. /**
  196. * Return the current element.
  197. * Similar to the current() function for arrays in PHP
  198. *
  199. * @return mixed current element from the collection
  200. */
  201. public function current() {
  202. if ($this->handle) {
  203. return $this->handle->current();
  204. }
  205. return null;
  206. }
  207. /**
  208. * Move forward to next element.
  209. * Similar to the next() function for arrays in PHP
  210. */
  211. public function next() {
  212. if ($this->handle) {
  213. $this->index++;
  214. return $this->handle->next();
  215. }
  216. return null;
  217. }
  218. /**
  219. * Return the identifying key of the current element.
  220. * Similar to the key() function for arrays in PHP
  221. *
  222. * @return mixed either an integer or a string
  223. */
  224. public function key() {
  225. if ($this->handle) {
  226. return $this->handle->key();
  227. }
  228. return null;
  229. }
  230. /**
  231. * Check if there is a current element after calls to rewind() or next().
  232. * Used to check if we've iterated to the end of the collection
  233. *
  234. * @return boolean FALSE if there's nothing more to iterate over
  235. */
  236. public function valid() {
  237. if ($this->handle) {
  238. return $this->handle->valid();
  239. }
  240. return false;
  241. }
  242. /**
  243. * total of file number
  244. * return int
  245. */
  246. public function count() {
  247. if ($this->handle) {
  248. return $this->handle->count();
  249. }
  250. return 0;
  251. }
  252. /**
  253. * Method for SeekableIterator interface. Takes a posiiton and traverses the file to that position
  254. * The value can be retrieved with a `current()` call afterwards.
  255. *
  256. * @param int position in file
  257. */
  258. public function seek($position) {
  259. if ( ! $this->handle) {
  260. throw new OutOfBoundsException('SpreadsheetReader: No file opened');
  261. }
  262. $Currentindex = $this->handle->key();
  263. if ($Currentindex != $position) {
  264. if ($position < $Currentindex || is_null($Currentindex) || $position == 0) {
  265. $this->rewind();
  266. }
  267. while ($this->handle->valid() && ($position > $this->handle->key())) {
  268. $this->handle->next();
  269. }
  270. if ( ! $this->handle->valid()) {
  271. throw new OutOfBoundsException('SpreadsheetError: position ' . $position . ' not found');
  272. }
  273. }
  274. return null;
  275. }
  276. }