Source for file PHPExcel.php
Documentation is available at PHPExcel.php
* Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.2, 2010-01-11
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/');
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
/** PHPExcel_DocumentProperties */
require_once PHPEXCEL_ROOT . 'PHPExcel/DocumentProperties.php';
/** PHPExcel_DocumentSecurity */
require_once PHPEXCEL_ROOT . 'PHPExcel/DocumentSecurity.php';
/** PHPExcel_Worksheet */
require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
/** PHPExcel_Shared_ZipStreamWrapper */
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/ZipStreamWrapper.php';
/** PHPExcel_NamedRange */
require_once PHPEXCEL_ROOT . 'PHPExcel/NamedRange.php';
/** PHPExcel_WorksheetIterator */
require_once PHPEXCEL_ROOT . 'PHPExcel/WorksheetIterator.php';
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
* @var PHPExcel_DocumentProperties
* @var PHPExcel_DocumentSecurity
* Collection of Worksheet objects
* @var PHPExcel_Worksheet[]
* @var PHPExcel_NamedRange[]
* Create a new PHPExcel with one Worksheet
// Initialise worksheet collection and add one worksheet
// Create document properties
// Create document security
// Create the cellXf supervisor
// Create the default style
* @return PHPExcel_DocumentProperties
* @param PHPExcel_DocumentProperties $pValue
public function setProperties(PHPExcel_DocumentProperties $pValue)
* @return PHPExcel_DocumentSecurity
* @param PHPExcel_DocumentSecurity $pValue
public function setSecurity(PHPExcel_DocumentSecurity $pValue)
* @return PHPExcel_Worksheet
* 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)
// Insert the sheet at the requested index
// Adjust active sheet index if necessary
* @param int $pIndex Active sheet index
throw new Exception("Sheet index is out of bounds.");
* @param int $pIndex Sheet index
* @return PHPExcel_Worksheet
throw new Exception("Sheet index is out of bounds.");
* @return PHPExcel_Worksheet[]
* @param string $pName Sheet name
* @return PHPExcel_Worksheet
for ($i = 0; $i < $worksheetCount; ++ $i) {
* @param PHPExcel_Worksheet $pSheet
public function getIndex(PHPExcel_Worksheet $pSheet)
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
* @return int Active sheet index
* @param int $pIndex Active sheet index
* @return PHPExcel_Worksheet
throw new Exception("Active sheet index is out of bounds.");
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
// 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 $cell) {
$cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
return $this->addSheet($pSheet, $iSheetIndex);
* @return PHPExcel_NamedRange[]
* @param PHPExcel_NamedRange $namedRange
$this->_namedRanges[$namedRange->getWorksheet()->getTitle(). '!'. $namedRange->getName()] = $namedRange;
* @param string $namedRange
public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
if ($namedRange != '' && !is_null($namedRange)) {
$key = $pSheet->getTitle(). '!'. $namedRange;
if ($_namedRange->getName() == $namedRange) {
if ((!is_null($pSheet)) && ($_namedRange->getWorksheet()->getTitle() == $pSheet->getTitle())) {
$returnValue = $_namedRange;
* @param string $namedRange
public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
if ($namedRange != '' && !is_null($namedRange)) {
$key = $pSheet->getTitle(). '!'. $namedRange;
if ($_namedRange->getName() == $namedRange) {
if ((!is_null($pSheet)) && ($_namedRange->getWorksheet()->getTitle() == $pSheet->getTitle())) {
$key = $pSheet->getTitle(). '!'. $namedRange;
* @return PHPExcel_WorksheetIterator
* Copy workbook (!= clone!)
for ($i = 0; $i < $worksheetCount; ++ $i) {
* 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[]
* Get cellXf by hash code
* @return PHPExcel_Style|false
if ($cellXf->getHashCode() == $pValue) {
throw new Exception('No default style found for this workbook');
* Add a cellXf to the workbook
public function addCellXf(PHPExcel_Style $style)
* Remove cellXf by index. It is ensured that all cells get their xf index updated.
* @param int $pIndex Index to cellXf
throw new Exception("CellXf index is out of bounds.");
// first remove the cellXf
// then update cellXf indexes for cells
foreach ($worksheet->getCellCollection(false) as $cell) {
$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
* Get the workbook collection of cellStyleXfs
* @return PHPExcel_Style[]
* Get cellStyleXf by index
* 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
* Remove cellStyleXf by index
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();
$countReferencesCellXf[$index] = 0;
foreach ($sheet->getCellCollection(false) as $cell) {
++ $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
if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
$map[$index] = $countNeededCellXfs - 1;
// update the index for all cellXfs
echo $cellXf->setIndex($i);
// make sure there is always at least one cellXf (there should be)
// update the xfIndex for all cells, row dimensions, column dimensions
foreach ($sheet->getCellCollection(false) as $cell) {
$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
|