Source for file OOCalc.php
Documentation is available at OOCalc.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
* @package PHPExcel_Reader
* @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.3c, 2010-06-01
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
// check mbstring.func_overload
if (ini_get('mbstring.func_overload') & 2) {
throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
private $_readDataOnly = false;
* Restict which sheets should be loaded?
private $_loadSheetsOnly = null;
private $_styles = array();
* PHPExcel_Reader_IReadFilter instance
* @var PHPExcel_Reader_IReadFilter
private $_readFilter = null;
return $this->_readDataOnly;
* @return PHPExcel_Reader_Excel2007
$this->_readDataOnly = $pValue;
* Get which sheets to load
return $this->_loadSheetsOnly;
* Set which sheets to load
* @return PHPExcel_Reader_Excel2007
$this->_loadSheetsOnly = is_array($value) ?
* @return PHPExcel_Reader_Excel2007
$this->_loadSheetsOnly = null;
* @return PHPExcel_Reader_IReadFilter
return $this->_readFilter;
* @param PHPExcel_Reader_IReadFilter $pValue
* @return PHPExcel_Reader_Excel2007
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');
* 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.");
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) {
// echo $propertyName.' = '.$propertyValue.'<hr />';
$docProps->setTitle($propertyValue);
$docProps->setSubject($propertyValue);
$docProps->setCreator($propertyValue);
$docProps->setCreated($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']);
// echo $propertyName.' = '.$propertyValue.'<br />';
// foreach ($propertyValueAttributes as $key => $value) {
// echo $key.' = '.$value.'<br />';
$docProps->setKeywords($propertyValue);
// 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 />';
foreach($rowData as $key => $cellData) {
// echo '<b>'.$columnID.$rowID.'</b><br />';
$cellDataText = $cellData->children($namespacesContent['text']);
$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 ($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 = date_create($cellDataOfficeAttributes['date-value']);
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 $key => &$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 />';
return $this->_sheetIndex;
* @param int $pValue Sheet index
* @return PHPExcel_Reader_OOCalc
$this->_sheetIndex = $pValue;
|