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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  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 - 2009 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->_workSheetCollection1{
  196.             throw new Exception("Sheet index is out of bounds.");
  197.         else {
  198.             array_splice($this->_workSheetCollection$pIndex1);
  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->_workSheetCollection1{
  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.         $worksheetCount count($this->_workSheetCollection);
  238.         for ($i 0$i $worksheetCount++$i{
  239.             if ($this->_workSheetCollection[$i]->getTitle(== $pName{
  240.                 return $this->_workSheetCollection[$i];
  241.             }
  242.         }
  243.  
  244.         return null;
  245.     }
  246.  
  247.     /**
  248.      * Get index for sheet
  249.      *
  250.      * @param PHPExcel_Worksheet $pSheet 
  251.      * @return Sheet index
  252.      * @throws Exception
  253.      */
  254.     public function getIndex(PHPExcel_Worksheet $pSheet)
  255.     {
  256.         foreach ($this->_workSheetCollection as $key => $value{
  257.             if ($value->getHashCode(== $pSheet->getHashCode()) {
  258.                 return $key;
  259.             }
  260.         }
  261.     }
  262.  
  263.     /**
  264.      * Get sheet count
  265.      *
  266.      * @return int 
  267.      */
  268.     public function getSheetCount()
  269.     {
  270.         return count($this->_workSheetCollection);
  271.     }
  272.  
  273.     /**
  274.      * Get active sheet index
  275.      *
  276.      * @return int Active sheet index
  277.      */
  278.     public function getActiveSheetIndex()
  279.     {
  280.         return $this->_activeSheetIndex;
  281.     }
  282.  
  283.     /**
  284.      * Set active sheet index
  285.      *
  286.      * @param int $pIndex Active sheet index
  287.      * @throws Exception
  288.      */
  289.     public function setActiveSheetIndex($pIndex 0)
  290.     {
  291.         if ($pIndex count($this->_workSheetCollection1{
  292.             throw new Exception("Active sheet index is out of bounds.");
  293.         else {
  294.             $this->_activeSheetIndex = $pIndex;
  295.         }
  296.     }
  297.  
  298.     /**
  299.      * Get sheet names
  300.      *
  301.      * @return string[] 
  302.      */
  303.     public function getSheetNames()
  304.     {
  305.         $returnValue array();
  306.         $worksheetCount $this->getSheetCount();
  307.         for ($i 0$i $worksheetCount++$i{
  308.             array_push($returnValue$this->getSheet($i)->getTitle());
  309.         }
  310.  
  311.         return $returnValue;
  312.     }
  313.  
  314.     /**
  315.      * Add external sheet
  316.      *
  317.      * @param PHPExcel_Worksheet $pSheet External sheet to add
  318.      * @throws Exception
  319.      */
  320.     public function addExternalSheet(PHPExcel_Worksheet $pSheet{
  321.         if (!is_null($this->getSheetByName($pSheet->getTitle()))) {
  322.             throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  323.         }
  324.  
  325.         $pSheet->rebindParent($this);
  326.         $this->addSheet($pSheet);
  327.     }
  328.  
  329.     /**
  330.      * Get named ranges
  331.      *
  332.      * @return PHPExcel_NamedRange[] 
  333.      */
  334.     public function getNamedRanges({
  335.         return $this->_namedRanges;
  336.     }
  337.  
  338.     /**
  339.      * Add named range
  340.      *
  341.      * @param PHPExcel_NamedRange $namedRange 
  342.      */
  343.     public function addNamedRange(PHPExcel_NamedRange $namedRange{
  344.         $this->_namedRanges[$namedRange->getName()$namedRange;
  345.     }
  346.  
  347.     /**
  348.      * Get named range
  349.      *
  350.      * @param string $namedRange 
  351.      */
  352.     public function getNamedRange($namedRange{
  353.         if ($namedRange != '' && !is_null($namedRange&& @isset($this->_namedRanges[$namedRange])) {
  354.             return $this->_namedRanges[$namedRange];
  355.         }
  356.  
  357.         return null;
  358.     }
  359.  
  360.     /**
  361.      * Remove named range
  362.      *
  363.      * @param string $namedRange 
  364.      */
  365.     public function removeNamedRange($namedRange{
  366.         if ($namedRange != '' && !is_null($namedRange&& @isset($this->_namedRanges[$namedRange])) {
  367.             unset($this->_namedRanges[$namedRange]);
  368.         }
  369.     }
  370.  
  371.     /**
  372.      * Copy workbook (!= clone!)
  373.      *
  374.      * @return PHPExcel 
  375.      */
  376.     public function copy({
  377.         $copied clone $this;
  378.  
  379.         $worksheetCount count($this->_workSheetCollection);
  380.         for ($i 0$i $worksheetCount++$i{
  381.             $this->_workSheetCollection[$i$this->_workSheetCollection[$i]->copy();
  382.             $this->_workSheetCollection[$i]->rebindParent($this);
  383.         }
  384.  
  385.         return $copied;
  386.     }
  387.  
  388.     /**
  389.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  390.      */
  391.     public function __clone({
  392.         $vars get_object_vars($this);
  393.         foreach ($vars as $key => $value{
  394.             if (is_object($value)) {
  395.                 $this->$key clone $value;
  396.             else {
  397.                 $this->$key $value;
  398.             }
  399.         }
  400.     }
  401. }

Documentation generated on Mon, 05 Jan 2009 20:38:22 +0100 by phpDocumentor 1.4.1