PHPExcel
[ class tree: PHPExcel ] [ index: PHPExcel ] [ all elements ]

Source for file PHPExcel.php

Documentation is available at PHPExcel.php

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

Documentation generated on Mon, 25 Aug 2008 13:47:34 +0200 by phpDocumentor 1.4.1