Abstract.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Reader_Abstract
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Reader
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  35. {
  36. /**
  37. * Read data only?
  38. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  39. * or whether it should read both data and formatting
  40. *
  41. * @var boolean
  42. */
  43. protected $_readDataOnly = FALSE;
  44. /**
  45. * Read charts that are defined in the workbook?
  46. * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;
  47. *
  48. * @var boolean
  49. */
  50. protected $_includeCharts = FALSE;
  51. /**
  52. * Restrict which sheets should be loaded?
  53. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  54. *
  55. * @var array of string
  56. */
  57. protected $_loadSheetsOnly = NULL;
  58. /**
  59. * PHPExcel_Reader_IReadFilter instance
  60. *
  61. * @var PHPExcel_Reader_IReadFilter
  62. */
  63. protected $_readFilter = NULL;
  64. protected $_fileHandle = NULL;
  65. /**
  66. * Read data only?
  67. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  68. * If false (the default) it will read data and formatting.
  69. *
  70. * @return boolean
  71. */
  72. public function getReadDataOnly() {
  73. return $this->_readDataOnly;
  74. }
  75. /**
  76. * Set read data only
  77. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  78. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  79. *
  80. * @param boolean $pValue
  81. *
  82. * @return PHPExcel_Reader_IReader
  83. */
  84. public function setReadDataOnly($pValue = FALSE) {
  85. $this->_readDataOnly = $pValue;
  86. return $this;
  87. }
  88. /**
  89. * Read charts in workbook?
  90. * If this is true, then the Reader will include any charts that exist in the workbook.
  91. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  92. * If false (the default) it will ignore any charts defined in the workbook file.
  93. *
  94. * @return boolean
  95. */
  96. public function getIncludeCharts() {
  97. return $this->_includeCharts;
  98. }
  99. /**
  100. * Set read charts in workbook
  101. * Set to true, to advise the Reader to include any charts that exist in the workbook.
  102. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  103. * Set to false (the default) to discard charts.
  104. *
  105. * @param boolean $pValue
  106. *
  107. * @return PHPExcel_Reader_IReader
  108. */
  109. public function setIncludeCharts($pValue = FALSE) {
  110. $this->_includeCharts = (boolean) $pValue;
  111. return $this;
  112. }
  113. /**
  114. * Get which sheets to load
  115. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  116. * indicating that all worksheets in the workbook should be loaded.
  117. *
  118. * @return mixed
  119. */
  120. public function getLoadSheetsOnly()
  121. {
  122. return $this->_loadSheetsOnly;
  123. }
  124. /**
  125. * Set which sheets to load
  126. *
  127. * @param mixed $value
  128. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  129. * If NULL, then it tells the Reader to read all worksheets in the workbook
  130. *
  131. * @return PHPExcel_Reader_IReader
  132. */
  133. public function setLoadSheetsOnly($value = NULL)
  134. {
  135. $this->_loadSheetsOnly = is_array($value) ?
  136. $value : array($value);
  137. return $this;
  138. }
  139. /**
  140. * Set all sheets to load
  141. * Tells the Reader to load all worksheets from the workbook.
  142. *
  143. * @return PHPExcel_Reader_IReader
  144. */
  145. public function setLoadAllSheets()
  146. {
  147. $this->_loadSheetsOnly = NULL;
  148. return $this;
  149. }
  150. /**
  151. * Read filter
  152. *
  153. * @return PHPExcel_Reader_IReadFilter
  154. */
  155. public function getReadFilter() {
  156. return $this->_readFilter;
  157. }
  158. /**
  159. * Set read filter
  160. *
  161. * @param PHPExcel_Reader_IReadFilter $pValue
  162. * @return PHPExcel_Reader_IReader
  163. */
  164. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  165. $this->_readFilter = $pValue;
  166. return $this;
  167. }
  168. /**
  169. * Open file for reading
  170. *
  171. * @param string $pFilename
  172. * @throws PHPExcel_Reader_Exception
  173. * @return resource
  174. */
  175. protected function _openFile($pFilename)
  176. {
  177. // Check if file exists
  178. if (!file_exists($pFilename) || !is_readable($pFilename)) {
  179. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  180. }
  181. // Open file
  182. $this->_fileHandle = fopen($pFilename, 'r');
  183. if ($this->_fileHandle === FALSE) {
  184. throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading.");
  185. }
  186. }
  187. /**
  188. * Can the current PHPExcel_Reader_IReader read the file?
  189. *
  190. * @param string $pFilename
  191. * @return boolean
  192. * @throws PHPExcel_Reader_Exception
  193. */
  194. public function canRead($pFilename)
  195. {
  196. // Check if file exists
  197. try {
  198. $this->_openFile($pFilename);
  199. } catch (Exception $e) {
  200. return FALSE;
  201. }
  202. $readable = $this->_isValidFormat();
  203. fclose ($this->_fileHandle);
  204. return $readable;
  205. }
  206. }