Source for file OOCalc.php
Documentation is available at OOCalc.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
* @package PHPExcel_Reader
* @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');
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
* or whether it should read both data and formatting
private $_readDataOnly = false;
* Restrict which sheets should be loaded?
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
private $_loadSheetsOnly = null;
private $_styles = array();
* PHPExcel_Reader_IReadFilter instance
* @var PHPExcel_Reader_IReadFilter
private $_readFilter = null;
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
* If false (the default) it will read data and formatting.
return $this->_readDataOnly;
* Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
* Set to false (the default) to advise the Reader to read both data and formatting for cells.
* @return PHPExcel_Reader_OOCalc
$this->_readDataOnly = $pValue;
* Get which sheets to load
* Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
* indicating that all worksheets in the workbook should be loaded.
return $this->_loadSheetsOnly;
* Set which sheets to load
* This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
* If NULL, then it tells the Reader to read all worksheets in the workbook
* @return PHPExcel_Reader_OOCalc
$this->_loadSheetsOnly = is_array($value) ?
* Tells the Reader to load all worksheets from the workbook.
* @return PHPExcel_Reader_OOCalc
$this->_loadSheetsOnly = null;
* @return PHPExcel_Reader_IReadFilter
return $this->_readFilter;
* @param PHPExcel_Reader_IReadFilter $pValue
* @return PHPExcel_Reader_OOCalc
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
* Create a new PHPExcel_Reader_OOCalc
* Can the current PHPExcel_Reader_IReader read the file?
* @param string $pFileName
public function canRead($pFilename)
// Check if zip class exists
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
if ($zip->open($pFilename) === true) {
// check if it is an OOXML archive
$mimeType = $zip->getFromName("mimetype");
return ($mimeType === 'application/vnd.oasis.opendocument.spreadsheet');
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
* @param string $pFilename
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
$worksheetNames = array();
if ($zip->open($pFilename) === true) {
$namespacesContent = $xml->getNamespaces(true);
$workbook = $xml->children($namespacesContent['office']);
foreach($workbook->body->spreadsheet as $workbookData) {
$workbookData = $workbookData->children($namespacesContent['table']);
foreach($workbookData->table as $worksheetDataSet) {
$worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
$worksheetNames[] = $worksheetDataAttributes['name'];
* Loads PHPExcel from file
* @param string $pFilename
public function load($pFilename)
// Load into this instance
private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
$styleAttributeValue = strtolower($styleAttributeValue);
foreach($styleList as $style) {
$styleAttributeValue = $style;
* Loads PHPExcel from file into PHPExcel instance
* @param string $pFilename
* @param PHPExcel $objPHPExcel
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
$timezoneObj = new DateTimeZone('Europe/London');
$GMT = new DateTimeZone('UTC');
if ($zip->open($pFilename) === true) {
// echo '<h1>Meta Information</h1>';
$namespacesMeta = $xml->getNamespaces(true);
// print_r($namespacesMeta);
$docProps = $objPHPExcel->getProperties();
$officeProperty = $xml->children($namespacesMeta['office']);
foreach($officeProperty as $officePropertyData) {
$officePropertyDC = array();
if (isset ($namespacesMeta['dc'])) {
$officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
foreach($officePropertyDC as $propertyName => $propertyValue) {
$docProps->setTitle($propertyValue);
$docProps->setSubject($propertyValue);
$docProps->setCreator($propertyValue);
$docProps->setLastModifiedBy($propertyValue);
$docProps->setCreated($creationDate);
$docProps->setModified($creationDate);
$docProps->setDescription($propertyValue);
$officePropertyMeta = array();
if (isset ($namespacesMeta['dc'])) {
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
foreach($officePropertyMeta as $propertyName => $propertyValue) {
$propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
$docProps->setCreator($propertyValue);
$docProps->setKeywords($propertyValue);
$docProps->setCreated($creationDate);
foreach ($propertyValueAttributes as $key => $value) {
$propertyValueName = (string) $value;
} elseif($key == 'value-type') {
$docProps->setCustomProperty($propertyValueName,$propertyValue,$propertyValueType);
// echo '<h1>Workbook Content</h1>';
$namespacesContent = $xml->getNamespaces(true);
// print_r($namespacesContent);
$workbook = $xml->children($namespacesContent['office']);
foreach($workbook->body->spreadsheet as $workbookData) {
$workbookData = $workbookData->children($namespacesContent['table']);
foreach($workbookData->table as $worksheetDataSet) {
$worksheetData = $worksheetDataSet->children($namespacesContent['table']);
// print_r($worksheetData);
$worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
// print_r($worksheetDataAttributes);
if ((isset ($this->_loadSheetsOnly)) && (isset ($worksheetDataAttributes['name'])) &&
(!in_array($worksheetDataAttributes['name'], $this->_loadSheetsOnly))) {
// echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($worksheetID);
if (isset ($worksheetDataAttributes['name'])) {
$worksheetName = (string) $worksheetDataAttributes['name'];
$objPHPExcel->getActiveSheet()->setTitle($worksheetName);
foreach($worksheetData as $key => $rowData) {
// echo '<b>'.$key.'</b><br />';
case 'table-header-rows':
foreach ($rowData as $key=> $cellData) {
foreach($rowData as $key => $cellData) {
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
// echo '<b>'.$columnID.$rowID.'</b><br />';
$cellDataText = $cellData->children($namespacesContent['text']);
$cellDataOffice = $cellData->children($namespacesContent['office']);
$cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
$cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
// echo 'Office Attributes: ';
// print_r($cellDataOfficeAttributes);
// echo '<br />Table Attributes: ';
// print_r($cellDataTableAttributes);
// echo '<br />Cell Data Text';
// print_r($cellDataText);
$type = $formatting = $hyperlink = null;
$hasCalculatedValue = false;
if (isset ($cellDataTableAttributes['formula'])) {
$cellDataFormula = $cellDataTableAttributes['formula'];
$hasCalculatedValue = true;
if (isset ($cellDataOffice->annotation)) {
// echo 'Cell has comment<br />';
$annotationText = $cellDataOffice->annotation->children($namespacesContent['text']);
foreach($annotationText as $t) {
foreach($t->span as $text) {
$textArray[] = (string) $text;
$objPHPExcel->getActiveSheet()->getComment( $columnID. $rowID )
// ->setAuthor( $author )
->setText($this->_parseRichText($text) );
if (isset ($cellDataText->p)) {
// echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
switch ($cellDataOfficeAttributes['value-type']) {
$dataValue = $cellDataText->p;
if (isset ($dataValue->a)) {
$dataValue = $dataValue->a;
$cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
$hyperlink = $cellXLinkAttributes['href'];
$dataValue = ($cellDataText->p == 'TRUE') ? True : False;
$dataValue = (float) $cellDataOfficeAttributes['value'];
if (floor($dataValue) == $dataValue) {
$dataValue = (integer) $dataValue;
$dateObj = new DateTime($cellDataOfficeAttributes['date-value'], $GMT);
$dateObj->setTimeZone($timezoneObj);
list ($year,$month,$day,$hour,$minute,$second) = explode(' ',$dateObj->format('Y m d H i s'));
if ($dataValue != floor($dataValue)) {
// echo 'Data value is '.$dataValue.'<br />';
// if (!is_null($hyperlink)) {
// echo 'Hyperlink is '.$hyperlink.'<br />';
if ($hasCalculatedValue) {
// echo 'Formula: '.$cellDataFormula.'<br />';
$cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+ 1);
$temp = explode('"',$cellDataFormula);
foreach($temp as &$value) {
// Only replace in alternate array entries (i.e. non-quoted blocks)
$value = preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value);
// Then rebuild the formula string
$cellDataFormula = implode('"',$temp);
// echo 'Adjusted Formula: '.$cellDataFormula.'<br />';
$objPHPExcel->getActiveSheet()->getCell($columnID. $rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue),$type);
if ($hasCalculatedValue) {
// echo 'Forumla result is '.$dataValue.'<br />';
$objPHPExcel->getActiveSheet()->getCell($columnID. $rowID)->setCalculatedValue($dataValue);
if (($cellDataOfficeAttributes['value-type'] == 'date') ||
($cellDataOfficeAttributes['value-type'] == 'time')) {
$objPHPExcel->getActiveSheet()->getStyle($columnID. $rowID)->getNumberFormat()->setFormatCode($formatting);
$objPHPExcel->getActiveSheet()->getCell($columnID. $rowID)->getHyperlink()->setUrl($hyperlink);
if ((isset ($cellDataTableAttributes['number-columns-spanned'])) || (isset ($cellDataTableAttributes['number-rows-spanned']))) {
if (isset ($cellDataTableAttributes['number-columns-spanned'])) {
if (isset ($cellDataTableAttributes['number-rows-spanned'])) {
$rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1;
$cellRange = $columnID. $rowID. ':'. $columnTo. $rowTo;
$objPHPExcel->getActiveSheet()->mergeCells($cellRange);
if (isset ($cellDataTableAttributes['number-columns-repeated'])) {
// echo 'Repeated '.$cellDataTableAttributes['number-columns-repeated'].' times<br />';
private function _parseRichText($is = '') {
|