PageRenderTime 48ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.4.5/Classes/PHPExcel.php

#
PHP | 268 lines | 101 code | 31 blank | 136 comment | 9 complexity | 7f63a55a43edf5617174bde8e6aaf4a2 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 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
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_Cell */
  28. require_once 'PHPExcel/Cell.php';
  29. /** PHPExcel_DocumentProperties */
  30. require_once 'PHPExcel/DocumentProperties.php';
  31. /** PHPExcel_DocumentSecurity */
  32. require_once 'PHPExcel/DocumentSecurity.php';
  33. /** PHPExcel_Worksheet */
  34. require_once 'PHPExcel/Worksheet.php';
  35. /** PHPExcel_Shared_ZipStreamWrapper */
  36. require_once 'PHPExcel/Shared/ZipStreamWrapper.php';
  37. /**
  38. * PHPExcel
  39. *
  40. * @category PHPExcel
  41. * @package PHPExcel
  42. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  43. */
  44. class PHPExcel
  45. {
  46. /**
  47. * Document properties
  48. *
  49. * @var PHPExcel_DocumentProperties
  50. */
  51. private $_properties;
  52. /**
  53. * Document security
  54. *
  55. * @var PHPExcel_DocumentSecurity
  56. */
  57. private $_security;
  58. /**
  59. * Collection of Worksheet objects
  60. *
  61. * @var PHPExcel_Worksheet[]
  62. */
  63. private $_workSheetCollection = array();
  64. /**
  65. * Active sheet index
  66. *
  67. * @var int
  68. */
  69. private $_activeSheetIndex = 0;
  70. /**
  71. * Create a new PHPExcel with one Worksheet
  72. */
  73. public function __construct()
  74. {
  75. // Initialise worksheet collection and add one worksheet
  76. $this->_workSheetCollection = array();
  77. $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
  78. $this->_activeSheetIndex = 0;
  79. // Create document properties
  80. $this->_properties = new PHPExcel_DocumentProperties();
  81. // Create document security
  82. $this->_security = new PHPExcel_DocumentSecurity();
  83. }
  84. /**
  85. * Get properties
  86. *
  87. * @return PHPExcel_DocumentProperties
  88. */
  89. public function getProperties()
  90. {
  91. return $this->_properties;
  92. }
  93. /**
  94. * Set properties
  95. *
  96. * @param PHPExcel_DocumentProperties $pValue
  97. */
  98. public function setProperties(PHPExcel_DocumentProperties $pValue)
  99. {
  100. $this->_properties = $pValue;
  101. }
  102. /**
  103. * Get security
  104. *
  105. * @return PHPExcel_DocumentSecurity
  106. */
  107. public function getSecurity()
  108. {
  109. return $this->_security;
  110. }
  111. /**
  112. * Set security
  113. *
  114. * @param PHPExcel_DocumentSecurity $pValue
  115. */
  116. public function setSecurity(PHPExcel_DocumentSecurity $pValue)
  117. {
  118. $this->_security = $pValue;
  119. }
  120. /**
  121. * Get active sheet
  122. *
  123. * @return PHPExcel_Worksheet
  124. */
  125. public function getActiveSheet()
  126. {
  127. return $this->_workSheetCollection[$this->_activeSheetIndex];
  128. }
  129. /**
  130. * Create sheet and add it to this workbook
  131. *
  132. * @return PHPExcel_Worksheet
  133. */
  134. public function createSheet()
  135. {
  136. $newSheet = new PHPExcel_Worksheet($this);
  137. $this->addSheet($newSheet);
  138. return $newSheet;
  139. }
  140. /**
  141. * Add sheet
  142. *
  143. * @param PHPExcel_Worksheet $pSheet
  144. * @throws Exception
  145. */
  146. public function addSheet(PHPExcel_Worksheet $pSheet = null)
  147. {
  148. $this->_workSheetCollection[] = $pSheet;
  149. }
  150. /**
  151. * Remove sheet by index
  152. *
  153. * @param int $pIndex Active sheet index
  154. * @throws Exception
  155. */
  156. public function removeSheetByIndex($pIndex = 0)
  157. {
  158. if ($pIndex > count($this->_workSheetCollection) - 1) {
  159. throw new Exception("Sheet index is out of bounds.");
  160. } else {
  161. array_splice($this->_workSheetCollection, $pIndex, 1);
  162. }
  163. }
  164. /**
  165. * Get sheet by index
  166. *
  167. * @param int $pIndex Active sheet index
  168. * @return PHPExcel_Worksheet
  169. * @throws Exception
  170. */
  171. public function getSheet($pIndex = 0)
  172. {
  173. if ($pIndex > count($this->_workSheetCollection) - 1) {
  174. throw new Exception("Sheet index is out of bounds.");
  175. } else {
  176. return $this->_workSheetCollection[$pIndex];
  177. }
  178. }
  179. /**
  180. * Get sheet count
  181. *
  182. * @return int
  183. */
  184. public function getSheetCount()
  185. {
  186. return count($this->_workSheetCollection);
  187. }
  188. /**
  189. * Get active sheet index
  190. *
  191. * @return int Active sheet index
  192. */
  193. public function getActiveSheetIndex()
  194. {
  195. return $this->_activeSheetIndex;
  196. }
  197. /**
  198. * Set active sheet index
  199. *
  200. * @param int $pIndex Active sheet index
  201. * @throws Exception
  202. */
  203. public function setActiveSheetIndex($pIndex = 0)
  204. {
  205. if ($pIndex > count($this->_workSheetCollection) - 1) {
  206. throw new Exception("Active sheet index is out of bounds.");
  207. } else {
  208. $this->_activeSheetIndex = $pIndex;
  209. }
  210. }
  211. /**
  212. * Get sheet names
  213. *
  214. * @return string[]
  215. */
  216. public function getSheetNames()
  217. {
  218. $returnValue = array();
  219. for ($i = 0; $i < $this->getSheetCount(); $i++) {
  220. array_push($returnValue, $this->getSheet($i)->getTitle());
  221. }
  222. return $returnValue;
  223. }
  224. /**
  225. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  226. */
  227. public function __clone() {
  228. $vars = get_object_vars($this);
  229. foreach ($vars as $key => $value) {
  230. if (is_object($value)) {
  231. $this->$key = clone $value;
  232. } else {
  233. $this->$key = $value;
  234. }
  235. }
  236. }
  237. }