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

Documentation generated on Wed, 22 Apr 2009 09:01:29 +0200 by phpDocumentor 1.4.1