ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
PHPExcel.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
31  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
32 }
33 
35 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel/DocumentProperties.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/DocumentSecurity.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/ZipStreamWrapper.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/NamedRange.php';
51 
53 require_once PHPEXCEL_ROOT . 'PHPExcel/WorksheetIterator.php';
54 
55 
63 class PHPExcel
64 {
70  private $_properties;
71 
77  private $_security;
78 
84  private $_workSheetCollection = array();
85 
91  private $_activeSheetIndex = 0;
92 
98  private $_namedRanges = array();
99 
106 
112  private $_cellXfCollection = array();
113 
119  private $_cellStyleXfCollection = array();
120 
124  public function __construct()
125  {
126  // Initialise worksheet collection and add one worksheet
127  $this->_workSheetCollection = array();
128  $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
129  $this->_activeSheetIndex = 0;
130 
131  // Create document properties
132  $this->_properties = new PHPExcel_DocumentProperties();
133 
134  // Create document security
135  $this->_security = new PHPExcel_DocumentSecurity();
136 
137  // Set named ranges
138  $this->_namedRanges = array();
139 
140  // Create the cellXf supervisor
141  $this->_cellXfSupervisor = new PHPExcel_Style(true);
142  $this->_cellXfSupervisor->bindParent($this);
143 
144  // Create the default style
145  $this->addCellXf(new PHPExcel_Style);
146  $this->addCellStyleXf(new PHPExcel_Style);
147  }
148 
154  public function getProperties()
155  {
156  return $this->_properties;
157  }
158 
165  {
166  $this->_properties = $pValue;
167  }
168 
174  public function getSecurity()
175  {
176  return $this->_security;
177  }
178 
184  public function setSecurity(PHPExcel_DocumentSecurity $pValue)
185  {
186  $this->_security = $pValue;
187  }
188 
194  public function getActiveSheet()
195  {
196  return $this->_workSheetCollection[$this->_activeSheetIndex];
197  }
198 
204  public function createSheet($iSheetIndex = null)
205  {
206  $newSheet = new PHPExcel_Worksheet($this);
207  $this->addSheet($newSheet, $iSheetIndex);
208  return $newSheet;
209  }
210 
217  public function addSheet(PHPExcel_Worksheet $pSheet = null, $iSheetIndex = null)
218  {
219  if(is_null($iSheetIndex))
220  {
221  $this->_workSheetCollection[] = $pSheet;
222  }
223  else
224  {
225  // Insert the sheet at the requested index
226  array_splice(
227  $this->_workSheetCollection,
228  $iSheetIndex,
229  0,
230  array($pSheet)
231  );
232  }
233  }
234 
241  public function removeSheetByIndex($pIndex = 0)
242  {
243  if ($pIndex > count($this->_workSheetCollection) - 1) {
244  throw new Exception("Sheet index is out of bounds.");
245  } else {
246  array_splice($this->_workSheetCollection, $pIndex, 1);
247  }
248  }
249 
257  public function getSheet($pIndex = 0)
258  {
259  if ($pIndex > count($this->_workSheetCollection) - 1) {
260  throw new Exception("Sheet index is out of bounds.");
261  } else {
262  return $this->_workSheetCollection[$pIndex];
263  }
264  }
265 
271  public function getAllSheets()
272  {
274  }
275 
283  public function getSheetByName($pName = '')
284  {
285  $worksheetCount = count($this->_workSheetCollection);
286  for ($i = 0; $i < $worksheetCount; ++$i) {
287  if ($this->_workSheetCollection[$i]->getTitle() == $pName) {
288  return $this->_workSheetCollection[$i];
289  }
290  }
291 
292  return null;
293  }
294 
302  public function getIndex(PHPExcel_Worksheet $pSheet)
303  {
304  foreach ($this->_workSheetCollection as $key => $value) {
305  if ($value->getHashCode() == $pSheet->getHashCode()) {
306  return $key;
307  }
308  }
309  }
310 
319  public function setIndexByName($sheetName, $newIndex)
320  {
321  $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
322  $pSheet = array_splice(
323  $this->_workSheetCollection,
324  $oldIndex,
325  1
326  );
327  array_splice(
328  $this->_workSheetCollection,
329  $newIndex,
330  0,
331  $pSheet
332  );
333  return $newIndex;
334  }
335 
341  public function getSheetCount()
342  {
343  return count($this->_workSheetCollection);
344  }
345 
351  public function getActiveSheetIndex()
352  {
354  }
355 
363  public function setActiveSheetIndex($pIndex = 0)
364  {
365  if ($pIndex > count($this->_workSheetCollection) - 1) {
366  throw new Exception("Active sheet index is out of bounds.");
367  } else {
368  $this->_activeSheetIndex = $pIndex;
369  }
370  return $this->getActiveSheet();
371  }
372 
378  public function getSheetNames()
379  {
380  $returnValue = array();
381  $worksheetCount = $this->getSheetCount();
382  for ($i = 0; $i < $worksheetCount; ++$i) {
383  array_push($returnValue, $this->getSheet($i)->getTitle());
384  }
385 
386  return $returnValue;
387  }
388 
396  public function addExternalSheet(PHPExcel_Worksheet $pSheet) {
397  if (!is_null($this->getSheetByName($pSheet->getTitle()))) {
398  throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
399  }
400 
401  // count how many cellXfs there are in this workbook currently, we will need this below
402  $countCellXfs = count($this->_cellXfCollection);
403 
404  // copy all the shared cellXfs from the external workbook and append them to the current
405  foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
406  $this->addCellXf(clone $cellXf);
407  }
408 
409  // move sheet to this workbook
410  $pSheet->rebindParent($this);
411 
412  // update the cellXfs
413  foreach ($pSheet->getCellCollection(false) as $cell) {
414  $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
415  }
416 
417  return $this->addSheet($pSheet);
418  }
419 
425  public function getNamedRanges() {
426  return $this->_namedRanges;
427  }
428 
435  public function addNamedRange(PHPExcel_NamedRange $namedRange) {
436  $this->_namedRanges[$namedRange->getWorksheet()->getTitle().'!'.$namedRange->getName()] = $namedRange;
437  return true;
438  }
439 
445  public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
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];
451  }
452  }
453  $returnCount = 0;
454  foreach($this->_namedRanges as $_namedRange) {
455  if ($_namedRange->getName() == $namedRange) {
456  if ((!is_null($pSheet)) && ($_namedRange->getWorksheet()->getTitle() == $pSheet->getTitle())) {
457  return $_namedRange;
458  } else {
459  $returnCount++;
460  $returnValue = $_namedRange;
461  }
462  }
463  }
464  if ($returnCount == 1) {
465  return $returnValue;
466  }
467  }
468 
469  return null;
470  }
471 
478  public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
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]);
484  }
485  }
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]);
492  }
493  }
494  }
495  }
496  }
497  return $this;
498  }
499 
505  public function getWorksheetIterator() {
506  return new PHPExcel_WorksheetIterator($this);
507  }
508 
514  public function copy() {
515  $copied = clone $this;
516 
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);
521  }
522 
523  return $copied;
524  }
525 
529  public function __clone() {
530  foreach($this as $key => $val) {
531  if (is_object($val) || (is_array($val))) {
532  $this->{$key} = unserialize(serialize($val));
533  }
534  }
535  }
536 
542  public function getCellXfCollection()
543  {
545  }
546 
553  public function getCellXfByIndex($pIndex = 0)
554  {
555  return $this->_cellXfCollection[$pIndex];
556  }
557 
564  public function getCellXfByHashCode($pValue = '')
565  {
566  foreach ($this->_cellXfCollection as $cellXf) {
567  if ($cellXf->getHashCode() == $pValue) {
568  return $cellXf;
569  }
570  }
571  return false;
572  }
573 
580  public function getDefaultStyle()
581  {
582  if (isset($this->_cellXfCollection[0])) {
583  return $this->_cellXfCollection[0];
584  }
585  throw new Exception('No default style found for this workbook');
586  }
587 
593  public function addCellXf(PHPExcel_Style $style)
594  {
595  $this->_cellXfCollection[] = $style;
596  $style->setIndex(count($this->_cellXfCollection) - 1);
597  }
598 
605  public function removeCellXfByIndex($pIndex = 0)
606  {
607  if ($pIndex > count($this->_cellXfCollection) - 1) {
608  throw new Exception("CellXf index is out of bounds.");
609  } else {
610  // first remove the cellXf
611  array_splice($this->_cellXfCollection, $pIndex, 1);
612 
613  // then update cellXf indexes for cells
614  foreach ($this->_workSheetCollection as $worksheet) {
615  foreach ($worksheet->getCellCollection(false) as $cell) {
616  $xfIndex = $cell->getXfIndex();
617  if ($xfIndex > $pIndex ) {
618  // decrease xf index by 1
619  $cell->setXfIndex($xfIndex - 1);
620  } else if ($xfIndex == $pIndex) {
621  // set to default xf index 0
622  $cell->setXfIndex(0);
623  }
624  }
625  }
626  }
627  }
628 
634  public function getCellXfSupervisor()
635  {
637  }
638 
644  public function getCellStyleXfCollection()
645  {
647  }
648 
655  public function getCellStyleXfByIndex($pIndex = 0)
656  {
657  return $this->_cellStyleXfCollection[$pIndex];
658  }
659 
666  public function getCellStyleXfByHashCode($pValue = '')
667  {
668  foreach ($this->_cellXfStyleCollection as $cellStyleXf) {
669  if ($cellStyleXf->getHashCode() == $pValue) {
670  return $cellStyleXf;
671  }
672  }
673  return false;
674  }
675 
681  public function addCellStyleXf(PHPExcel_Style $pStyle)
682  {
683  $this->_cellStyleXfCollection[] = $pStyle;
684  $pStyle->setIndex(count($this->_cellStyleXfCollection) - 1);
685  }
686 
693  public function removeCellStyleXfByIndex($pIndex = 0)
694  {
695  if ($pIndex > count($this->_cellStyleXfCollection) - 1) {
696  throw new Exception("CellStyleXf index is out of bounds.");
697  } else {
698  array_splice($this->_cellStyleXfCollection, $pIndex, 1);
699  }
700  }
701 
705  public function garbageCollect()
706  {
707  // how many references are there to each cellXf ?
708  $countReferencesCellXf = array();
709  foreach ($this->_cellXfCollection as $index => $cellXf) {
710  $countReferencesCellXf[$index] = 0;
711  }
712 
713  foreach ($this->getWorksheetIterator() as $sheet) {
714  foreach ($sheet->getCellCollection(false) as $cell) {
715  ++$countReferencesCellXf[$cell->getXfIndex()];
716  }
717  }
718 
719  // remove those cellXfs that have zero references and create mapping so we can update xfIndex for all cells
720  $countNeededCellXfs = 0;
721  foreach ($this->_cellXfCollection as $index => $cellXf) {
722  if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
723  ++$countNeededCellXfs;
724  } else {
725  unset($this->_cellXfCollection[$index]);
726  }
727  $map[$index] = $countNeededCellXfs - 1;
728  }
729  $this->_cellXfCollection = array_values($this->_cellXfCollection);
730 
731  // if we removed the first style by accident, recreate it
732  if (count($this->_cellXfCollection) == 0) {
733  $this->_cellXfCollection[] = new PHPExcel_Style();
734  }
735 
736  // update the xfIndex for all cells
737  foreach ($this->getWorksheetIterator() as $sheet) {
738  foreach ($sheet->getCellCollection(false) as $cell) {
739  $cell->setXfIndex( $map[$cell->getXfIndex()] );
740  }
741  }
742 
743  // also do garbage collection for all the sheets
744  foreach ($this->getWorksheetIterator() as $sheet) {
745  $sheet->garbageCollect();
746  }
747  }
748 
749 }