PHPExcel.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
  30. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  31. }
  32. /**
  33. * PHPExcel
  34. *
  35. * @category PHPExcel
  36. * @package PHPExcel
  37. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  38. */
  39. class PHPExcel
  40. {
  41. /**
  42. * Document properties
  43. *
  44. * @var PHPExcel_DocumentProperties
  45. */
  46. private $_properties;
  47. /**
  48. * Document security
  49. *
  50. * @var PHPExcel_DocumentSecurity
  51. */
  52. private $_security;
  53. /**
  54. * Collection of Worksheet objects
  55. *
  56. * @var PHPExcel_Worksheet[]
  57. */
  58. private $_workSheetCollection = array();
  59. /**
  60. * Active sheet index
  61. *
  62. * @var int
  63. */
  64. private $_activeSheetIndex = 0;
  65. /**
  66. * Named ranges
  67. *
  68. * @var PHPExcel_NamedRange[]
  69. */
  70. private $_namedRanges = array();
  71. /**
  72. * CellXf supervisor
  73. *
  74. * @var PHPExcel_Style
  75. */
  76. private $_cellXfSupervisor;
  77. /**
  78. * CellXf collection
  79. *
  80. * @var PHPExcel_Style[]
  81. */
  82. private $_cellXfCollection = array();
  83. /**
  84. * CellStyleXf collection
  85. *
  86. * @var PHPExcel_Style[]
  87. */
  88. private $_cellStyleXfCollection = array();
  89. /**
  90. * Create a new PHPExcel with one Worksheet
  91. */
  92. public function __construct()
  93. {
  94. // Initialise worksheet collection and add one worksheet
  95. $this->_workSheetCollection = array();
  96. $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
  97. $this->_activeSheetIndex = 0;
  98. // Create document properties
  99. $this->_properties = new PHPExcel_DocumentProperties();
  100. // Create document security
  101. $this->_security = new PHPExcel_DocumentSecurity();
  102. // Set named ranges
  103. $this->_namedRanges = array();
  104. // Create the cellXf supervisor
  105. $this->_cellXfSupervisor = new PHPExcel_Style(true);
  106. $this->_cellXfSupervisor->bindParent($this);
  107. // Create the default style
  108. $this->addCellXf(new PHPExcel_Style);
  109. $this->addCellStyleXf(new PHPExcel_Style);
  110. }
  111. public function disconnectWorksheets() {
  112. foreach($this->_workSheetCollection as $k => &$worksheet) {
  113. $worksheet->disconnectCells();
  114. $this->_workSheetCollection[$k] = null;
  115. }
  116. unset($worksheet);
  117. $this->_workSheetCollection = array();
  118. }
  119. /**
  120. * Get properties
  121. *
  122. * @return PHPExcel_DocumentProperties
  123. */
  124. public function getProperties()
  125. {
  126. return $this->_properties;
  127. }
  128. /**
  129. * Set properties
  130. *
  131. * @param PHPExcel_DocumentProperties $pValue
  132. */
  133. public function setProperties(PHPExcel_DocumentProperties $pValue)
  134. {
  135. $this->_properties = $pValue;
  136. }
  137. /**
  138. * Get security
  139. *
  140. * @return PHPExcel_DocumentSecurity
  141. */
  142. public function getSecurity()
  143. {
  144. return $this->_security;
  145. }
  146. /**
  147. * Set security
  148. *
  149. * @param PHPExcel_DocumentSecurity $pValue
  150. */
  151. public function setSecurity(PHPExcel_DocumentSecurity $pValue)
  152. {
  153. $this->_security = $pValue;
  154. }
  155. /**
  156. * Get active sheet
  157. *
  158. * @return PHPExcel_Worksheet
  159. */
  160. public function getActiveSheet()
  161. {
  162. return $this->_workSheetCollection[$this->_activeSheetIndex];
  163. }
  164. /**
  165. * Create sheet and add it to this workbook
  166. *
  167. * @return PHPExcel_Worksheet
  168. */
  169. public function createSheet($iSheetIndex = null)
  170. {
  171. $newSheet = new PHPExcel_Worksheet($this);
  172. $this->addSheet($newSheet, $iSheetIndex);
  173. return $newSheet;
  174. }
  175. /**
  176. * Add sheet
  177. *
  178. * @param PHPExcel_Worksheet $pSheet
  179. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  180. * @return PHPExcel_Worksheet
  181. * @throws Exception
  182. */
  183. public function addSheet(PHPExcel_Worksheet $pSheet = null, $iSheetIndex = null)
  184. {
  185. if(is_null($iSheetIndex))
  186. {
  187. $this->_workSheetCollection[] = $pSheet;
  188. }
  189. else
  190. {
  191. // Insert the sheet at the requested index
  192. array_splice(
  193. $this->_workSheetCollection,
  194. $iSheetIndex,
  195. 0,
  196. array($pSheet)
  197. );
  198. // Adjust active sheet index if necessary
  199. if ($this->_activeSheetIndex >= $iSheetIndex) {
  200. ++$this->_activeSheetIndex;
  201. }
  202. }
  203. return $pSheet;
  204. }
  205. /**
  206. * Remove sheet by index
  207. *
  208. * @param int $pIndex Active sheet index
  209. * @throws Exception
  210. */
  211. public function removeSheetByIndex($pIndex = 0)
  212. {
  213. if ($pIndex > count($this->_workSheetCollection) - 1) {
  214. throw new Exception("Sheet index is out of bounds.");
  215. } else {
  216. array_splice($this->_workSheetCollection, $pIndex, 1);
  217. }
  218. }
  219. /**
  220. * Get sheet by index
  221. *
  222. * @param int $pIndex Sheet index
  223. * @return PHPExcel_Worksheet
  224. * @throws Exception
  225. */
  226. public function getSheet($pIndex = 0)
  227. {
  228. if ($pIndex > count($this->_workSheetCollection) - 1) {
  229. throw new Exception("Sheet index is out of bounds.");
  230. } else {
  231. return $this->_workSheetCollection[$pIndex];
  232. }
  233. }
  234. /**
  235. * Get all sheets
  236. *
  237. * @return PHPExcel_Worksheet[]
  238. */
  239. public function getAllSheets()
  240. {
  241. return $this->_workSheetCollection;
  242. }
  243. /**
  244. * Get sheet by name
  245. *
  246. * @param string $pName Sheet name
  247. * @return PHPExcel_Worksheet
  248. * @throws Exception
  249. */
  250. public function getSheetByName($pName = '')
  251. {
  252. $worksheetCount = count($this->_workSheetCollection);
  253. for ($i = 0; $i < $worksheetCount; ++$i) {
  254. if ($this->_workSheetCollection[$i]->getTitle() == $pName) {
  255. return $this->_workSheetCollection[$i];
  256. }
  257. }
  258. return null;
  259. }
  260. /**
  261. * Get index for sheet
  262. *
  263. * @param PHPExcel_Worksheet $pSheet
  264. * @return Sheet index
  265. * @throws Exception
  266. */
  267. public function getIndex(PHPExcel_Worksheet $pSheet)
  268. {
  269. foreach ($this->_workSheetCollection as $key => $value) {
  270. if ($value->getHashCode() == $pSheet->getHashCode()) {
  271. return $key;
  272. }
  273. }
  274. }
  275. /**
  276. * Set index for sheet by sheet name.
  277. *
  278. * @param string $sheetName Sheet name to modify index for
  279. * @param int $newIndex New index for the sheet
  280. * @return New sheet index
  281. * @throws Exception
  282. */
  283. public function setIndexByName($sheetName, $newIndex)
  284. {
  285. $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
  286. $pSheet = array_splice(
  287. $this->_workSheetCollection,
  288. $oldIndex,
  289. 1
  290. );
  291. array_splice(
  292. $this->_workSheetCollection,
  293. $newIndex,
  294. 0,
  295. $pSheet
  296. );
  297. return $newIndex;
  298. }
  299. /**
  300. * Get sheet count
  301. *
  302. * @return int
  303. */
  304. public function getSheetCount()
  305. {
  306. return count($this->_workSheetCollection);
  307. }
  308. /**
  309. * Get active sheet index
  310. *
  311. * @return int Active sheet index
  312. */
  313. public function getActiveSheetIndex()
  314. {
  315. return $this->_activeSheetIndex;
  316. }
  317. /**
  318. * Set active sheet index
  319. *
  320. * @param int $pIndex Active sheet index
  321. * @throws Exception
  322. * @return PHPExcel_Worksheet
  323. */
  324. public function setActiveSheetIndex($pIndex = 0)
  325. {
  326. if ($pIndex > count($this->_workSheetCollection) - 1) {
  327. throw new Exception("Active sheet index is out of bounds.");
  328. } else {
  329. $this->_activeSheetIndex = $pIndex;
  330. }
  331. return $this->getActiveSheet();
  332. }
  333. /**
  334. * Set active sheet index by name
  335. *
  336. * @param string $pValue Sheet title
  337. * @return PHPExcel_Worksheet
  338. * @throws Exception
  339. */
  340. public function setActiveSheetIndexByName($pValue = '')
  341. {
  342. if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) {
  343. $this->setActiveSheetIndex($worksheet->getParent()->getIndex($worksheet));
  344. return $worksheet;
  345. }
  346. throw new Exception('Workbook does not contain sheet:' . $pValue);
  347. }
  348. /**
  349. * Get sheet names
  350. *
  351. * @return string[]
  352. */
  353. public function getSheetNames()
  354. {
  355. $returnValue = array();
  356. $worksheetCount = $this->getSheetCount();
  357. for ($i = 0; $i < $worksheetCount; ++$i) {
  358. array_push($returnValue, $this->getSheet($i)->getTitle());
  359. }
  360. return $returnValue;
  361. }
  362. /**
  363. * Add external sheet
  364. *
  365. * @param PHPExcel_Worksheet $pSheet External sheet to add
  366. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  367. * @throws Exception
  368. * @return PHPExcel_Worksheet
  369. */
  370. public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) {
  371. if (!is_null($this->getSheetByName($pSheet->getTitle()))) {
  372. throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  373. }
  374. // count how many cellXfs there are in this workbook currently, we will need this below
  375. $countCellXfs = count($this->_cellXfCollection);
  376. // copy all the shared cellXfs from the external workbook and append them to the current
  377. foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
  378. $this->addCellXf(clone $cellXf);
  379. }
  380. // move sheet to this workbook
  381. $pSheet->rebindParent($this);
  382. // update the cellXfs
  383. foreach ($pSheet->getCellCollection(false) as $cellID) {
  384. $cell = $pSheet->getCell($cellID);
  385. $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
  386. }
  387. return $this->addSheet($pSheet, $iSheetIndex);
  388. }
  389. /**
  390. * Get named ranges
  391. *
  392. * @return PHPExcel_NamedRange[]
  393. */
  394. public function getNamedRanges() {
  395. return $this->_namedRanges;
  396. }
  397. /**
  398. * Add named range
  399. *
  400. * @param PHPExcel_NamedRange $namedRange
  401. * @return PHPExcel
  402. */
  403. public function addNamedRange(PHPExcel_NamedRange $namedRange) {
  404. if ($namedRange->getScope() == null) {
  405. // global scope
  406. $this->_namedRanges[$namedRange->getName()] = $namedRange;
  407. } else {
  408. // local scope
  409. $this->_namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
  410. }
  411. return true;
  412. }
  413. /**
  414. * Get named range
  415. *
  416. * @param string $namedRange
  417. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  418. * @return PHPExcel_NamedRange|null
  419. */
  420. public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
  421. $returnValue = null;
  422. if ($namedRange != '' && !is_null($namedRange)) {
  423. // first look for global defined name
  424. if (isset($this->_namedRanges[$namedRange])) {
  425. $returnValue = $this->_namedRanges[$namedRange];
  426. }
  427. // then look for local defined name (has priority over global defined name if both names exist)
  428. if (!is_null($pSheet) && isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  429. $returnValue = $this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange];
  430. }
  431. }
  432. return $returnValue;
  433. }
  434. /**
  435. * Remove named range
  436. *
  437. * @param string $namedRange
  438. * @param PHPExcel_Worksheet|null $pSheet. Scope. Use null for global scope.
  439. * @return PHPExcel
  440. */
  441. public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
  442. if (is_null($pSheet)) {
  443. if (isset($this->_namedRanges[$namedRange])) {
  444. unset($this->_namedRanges[$namedRange]);
  445. }
  446. } else {
  447. if (isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  448. unset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
  449. }
  450. }
  451. return $this;
  452. }
  453. /**
  454. * Get worksheet iterator
  455. *
  456. * @return PHPExcel_WorksheetIterator
  457. */
  458. public function getWorksheetIterator() {
  459. return new PHPExcel_WorksheetIterator($this);
  460. }
  461. /**
  462. * Copy workbook (!= clone!)
  463. *
  464. * @return PHPExcel
  465. */
  466. public function copy() {
  467. $copied = clone $this;
  468. $worksheetCount = count($this->_workSheetCollection);
  469. for ($i = 0; $i < $worksheetCount; ++$i) {
  470. $this->_workSheetCollection[$i] = $this->_workSheetCollection[$i]->copy();
  471. $this->_workSheetCollection[$i]->rebindParent($this);
  472. }
  473. return $copied;
  474. }
  475. /**
  476. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  477. */
  478. public function __clone() {
  479. foreach($this as $key => $val) {
  480. if (is_object($val) || (is_array($val))) {
  481. $this->{$key} = unserialize(serialize($val));
  482. }
  483. }
  484. }
  485. /**
  486. * Get the workbook collection of cellXfs
  487. *
  488. * @return PHPExcel_Style[]
  489. */
  490. public function getCellXfCollection()
  491. {
  492. return $this->_cellXfCollection;
  493. }
  494. /**
  495. * Get cellXf by index
  496. *
  497. * @param int $index
  498. * @return PHPExcel_Style
  499. */
  500. public function getCellXfByIndex($pIndex = 0)
  501. {
  502. return $this->_cellXfCollection[$pIndex];
  503. }
  504. /**
  505. * Get cellXf by hash code
  506. *
  507. * @param string $pValue
  508. * @return PHPExcel_Style|false
  509. */
  510. public function getCellXfByHashCode($pValue = '')
  511. {
  512. foreach ($this->_cellXfCollection as $cellXf) {
  513. if ($cellXf->getHashCode() == $pValue) {
  514. return $cellXf;
  515. }
  516. }
  517. return false;
  518. }
  519. /**
  520. * Get default style
  521. *
  522. * @return PHPExcel_Style
  523. * @throws Exception
  524. */
  525. public function getDefaultStyle()
  526. {
  527. if (isset($this->_cellXfCollection[0])) {
  528. return $this->_cellXfCollection[0];
  529. }
  530. throw new Exception('No default style found for this workbook');
  531. }
  532. /**
  533. * Add a cellXf to the workbook
  534. *
  535. * @param PHPExcel_Style
  536. */
  537. public function addCellXf(PHPExcel_Style $style)
  538. {
  539. $this->_cellXfCollection[] = $style;
  540. $style->setIndex(count($this->_cellXfCollection) - 1);
  541. }
  542. /**
  543. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  544. *
  545. * @param int $pIndex Index to cellXf
  546. * @throws Exception
  547. */
  548. public function removeCellXfByIndex($pIndex = 0)
  549. {
  550. if ($pIndex > count($this->_cellXfCollection) - 1) {
  551. throw new Exception("CellXf index is out of bounds.");
  552. } else {
  553. // first remove the cellXf
  554. array_splice($this->_cellXfCollection, $pIndex, 1);
  555. // then update cellXf indexes for cells
  556. foreach ($this->_workSheetCollection as $worksheet) {
  557. foreach ($worksheet->getCellCollection(false) as $cellID) {
  558. $cell = $worksheet->getCell($cellID);
  559. $xfIndex = $cell->getXfIndex();
  560. if ($xfIndex > $pIndex ) {
  561. // decrease xf index by 1
  562. $cell->setXfIndex($xfIndex - 1);
  563. } else if ($xfIndex == $pIndex) {
  564. // set to default xf index 0
  565. $cell->setXfIndex(0);
  566. }
  567. }
  568. }
  569. }
  570. }
  571. /**
  572. * Get the cellXf supervisor
  573. *
  574. * @return PHPExcel_Style
  575. */
  576. public function getCellXfSupervisor()
  577. {
  578. return $this->_cellXfSupervisor;
  579. }
  580. /**
  581. * Get the workbook collection of cellStyleXfs
  582. *
  583. * @return PHPExcel_Style[]
  584. */
  585. public function getCellStyleXfCollection()
  586. {
  587. return $this->_cellStyleXfCollection;
  588. }
  589. /**
  590. * Get cellStyleXf by index
  591. *
  592. * @param int $pIndex
  593. * @return PHPExcel_Style
  594. */
  595. public function getCellStyleXfByIndex($pIndex = 0)
  596. {
  597. return $this->_cellStyleXfCollection[$pIndex];
  598. }
  599. /**
  600. * Get cellStyleXf by hash code
  601. *
  602. * @param string $pValue
  603. * @return PHPExcel_Style|false
  604. */
  605. public function getCellStyleXfByHashCode($pValue = '')
  606. {
  607. foreach ($this->_cellXfStyleCollection as $cellStyleXf) {
  608. if ($cellStyleXf->getHashCode() == $pValue) {
  609. return $cellStyleXf;
  610. }
  611. }
  612. return false;
  613. }
  614. /**
  615. * Add a cellStyleXf to the workbook
  616. *
  617. * @param PHPExcel_Style $pStyle
  618. */
  619. public function addCellStyleXf(PHPExcel_Style $pStyle)
  620. {
  621. $this->_cellStyleXfCollection[] = $pStyle;
  622. $pStyle->setIndex(count($this->_cellStyleXfCollection) - 1);
  623. }
  624. /**
  625. * Remove cellStyleXf by index
  626. *
  627. * @param int $pIndex
  628. * @throws Exception
  629. */
  630. public function removeCellStyleXfByIndex($pIndex = 0)
  631. {
  632. if ($pIndex > count($this->_cellStyleXfCollection) - 1) {
  633. throw new Exception("CellStyleXf index is out of bounds.");
  634. } else {
  635. array_splice($this->_cellStyleXfCollection, $pIndex, 1);
  636. }
  637. }
  638. /**
  639. * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
  640. * and columns in the workbook
  641. */
  642. public function garbageCollect()
  643. {
  644. // how many references are there to each cellXf ?
  645. $countReferencesCellXf = array();
  646. foreach ($this->_cellXfCollection as $index => $cellXf) {
  647. $countReferencesCellXf[$index] = 0;
  648. }
  649. foreach ($this->getWorksheetIterator() as $sheet) {
  650. // from cells
  651. foreach ($sheet->getCellCollection(false) as $cellID) {
  652. $cell = $sheet->getCell($cellID);
  653. ++$countReferencesCellXf[$cell->getXfIndex()];
  654. }
  655. // from row dimensions
  656. foreach ($sheet->getRowDimensions() as $rowDimension) {
  657. if ($rowDimension->getXfIndex() !== null) {
  658. ++$countReferencesCellXf[$rowDimension->getXfIndex()];
  659. }
  660. }
  661. // from column dimensions
  662. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  663. ++$countReferencesCellXf[$columnDimension->getXfIndex()];
  664. }
  665. }
  666. // remove cellXfs without references and create mapping so we can update xfIndex
  667. // for all cells and columns
  668. $countNeededCellXfs = 0;
  669. foreach ($this->_cellXfCollection as $index => $cellXf) {
  670. if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
  671. ++$countNeededCellXfs;
  672. } else {
  673. unset($this->_cellXfCollection[$index]);
  674. }
  675. $map[$index] = $countNeededCellXfs - 1;
  676. }
  677. $this->_cellXfCollection = array_values($this->_cellXfCollection);
  678. // update the index for all cellXfs
  679. foreach ($this->_cellXfCollection as $i => $cellXf) {
  680. $cellXf->setIndex($i);
  681. }
  682. // make sure there is always at least one cellXf (there should be)
  683. if (count($this->_cellXfCollection) == 0) {
  684. $this->_cellXfCollection[] = new PHPExcel_Style();
  685. }
  686. // update the xfIndex for all cells, row dimensions, column dimensions
  687. foreach ($this->getWorksheetIterator() as $sheet) {
  688. // for all cells
  689. foreach ($sheet->getCellCollection(false) as $cellID) {
  690. $cell = $sheet->getCell($cellID);
  691. $cell->setXfIndex( $map[$cell->getXfIndex()] );
  692. }
  693. // for all row dimensions
  694. foreach ($sheet->getRowDimensions() as $rowDimension) {
  695. if ($rowDimension->getXfIndex() !== null) {
  696. $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] );
  697. }
  698. }
  699. // for all column dimensions
  700. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  701. $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] );
  702. }
  703. }
  704. // also do garbage collection for all the sheets
  705. foreach ($this->getWorksheetIterator() as $sheet) {
  706. $sheet->garbageCollect();
  707. }
  708. }
  709. }