PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.6.2/Classes/PHPExcel.php

#
PHP | 382 lines | 149 code | 45 blank | 188 comment | 23 complexity | 460109f64fd55187675dedd318fa127b 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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.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. /** PHPExcel_NamedRange */
  38. require_once 'PHPExcel/NamedRange.php';
  39. /**
  40. * PHPExcel
  41. *
  42. * @category PHPExcel
  43. * @package PHPExcel
  44. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  45. */
  46. class PHPExcel
  47. {
  48. /**
  49. * Document properties
  50. *
  51. * @var PHPExcel_DocumentProperties
  52. */
  53. private $_properties;
  54. /**
  55. * Document security
  56. *
  57. * @var PHPExcel_DocumentSecurity
  58. */
  59. private $_security;
  60. /**
  61. * Collection of Worksheet objects
  62. *
  63. * @var PHPExcel_Worksheet[]
  64. */
  65. private $_workSheetCollection = array();
  66. /**
  67. * Active sheet index
  68. *
  69. * @var int
  70. */
  71. private $_activeSheetIndex = 0;
  72. /**
  73. * Named ranges
  74. *
  75. * @var PHPExcel_NamedRange[]
  76. */
  77. private $_namedRanges = array();
  78. /**
  79. * Create a new PHPExcel with one Worksheet
  80. */
  81. public function __construct()
  82. {
  83. // Initialise worksheet collection and add one worksheet
  84. $this->_workSheetCollection = array();
  85. $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
  86. $this->_activeSheetIndex = 0;
  87. // Create document properties
  88. $this->_properties = new PHPExcel_DocumentProperties();
  89. // Create document security
  90. $this->_security = new PHPExcel_DocumentSecurity();
  91. // Set named ranges
  92. $this->_namedRanges = array();
  93. }
  94. /**
  95. * Get properties
  96. *
  97. * @return PHPExcel_DocumentProperties
  98. */
  99. public function getProperties()
  100. {
  101. return $this->_properties;
  102. }
  103. /**
  104. * Set properties
  105. *
  106. * @param PHPExcel_DocumentProperties $pValue
  107. */
  108. public function setProperties(PHPExcel_DocumentProperties $pValue)
  109. {
  110. $this->_properties = $pValue;
  111. }
  112. /**
  113. * Get security
  114. *
  115. * @return PHPExcel_DocumentSecurity
  116. */
  117. public function getSecurity()
  118. {
  119. return $this->_security;
  120. }
  121. /**
  122. * Set security
  123. *
  124. * @param PHPExcel_DocumentSecurity $pValue
  125. */
  126. public function setSecurity(PHPExcel_DocumentSecurity $pValue)
  127. {
  128. $this->_security = $pValue;
  129. }
  130. /**
  131. * Get active sheet
  132. *
  133. * @return PHPExcel_Worksheet
  134. */
  135. public function getActiveSheet()
  136. {
  137. return $this->_workSheetCollection[$this->_activeSheetIndex];
  138. }
  139. /**
  140. * Create sheet and add it to this workbook
  141. *
  142. * @return PHPExcel_Worksheet
  143. */
  144. public function createSheet()
  145. {
  146. $newSheet = new PHPExcel_Worksheet($this);
  147. $this->addSheet($newSheet);
  148. return $newSheet;
  149. }
  150. /**
  151. * Add sheet
  152. *
  153. * @param PHPExcel_Worksheet $pSheet
  154. * @throws Exception
  155. */
  156. public function addSheet(PHPExcel_Worksheet $pSheet = null)
  157. {
  158. $this->_workSheetCollection[] = $pSheet;
  159. }
  160. /**
  161. * Remove sheet by index
  162. *
  163. * @param int $pIndex Active sheet index
  164. * @throws Exception
  165. */
  166. public function removeSheetByIndex($pIndex = 0)
  167. {
  168. if ($pIndex > count($this->_workSheetCollection) - 1) {
  169. throw new Exception("Sheet index is out of bounds.");
  170. } else {
  171. array_splice($this->_workSheetCollection, $pIndex, 1);
  172. }
  173. }
  174. /**
  175. * Get sheet by index
  176. *
  177. * @param int $pIndex Sheet index
  178. * @return PHPExcel_Worksheet
  179. * @throws Exception
  180. */
  181. public function getSheet($pIndex = 0)
  182. {
  183. if ($pIndex > count($this->_workSheetCollection) - 1) {
  184. throw new Exception("Sheet index is out of bounds.");
  185. } else {
  186. return $this->_workSheetCollection[$pIndex];
  187. }
  188. }
  189. /**
  190. * Get all sheets
  191. *
  192. * @return PHPExcel_Worksheet[]
  193. */
  194. public function getAllSheets()
  195. {
  196. return $this->_workSheetCollection;
  197. }
  198. /**
  199. * Get sheet by name
  200. *
  201. * @param string $pName Sheet name
  202. * @return PHPExcel_Worksheet
  203. * @throws Exception
  204. */
  205. public function getSheetByName($pName = '')
  206. {
  207. for ($i = 0; $i < count($this->_workSheetCollection); $i++) {
  208. if ($this->_workSheetCollection[$i]->getTitle() == $pName) {
  209. return $this->_workSheetCollection[$i];
  210. }
  211. }
  212. return null;
  213. }
  214. /**
  215. * Get index for sheet
  216. *
  217. * @param PHPExcel_Worksheet $pSheet
  218. * @return Sheet index
  219. * @throws Exception
  220. */
  221. public function getIndex(PHPExcel_Worksheet $pSheet)
  222. {
  223. foreach ($this->_workSheetCollection as $key => $value) {
  224. if ($value->getHashCode() == $pSheet->getHashCode()) {
  225. return $key;
  226. }
  227. }
  228. }
  229. /**
  230. * Get sheet count
  231. *
  232. * @return int
  233. */
  234. public function getSheetCount()
  235. {
  236. return count($this->_workSheetCollection);
  237. }
  238. /**
  239. * Get active sheet index
  240. *
  241. * @return int Active sheet index
  242. */
  243. public function getActiveSheetIndex()
  244. {
  245. return $this->_activeSheetIndex;
  246. }
  247. /**
  248. * Set active sheet index
  249. *
  250. * @param int $pIndex Active sheet index
  251. * @throws Exception
  252. */
  253. public function setActiveSheetIndex($pIndex = 0)
  254. {
  255. if ($pIndex > count($this->_workSheetCollection) - 1) {
  256. throw new Exception("Active sheet index is out of bounds.");
  257. } else {
  258. $this->_activeSheetIndex = $pIndex;
  259. }
  260. }
  261. /**
  262. * Get sheet names
  263. *
  264. * @return string[]
  265. */
  266. public function getSheetNames()
  267. {
  268. $returnValue = array();
  269. for ($i = 0; $i < $this->getSheetCount(); $i++) {
  270. array_push($returnValue, $this->getSheet($i)->getTitle());
  271. }
  272. return $returnValue;
  273. }
  274. /**
  275. * Add external sheet
  276. *
  277. * @param PHPExcel_Worksheet $pSheet External sheet to add
  278. * @throws Exception
  279. */
  280. public function addExternalSheet(PHPExcel_Worksheet $pSheet) {
  281. if (!is_null($this->getSheetByName($pSheet->getTitle()))) {
  282. throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  283. }
  284. $pSheet->rebindParent($this);
  285. $this->addSheet($pSheet);
  286. }
  287. /**
  288. * Get named ranges
  289. *
  290. * @return PHPExcel_NamedRange[]
  291. */
  292. public function getNamedRanges() {
  293. return $this->_namedRanges;
  294. }
  295. /**
  296. * Add named range
  297. *
  298. * @param PHPExcel_NamedRange $namedRange
  299. */
  300. public function addNamedRange(PHPExcel_NamedRange $namedRange) {
  301. $this->_namedRanges[$namedRange->getName()] = $namedRange;
  302. }
  303. /**
  304. * Get named range
  305. *
  306. * @param string $namedRange
  307. */
  308. public function getNamedRange($namedRange) {
  309. if ($namedRange != '' && !is_null($namedRange) && @isset($this->_namedRanges[$namedRange])) {
  310. return $this->_namedRanges[$namedRange];
  311. }
  312. return null;
  313. }
  314. /**
  315. * Remove named range
  316. *
  317. * @param string $namedRange
  318. */
  319. public function removeNamedRange($namedRange) {
  320. if ($namedRange != '' && !is_null($namedRange) && @isset($this->_namedRanges[$namedRange])) {
  321. unset($this->_namedRanges[$namedRange]);
  322. }
  323. }
  324. /**
  325. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  326. */
  327. public function __clone() {
  328. $vars = get_object_vars($this);
  329. foreach ($vars as $key => $value) {
  330. if (is_object($value)) {
  331. $this->$key = clone $value;
  332. } else {
  333. $this->$key = $value;
  334. }
  335. }
  336. }
  337. }