Source for file PHPExcel.php
Documentation is available at PHPExcel.php 
 * Copyright (c) 2006 - 2011 PHPExcel  
 * This library is free software; you can redistribute it and/or  
 * modify it under the terms of the GNU Lesser General Public  
 * License as published by the Free Software Foundation; either  
 * version 2.1 of the License, or (at your option) any later version.  
 * This library is distributed in the hope that it will be useful,  
 * but WITHOUT ANY WARRANTY; without even the implied warranty of  
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 * Lesser General Public License for more details.  
 * You should have received a copy of the GNU Lesser General Public  
 * License along with this library; if not, write to the Free Software  
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA  
 * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)  
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL  
 * @version    1.7.6, 2011-02-27  
/** PHPExcel root directory */  
    define('PHPEXCEL_ROOT', dirname(__FILE__ ) .  '/');  
    require (PHPEXCEL_ROOT .  'PHPExcel/Autoloader.php'); 
 * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)  
     * @var PHPExcel_DocumentProperties   
     * @var PHPExcel_DocumentSecurity   
     * Collection of Worksheet objects  
     * @var PHPExcel_Worksheet[]   
    private $_workSheetCollection =  array();  
    private $_activeSheetIndex =  0;  
     * @var PHPExcel_NamedRange[]   
    private $_namedRanges =  array();  
    private $_cellXfSupervisor;  
    private $_cellXfCollection =  array();  
    private $_cellStyleXfCollection =  array();  
     * Create a new PHPExcel with one Worksheet  
        // Initialise worksheet collection and add one worksheet  
        $this->_workSheetCollection =  array();  
        $this->_activeSheetIndex =  0;  
        // Create document properties  
        // Create document security  
        $this->_namedRanges =  array();  
        // Create the cellXf supervisor  
        $this->_cellXfSupervisor->bindParent($this);  
        // Create the default style  
        foreach($this->_workSheetCollection as $k =>  &$worksheet) {  
            $worksheet->disconnectCells();  
            $this->_workSheetCollection[$k] =  null;  
        $this->_workSheetCollection =  array();  
     * @return PHPExcel_DocumentProperties   
        return $this->_properties;  
     * @param PHPExcel_DocumentProperties    $pValue   
    public function setProperties(PHPExcel_DocumentProperties $pValue)  
        $this->_properties =  $pValue;  
     * @return PHPExcel_DocumentSecurity   
     * @param PHPExcel_DocumentSecurity    $pValue   
    public function setSecurity(PHPExcel_DocumentSecurity $pValue)  
        $this->_security =  $pValue;  
     * @return PHPExcel_Worksheet   
        return $this->_workSheetCollection[$this->_activeSheetIndex];  
     * Create sheet and add it to this workbook  
     * @return PHPExcel_Worksheet   
        $this->addSheet($newSheet, $iSheetIndex);  
     * @param PHPExcel_Worksheet $pSheet   
     * @param int|null$iSheetIndex Index where sheet should go (0,1,..., or null for last)  
     * @return PHPExcel_Worksheet   
    public function addSheet(PHPExcel_Worksheet $pSheet =  null, $iSheetIndex =  null)  
            $this->_workSheetCollection[] =  $pSheet;  
            // Insert the sheet at the requested index  
                $this->_workSheetCollection,  
            // Adjust active sheet index if necessary  
            if ($this->_activeSheetIndex >=  $iSheetIndex) {  
                ++ $this->_activeSheetIndex; 
     * @param int $pIndex Active sheet index  
        if ($pIndex >  count($this->_workSheetCollection) -  1) {  
            throw  new Exception("Sheet index is out of bounds."); 
     * @param int $pIndex Sheet index  
     * @return PHPExcel_Worksheet   
        if ($pIndex >  count($this->_workSheetCollection) -  1) {  
            throw  new Exception("Sheet index is out of bounds."); 
            return $this->_workSheetCollection[$pIndex];  
     * @return PHPExcel_Worksheet[]   
        return $this->_workSheetCollection;  
     * @param string $pName Sheet name  
     * @return PHPExcel_Worksheet   
        $worksheetCount =  count($this->_workSheetCollection);  
        for ($i =  0; $i <  $worksheetCount; ++ $i) {  
            if ($this->_workSheetCollection[$i]->getTitle() ==  $pName) {  
                return $this->_workSheetCollection[$i];  
     * @param PHPExcel_Worksheet $pSheet   
    public function getIndex(PHPExcel_Worksheet $pSheet)  
        foreach ($this->_workSheetCollection as $key =>  $value) {  
            if ($value->getHashCode() ==  $pSheet->getHashCode()) {  
     * Set index for sheet by sheet name.  
     * @param string $sheetName Sheet name to modify index for  
     * @param int $newIndex New index for the sheet  
     * @return New sheet index  
            $this->_workSheetCollection,  
            $this->_workSheetCollection,  
        return count($this->_workSheetCollection);  
     * @return int Active sheet index  
        return $this->_activeSheetIndex;  
     * @param int $pIndex Active sheet index  
     * @return PHPExcel_Worksheet   
        if ($pIndex >  count($this->_workSheetCollection) -  1) {  
            throw  new Exception("Active sheet index is out of bounds."); 
            $this->_activeSheetIndex =  $pIndex;  
     * Set active sheet index by name  
     * @param string $pValue Sheet title  
     * @return PHPExcel_Worksheet   
        throw  new Exception('Workbook does not contain sheet:' .  $pValue); 
        for ($i =  0; $i <  $worksheetCount; ++ $i) {  
     * @param PHPExcel_Worksheet $pSheet External sheet to add  
     * @param int|null$iSheetIndex Index where sheet should go (0,1,..., or null for last)  
     * @return PHPExcel_Worksheet   
    public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex =  null) {  
            throw  new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first. "); 
        // count how many cellXfs there are in this workbook currently, we will need this below  
        $countCellXfs =  count($this->_cellXfCollection);  
        // copy all the shared cellXfs from the external workbook and append them to the current  
        foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {  
        // move sheet to this workbook  
        $pSheet->rebindParent($this);  
        foreach ($pSheet->getCellCollection(false) as $cellID) {  
            $cell =  $pSheet->getCell($cellID);  
            $cell->setXfIndex( $cell->getXfIndex() +  $countCellXfs );  
        return $this->addSheet($pSheet, $iSheetIndex);  
     * @return PHPExcel_NamedRange[]   
        return $this->_namedRanges;  
     * @param PHPExcel_NamedRange $namedRange   
        if ($namedRange->getScope() ==  null) {  
            $this->_namedRanges[$namedRange->getName()] =  $namedRange;  
            $this->_namedRanges[$namedRange->getScope()->getTitle(). '!'. $namedRange->getName()] =  $namedRange;  
     * @param string $namedRange   
     * @param PHPExcel_Worksheet|null$pSheet Scope. Use null for global scope  
     * @return PHPExcel_NamedRange|null  
    public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet =  null) {  
        if ($namedRange !=  '' &&  !is_null($namedRange)) {  
            // first look for global defined name  
            if (isset ($this->_namedRanges[$namedRange])) {  
                $returnValue =  $this->_namedRanges[$namedRange];  
            // then look for local defined name (has priority over global defined name if both names exist)  
            if (!is_null($pSheet) && isset ($this->_namedRanges[$pSheet->getTitle() .  '!' .  $namedRange])) {  
                $returnValue =  $this->_namedRanges[$pSheet->getTitle() .  '!' .  $namedRange];  
     * @param string $namedRange   
     * @param PHPExcel_Worksheet|null$pSheet. Scope. Use null for global scope.  
    public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet =  null) {  
            if (isset ($this->_namedRanges[$namedRange])) {  
                unset ($this->_namedRanges[$namedRange]); 
            if (isset ($this->_namedRanges[$pSheet->getTitle() .  '!' .  $namedRange])) {  
                unset ($this->_namedRanges[$pSheet->getTitle() .  '!' .  $namedRange]); 
     * @return PHPExcel_WorksheetIterator   
     * Copy workbook (!= clone!)  
        $worksheetCount =  count($this->_workSheetCollection);  
        for ($i =  0; $i <  $worksheetCount; ++ $i) {  
            $this->_workSheetCollection[$i] =  $this->_workSheetCollection[$i]->copy();  
            $this->_workSheetCollection[$i]->rebindParent($this);  
     * Implement PHP __clone to create a deep clone, not just a shallow copy.  
        foreach($this as $key =>  $val) {  
     * Get the workbook collection of cellXfs  
     * @return PHPExcel_Style[]   
        return $this->_cellXfCollection;  
        return $this->_cellXfCollection[$pIndex];  
     * Get cellXf by hash code  
     * @return PHPExcel_Style|false  
        foreach ($this->_cellXfCollection as $cellXf) {  
            if ($cellXf->getHashCode() ==  $pValue) {  
        if (isset ($this->_cellXfCollection[0])) {  
            return $this->_cellXfCollection[0];  
        throw  new Exception('No default style found for this workbook'); 
     * Add a cellXf to the workbook  
    public function addCellXf(PHPExcel_Style $style)  
        $this->_cellXfCollection[] =  $style;  
        $style->setIndex(count($this->_cellXfCollection) -  1);  
     * Remove cellXf by index. It is ensured that all cells get their xf index updated.  
     * @param int $pIndex Index to cellXf  
        if ($pIndex >  count($this->_cellXfCollection) -  1) {  
            throw  new Exception("CellXf index is out of bounds."); 
            // first remove the cellXf  
            // then update cellXf indexes for cells  
            foreach ($this->_workSheetCollection as $worksheet) {  
                foreach ($worksheet->getCellCollection(false) as $cellID) {  
                    $cell =  $worksheet->getCell($cellID);  
                    $xfIndex =  $cell->getXfIndex();  
                    if ($xfIndex >  $pIndex ) {  
                        // decrease xf index by 1  
                        $cell->setXfIndex($xfIndex -  1);  
                    } else if ($xfIndex ==  $pIndex) {  
                        // set to default xf index 0  
     * Get the cellXf supervisor  
        return $this->_cellXfSupervisor;  
     * Get the workbook collection of cellStyleXfs  
     * @return PHPExcel_Style[]   
        return $this->_cellStyleXfCollection;  
     * Get cellStyleXf by index  
        return $this->_cellStyleXfCollection[$pIndex];  
     * Get cellStyleXf by hash code  
     * @return PHPExcel_Style|false  
        foreach ($this->_cellXfStyleCollection as $cellStyleXf) {  
            if ($cellStyleXf->getHashCode() ==  $pValue) {  
     * Add a cellStyleXf to the workbook  
     * @param PHPExcel_Style $pStyle   
        $this->_cellStyleXfCollection[] =  $pStyle;  
        $pStyle->setIndex(count($this->_cellStyleXfCollection) -  1);  
     * Remove cellStyleXf by index  
        if ($pIndex >  count($this->_cellStyleXfCollection) -  1) {  
            throw  new Exception("CellStyleXf index is out of bounds."); 
     * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells  
     * and columns in the workbook  
        // how many references are there to each cellXf ?  
        $countReferencesCellXf =  array();  
        foreach ($this->_cellXfCollection as $index =>  $cellXf) {  
            $countReferencesCellXf[$index] =  0;  
            foreach ($sheet->getCellCollection(false) as $cellID) {  
                $cell =  $sheet->getCell($cellID);  
                ++ $countReferencesCellXf[$cell->getXfIndex()]; 
            foreach ($sheet->getRowDimensions() as $rowDimension) {  
                if ($rowDimension->getXfIndex() !==  null) {  
                    ++ $countReferencesCellXf[$rowDimension->getXfIndex()]; 
            // from column dimensions  
            foreach ($sheet->getColumnDimensions() as $columnDimension) {  
                ++ $countReferencesCellXf[$columnDimension->getXfIndex()]; 
        // remove cellXfs without references and create mapping so we can update xfIndex  
        // for all cells and columns  
        foreach ($this->_cellXfCollection as $index =>  $cellXf) {  
            if ($countReferencesCellXf[$index] >  0 ||  $index ==  0) { // we must never remove the first cellXf  
                unset ($this->_cellXfCollection[$index]); 
            $map[$index] =  $countNeededCellXfs -  1;  
        $this->_cellXfCollection =  array_values($this->_cellXfCollection);  
        // update the index for all cellXfs  
        foreach ($this->_cellXfCollection as $i =>  $cellXf) {  
        // make sure there is always at least one cellXf (there should be)  
        if (count($this->_cellXfCollection) ==  0) {  
        // update the xfIndex for all cells, row dimensions, column dimensions  
            foreach ($sheet->getCellCollection(false) as $cellID) {  
                $cell =  $sheet->getCell($cellID);  
                $cell->setXfIndex( $map[$cell->getXfIndex()] );  
            // for all row dimensions  
            foreach ($sheet->getRowDimensions() as $rowDimension) {  
                if ($rowDimension->getXfIndex() !==  null) {  
                    $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] );  
            // for all column dimensions  
            foreach ($sheet->getColumnDimensions() as $columnDimension) {  
                $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] );  
        // also do garbage collection for all the sheets  
 
 
        
       |