30 if (!defined(
'PHPEXCEL_ROOT')) {
31 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/');
35 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell.php';
38 require_once PHPEXCEL_ROOT .
'PHPExcel/DocumentProperties.php';
41 require_once PHPEXCEL_ROOT .
'PHPExcel/DocumentSecurity.php';
44 require_once PHPEXCEL_ROOT .
'PHPExcel/Worksheet.php';
47 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/ZipStreamWrapper.php';
50 require_once PHPEXCEL_ROOT .
'PHPExcel/NamedRange.php';
53 require_once PHPEXCEL_ROOT .
'PHPExcel/WorksheetIterator.php';
127 $this->_workSheetCollection = array();
129 $this->_activeSheetIndex = 0;
138 $this->_namedRanges = array();
142 $this->_cellXfSupervisor->bindParent($this);
166 $this->_properties = $pValue;
186 $this->_security = $pValue;
207 $this->
addSheet($newSheet, $iSheetIndex);
219 if(is_null($iSheetIndex))
221 $this->_workSheetCollection[] = $pSheet;
227 $this->_workSheetCollection,
243 if ($pIndex > count($this->_workSheetCollection) - 1) {
244 throw new Exception(
"Sheet index is out of bounds.");
246 array_splice($this->_workSheetCollection, $pIndex, 1);
259 if ($pIndex > count($this->_workSheetCollection) - 1) {
260 throw new Exception(
"Sheet index is out of bounds.");
262 return $this->_workSheetCollection[$pIndex];
285 $worksheetCount = count($this->_workSheetCollection);
286 for ($i = 0; $i < $worksheetCount; ++$i) {
287 if ($this->_workSheetCollection[$i]->getTitle() == $pName) {
288 return $this->_workSheetCollection[$i];
304 foreach ($this->_workSheetCollection as
$key => $value) {
305 if ($value->getHashCode() == $pSheet->
getHashCode()) {
322 $pSheet = array_splice(
323 $this->_workSheetCollection,
328 $this->_workSheetCollection,
343 return count($this->_workSheetCollection);
365 if ($pIndex > count($this->_workSheetCollection) - 1) {
366 throw new Exception(
"Active sheet index is out of bounds.");
368 $this->_activeSheetIndex = $pIndex;
380 $returnValue = array();
382 for ($i = 0; $i < $worksheetCount; ++$i) {
383 array_push($returnValue, $this->
getSheet($i)->getTitle());
398 throw new Exception(
"Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
402 $countCellXfs = count($this->_cellXfCollection);
405 foreach ($pSheet->
getParent()->getCellXfCollection() as $cellXf) {
414 $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
436 $this->_namedRanges[$namedRange->
getWorksheet()->getTitle().
'!'.$namedRange->
getName()] = $namedRange;
446 if ($namedRange !=
'' && !is_null($namedRange)) {
447 if (!is_null($pSheet)) {
448 $key = $pSheet->getTitle().
'!'.$namedRange;
449 if (isset($this->_namedRanges[
$key])) {
450 return $this->_namedRanges[
$key];
454 foreach($this->_namedRanges as $_namedRange) {
455 if ($_namedRange->getName() == $namedRange) {
456 if ((!is_null($pSheet)) && ($_namedRange->getWorksheet()->getTitle() == $pSheet->getTitle())) {
460 $returnValue = $_namedRange;
464 if ($returnCount == 1) {
479 if ($namedRange !=
'' && !is_null($namedRange)) {
480 if (!is_null($pSheet)) {
481 $key = $pSheet->getTitle().
'!'.$namedRange;
482 if (isset($this->_namedRanges[
$key])) {
483 unset($this->_namedRanges[$key]);
486 foreach($this->_namedRanges as $_namedRange) {
487 if ($_namedRange->getName() == $namedRange) {
488 if ((!is_null($pSheet)) && ($_namedRange->getWorksheet()->getTitle() == $pSheet->getTitle())) {
489 $key = $pSheet->getTitle().
'!'.$namedRange;
490 if (isset($this->_namedRanges[
$key])) {
491 unset($this->_namedRanges[$key]);
515 $copied = clone $this;
517 $worksheetCount = count($this->_workSheetCollection);
518 for ($i = 0; $i < $worksheetCount; ++$i) {
519 $this->_workSheetCollection[$i] = $this->_workSheetCollection[$i]->copy();
520 $this->_workSheetCollection[$i]->rebindParent($this);
530 foreach($this as
$key => $val) {
531 if (is_object($val) || (is_array($val))) {
532 $this->{
$key} = unserialize(serialize($val));
555 return $this->_cellXfCollection[$pIndex];
566 foreach ($this->_cellXfCollection as $cellXf) {
567 if ($cellXf->getHashCode() == $pValue) {
582 if (isset($this->_cellXfCollection[0])) {
583 return $this->_cellXfCollection[0];
585 throw new Exception(
'No default style found for this workbook');
595 $this->_cellXfCollection[] = $style;
596 $style->
setIndex(count($this->_cellXfCollection) - 1);
607 if ($pIndex > count($this->_cellXfCollection) - 1) {
608 throw new Exception(
"CellXf index is out of bounds.");
611 array_splice($this->_cellXfCollection, $pIndex, 1);
614 foreach ($this->_workSheetCollection as $worksheet) {
615 foreach ($worksheet->getCellCollection(
false) as $cell) {
616 $xfIndex = $cell->getXfIndex();
617 if ($xfIndex > $pIndex ) {
619 $cell->setXfIndex($xfIndex - 1);
620 }
else if ($xfIndex == $pIndex) {
622 $cell->setXfIndex(0);
657 return $this->_cellStyleXfCollection[$pIndex];
668 foreach ($this->_cellXfStyleCollection as $cellStyleXf) {
669 if ($cellStyleXf->getHashCode() == $pValue) {
683 $this->_cellStyleXfCollection[] = $pStyle;
684 $pStyle->
setIndex(count($this->_cellStyleXfCollection) - 1);
695 if ($pIndex > count($this->_cellStyleXfCollection) - 1) {
696 throw new Exception(
"CellStyleXf index is out of bounds.");
698 array_splice($this->_cellStyleXfCollection, $pIndex, 1);
708 $countReferencesCellXf = array();
709 foreach ($this->_cellXfCollection as $index => $cellXf) {
710 $countReferencesCellXf[$index] = 0;
714 foreach ($sheet->getCellCollection(
false) as $cell) {
715 ++$countReferencesCellXf[$cell->getXfIndex()];
720 $countNeededCellXfs = 0;
721 foreach ($this->_cellXfCollection as $index => $cellXf) {
722 if ($countReferencesCellXf[$index] > 0 || $index == 0) {
723 ++$countNeededCellXfs;
725 unset($this->_cellXfCollection[$index]);
727 $map[$index] = $countNeededCellXfs - 1;
729 $this->_cellXfCollection = array_values($this->_cellXfCollection);
732 if (count($this->_cellXfCollection) == 0) {
738 foreach ($sheet->getCellCollection(
false) as $cell) {
739 $cell->setXfIndex(
$map[$cell->getXfIndex()] );
745 $sheet->garbageCollect();