PHPExcel_Reader
[ class tree: PHPExcel_Reader ] [ index: PHPExcel_Reader ] [ all elements ]

Source for file OOCalc.php

Documentation is available at OOCalc.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2010 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_Reader
  23.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36.     // check mbstring.func_overload
  37.     if (ini_get('mbstring.func_overload'2{
  38.         throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
  39.     }
  40. }
  41.  
  42. /**
  43.  * PHPExcel_Reader_OOCalc
  44.  *
  45.  * @category   PHPExcel
  46.  * @package    PHPExcel_Reader
  47.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  48.  */
  49. class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
  50. {
  51.     /**
  52.      * Read data only?
  53.      *
  54.      * @var boolean 
  55.      */
  56.     private $_readDataOnly false;
  57.  
  58.     /**
  59.      * Restict which sheets should be loaded?
  60.      *
  61.      * @var array 
  62.      */
  63.     private $_loadSheetsOnly null;
  64.  
  65.     /**
  66.      * Sheet index to read
  67.      *
  68.      * @var int 
  69.      */
  70.     private $_sheetIndex;
  71.  
  72.     /**
  73.      * Formats
  74.      *
  75.      * @var array 
  76.      */
  77.     private $_styles array();
  78.  
  79.     /**
  80.      * PHPExcel_Reader_IReadFilter instance
  81.      *
  82.      * @var PHPExcel_Reader_IReadFilter 
  83.      */
  84.     private $_readFilter null;
  85.  
  86.  
  87.     /**
  88.      * Read data only?
  89.      *
  90.      * @return boolean 
  91.      */
  92.     public function getReadDataOnly({
  93.         return $this->_readDataOnly;
  94.     }
  95.  
  96.     /**
  97.      * Set read data only
  98.      *
  99.      * @param boolean $pValue 
  100.      * @return PHPExcel_Reader_Excel2007 
  101.      */
  102.     public function setReadDataOnly($pValue false{
  103.         $this->_readDataOnly $pValue;
  104.         return $this;
  105.     }
  106.  
  107.     /**
  108.      * Get which sheets to load
  109.      *
  110.      * @return mixed 
  111.      */
  112.     public function getLoadSheetsOnly()
  113.     {
  114.         return $this->_loadSheetsOnly;
  115.     }
  116.  
  117.     /**
  118.      * Set which sheets to load
  119.      *
  120.      * @param mixed $value 
  121.      * @return PHPExcel_Reader_Excel2007 
  122.      */
  123.     public function setLoadSheetsOnly($value null)
  124.     {
  125.         $this->_loadSheetsOnly is_array($value?
  126.             $value array($value);
  127.         return $this;
  128.     }
  129.  
  130.     /**
  131.      * Set all sheets to load
  132.      *
  133.      * @return PHPExcel_Reader_Excel2007 
  134.      */
  135.     public function setLoadAllSheets()
  136.     {
  137.         $this->_loadSheetsOnly null;
  138.         return $this;
  139.     }
  140.  
  141.     /**
  142.      * Read filter
  143.      *
  144.      * @return PHPExcel_Reader_IReadFilter 
  145.      */
  146.     public function getReadFilter({
  147.         return $this->_readFilter;
  148.     }
  149.  
  150.     /**
  151.      * Set read filter
  152.      *
  153.      * @param PHPExcel_Reader_IReadFilter $pValue 
  154.      * @return PHPExcel_Reader_Excel2007 
  155.      */
  156.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  157.         $this->_readFilter $pValue;
  158.         return $this;
  159.     }
  160.  
  161.     /**
  162.      * Create a new PHPExcel_Reader_OOCalc
  163.      */
  164.     public function __construct({
  165.         $this->_sheetIndex     0;
  166.         $this->_readFilter     new PHPExcel_Reader_DefaultReadFilter();
  167.     }
  168.  
  169.     /**
  170.      * Can the current PHPExcel_Reader_IReader read the file?
  171.      *
  172.      * @param     string         $pFileName 
  173.      * @return     boolean 
  174.      */
  175.     public function canRead($pFilename)
  176.     {
  177.         // Check if zip class exists
  178.         if (!class_exists('ZipArchive')) {
  179.             return false;
  180.         }
  181.  
  182.         // Check if file exists
  183.         if (!file_exists($pFilename)) {
  184.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  185.         }
  186.  
  187.         // Load file
  188.         $zip new ZipArchive;
  189.         if ($zip->open($pFilename=== true{
  190.             // check if it is an OOXML archive
  191.             $mimeType $zip->getFromName("mimetype");
  192.  
  193.             $zip->close();
  194.  
  195.             return ($mimeType === 'application/vnd.oasis.opendocument.spreadsheet');
  196.         }
  197.  
  198.         return false;
  199.     }
  200.  
  201.     /**
  202.      * Loads PHPExcel from file
  203.      *
  204.      * @param     string         $pFilename 
  205.      * @return     PHPExcel 
  206.      * @throws     Exception
  207.      */
  208.     public function load($pFilename)
  209.     {
  210.         // Create new PHPExcel
  211.         $objPHPExcel new PHPExcel();
  212.  
  213.         // Load into this instance
  214.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  215.     }
  216.  
  217.     private static function identifyFixedStyleValue($styleList,&$styleAttributeValue{
  218.         $styleAttributeValue strtolower($styleAttributeValue);
  219.         foreach($styleList as $style{
  220.             if ($styleAttributeValue == strtolower($style)) {
  221.                 $styleAttributeValue $style;
  222.                 return true;
  223.             }
  224.         }
  225.         return false;
  226.     }
  227.  
  228.     /**
  229.      * Loads PHPExcel from file into PHPExcel instance
  230.      *
  231.      * @param     string         $pFilename 
  232.      * @param    PHPExcel    $objPHPExcel 
  233.      * @return     PHPExcel 
  234.      * @throws     Exception
  235.      */
  236.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  237.     {
  238.         // Check if file exists
  239.         if (!file_exists($pFilename)) {
  240.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  241.         }
  242.  
  243.         $zip new ZipArchive;
  244.         if ($zip->open($pFilename=== true{
  245. //            echo '<h1>Meta Information</h1>';
  246.             $xml simplexml_load_string($zip->getFromName("meta.xml"));
  247.             $namespacesMeta $xml->getNamespaces(true);
  248. //            echo '<pre>';
  249. //            print_r($namespacesMeta);
  250. //            echo '</pre><hr />';
  251.  
  252.             $docProps $objPHPExcel->getProperties();
  253.             $officeProperty $xml->children($namespacesMeta['office']);
  254.             foreach($officeProperty as $officePropertyData{
  255.                 $officePropertyDC array();
  256.                 if (isset($namespacesMeta['dc'])) {
  257.                     $officePropertyDC $officePropertyData->children($namespacesMeta['dc']);
  258.                 }
  259.                 foreach($officePropertyDC as $propertyName => $propertyValue{
  260. //                    echo $propertyName.' = '.$propertyValue.'<hr />';
  261.  
  262.                     switch ($propertyName{
  263.                         case 'title' :
  264.                                 $docProps->setTitle($propertyValue);
  265.                                 break;
  266.                         case 'subject' :
  267.                                 $docProps->setSubject($propertyValue);
  268.                                 break;
  269.                         case 'creator' :
  270.                                 $docProps->setCreator($propertyValue);
  271.                                 break;
  272.                         case 'date' :
  273.                                 $creationDate strtotime($propertyValue);
  274.                                 $docProps->setCreated($creationDate);
  275.                                 break;
  276.                         case 'description' :
  277.                                 $docProps->setDescription($propertyValue);
  278.                                 break;
  279.                     }
  280.                 }
  281.                 $officePropertyMeta array();
  282.                 if (isset($namespacesMeta['dc'])) {
  283.                     $officePropertyMeta $officePropertyData->children($namespacesMeta['meta']);
  284.                 }
  285.                 foreach($officePropertyMeta as $propertyName => $propertyValue{
  286.                     $propertyValueAttributes $propertyValue->attributes($namespacesMeta['meta']);
  287.  
  288. //                    echo $propertyName.' = '.$propertyValue.'<br />';
  289. //                    foreach ($propertyValueAttributes as $key => $value) {
  290. //                        echo $key.' = '.$value.'<br />';
  291. //                    }
  292. //                    echo '<hr />';
  293. //
  294.                     switch ($propertyName{
  295.                         case 'keyword' :
  296.                                 $docProps->setKeywords($propertyValue);
  297.                                 break;
  298.                     }
  299.                 }
  300.             }
  301.  
  302.  
  303. //            echo '<h1>Workbook Content</h1>';
  304.             $xml simplexml_load_string($zip->getFromName("content.xml"));
  305.             $namespacesContent $xml->getNamespaces(true);
  306. //            echo '<pre>';
  307. //            print_r($namespacesContent);
  308. //            echo '</pre><hr />';
  309.  
  310.             $workbook $xml->children($namespacesContent['office']);
  311.             foreach($workbook->body->spreadsheet as $workbookData{
  312.                 $workbookData $workbookData->children($namespacesContent['table']);
  313.                 $worksheetID 0;
  314.                 foreach($workbookData->table as $worksheetDataSet{
  315.                     $worksheetData $worksheetDataSet->children($namespacesContent['table']);
  316. //                    print_r($worksheetData);
  317. //                    echo '<br />';
  318.                     $worksheetDataAttributes $worksheetDataSet->attributes($namespacesContent['table']);
  319. //                    print_r($worksheetDataAttributes);
  320. //                    echo '<br />';
  321.                     if ((isset($this->_loadSheetsOnly)) && (isset($worksheetDataAttributes['name'])) &&
  322.                         (!in_array($worksheetDataAttributes['name']$this->_loadSheetsOnly))) {
  323.                         continue;
  324.                     }
  325.  
  326. //                    echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
  327.                     // Create new Worksheet
  328.                     $objPHPExcel->createSheet();
  329.                     $objPHPExcel->setActiveSheetIndex($worksheetID);
  330.                     if (isset($worksheetDataAttributes['name'])) {
  331.                         $worksheetName = (string) $worksheetDataAttributes['name'];
  332.                         $objPHPExcel->getActiveSheet()->setTitle($worksheetName);
  333.                     }
  334.  
  335.                     $rowID 1;
  336.                     foreach($worksheetData as $key => $rowData{
  337. //                        echo '<b>'.$key.'</b><br />';
  338.                         switch ($key{
  339.                             case 'table-row' :
  340.                                 $columnID 'A';
  341.                                 foreach($rowData as $key => $cellData{
  342. //                                    echo '<b>'.$columnID.$rowID.'</b><br />';
  343.                                     $cellDataText $cellData->children($namespacesContent['text']);
  344.                                     $cellDataOfficeAttributes $cellData->attributes($namespacesContent['office']);
  345.                                     $cellDataTableAttributes $cellData->attributes($namespacesContent['table']);
  346.  
  347. //                                    echo 'Office Attributes: ';
  348. //                                    print_r($cellDataOfficeAttributes);
  349. //                                    echo '<br />Table Attributes: ';
  350. //                                    print_r($cellDataTableAttributes);
  351. //                                    echo '<br />Cell Data Text';
  352. //                                    print_r($cellDataText);
  353. //                                    echo '<br />';
  354. //
  355.                                     $type $formatting $hyperlink null;
  356.                                     $hasCalculatedValue false;
  357.                                     $cellDataFormula '';
  358.                                     if (isset($cellDataTableAttributes['formula'])) {
  359.                                         $cellDataFormula $cellDataTableAttributes['formula'];
  360.                                         $hasCalculatedValue true;
  361.                                     }
  362.  
  363.                                     if (isset($cellDataText->p)) {
  364. //                                        echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
  365.                                         switch ($cellDataOfficeAttributes['value-type']{
  366.                                             case 'string' :
  367.                                                     $type PHPExcel_Cell_DataType::TYPE_STRING;
  368.                                                     $dataValue $cellDataText->p;
  369.                                                     if (isset($dataValue->a)) {
  370.                                                         $dataValue $dataValue->a;
  371.                                                         $cellXLinkAttributes $dataValue->attributes($namespacesContent['xlink']);
  372.                                                         $hyperlink $cellXLinkAttributes['href'];
  373.                                                     }
  374.                                                     break;
  375.                                             case 'boolean' :
  376.                                                     $type PHPExcel_Cell_DataType::TYPE_BOOL;
  377.                                                     $dataValue ($cellDataText->== 'TRUE'True False;
  378.                                                     break;
  379.                                             case 'float' :
  380.                                                     $type PHPExcel_Cell_DataType::TYPE_NUMERIC;
  381.                                                     $dataValue = (float) $cellDataOfficeAttributes['value'];
  382.                                                     if (floor($dataValue== $dataValue{
  383.                                                         $dataValue = (integer) $dataValue;
  384.                                                     }
  385.                                                     break;
  386.                                             case 'date' :
  387.                                                     $type PHPExcel_Cell_DataType::TYPE_NUMERIC;
  388.                                                     $dateObj date_create($cellDataOfficeAttributes['date-value']);
  389.                                                     list($year,$month,$day,$hour,$minute,$secondexplode(' ',$dateObj->format('Y m d H i s'));
  390.                                                     $dataValue PHPExcel_Shared_Date::FormattedPHPToExcel($year,$month,$day,$hour,$minute,$second);
  391.                                                     if ($dataValue != floor($dataValue)) {
  392.                                                         $formatting PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15.' '.PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  393.                                                     else {
  394.                                                         $formatting PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15;
  395.                                                     }
  396.                                                     break;
  397.                                             case 'time' :
  398.                                                     $type PHPExcel_Cell_DataType::TYPE_NUMERIC;
  399.                                                     $dataValue PHPExcel_Shared_Date::PHPToExcel(strtotime('01-01-1970 '.implode(':',sscanf($cellDataOfficeAttributes['time-value'],'PT%dH%dM%dS'))));
  400.                                                     $formatting PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  401.                                                     break;
  402.                                         }
  403. //                                        echo 'Data value is '.$dataValue.'<br />';
  404. //                                        if (!is_null($hyperlink)) {
  405. //                                            echo 'Hyperlink is '.$hyperlink.'<br />';
  406. //                                        }
  407.                                     }
  408.  
  409.                                     if ($hasCalculatedValue{
  410.                                         $type PHPExcel_Cell_DataType::TYPE_FORMULA;
  411. //                                        echo 'Formula: '.$cellDataFormula.'<br />';
  412.                                         $cellDataFormula substr($cellDataFormula,strpos($cellDataFormula,':=')+1);
  413.                                         $temp explode('"',$cellDataFormula);
  414.                                         foreach($temp as $key => &$value{
  415.                                             //    Only replace in alternate array entries (i.e. non-quoted blocks)
  416.                                             if (($key 2== 0{
  417.                                                 $value preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value);
  418.                                                 $value preg_replace('/\[\.(.*)\]/Ui','$1',$value);
  419.                                                 $value PHPExcel_Calculation::_translateSeparator(';',',',$value);
  420.                                             }
  421.                                         }
  422.                                         unset($value);
  423.                                         //    Then rebuild the formula string
  424.                                         $cellDataFormula implode('"',$temp);
  425. //                                        echo 'Adjusted Formula: '.$cellDataFormula.'<br />';
  426.                                     }
  427.  
  428.                                     if (!is_null($type)) {
  429.                                         $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue$cellDataFormula $dataValue),$type);
  430.                                         if ($hasCalculatedValue{
  431. //                                            echo 'Forumla result is '.$dataValue.'<br />';
  432.                                             $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($dataValue);
  433.                                         }
  434.                                         if (($cellDataOfficeAttributes['value-type'== 'date'||
  435.                                             ($cellDataOfficeAttributes['value-type'== 'time')) {
  436.                                             $objPHPExcel->getActiveSheet()->getStyle($columnID.$rowID)->getNumberFormat()->setFormatCode($formatting);
  437.                                         }
  438.                                         if (!is_null($hyperlink)) {
  439.                                             $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->getHyperlink()->setUrl($hyperlink);
  440.                                         }
  441.                                     }
  442.  
  443.                                     //    Merged cells
  444.                                     if ((isset($cellDataTableAttributes['number-columns-spanned'])) || (isset($cellDataTableAttributes['number-rows-spanned']))) {
  445.                                         $columnTo $columnID;
  446.                                         if (isset($cellDataTableAttributes['number-columns-spanned'])) {
  447.                                             $columnTo PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID$cellDataTableAttributes['number-columns-spanned'-2);
  448.                                         }
  449.                                         $rowTo $rowID;
  450.                                         if (isset($cellDataTableAttributes['number-rows-spanned'])) {
  451.                                             $rowTo $rowTo $cellDataTableAttributes['number-rows-spanned'1;
  452.                                         }
  453.                                         $cellRange $columnID.$rowID.':'.$columnTo.$rowTo;
  454.                                         $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
  455.                                     }
  456.  
  457.                                     if (isset($cellDataTableAttributes['number-columns-repeated'])) {
  458. //                                        echo 'Repeated '.$cellDataTableAttributes['number-columns-repeated'].' times<br />';
  459.                                         $columnID PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID$cellDataTableAttributes['number-columns-repeated'2);
  460.                                     }
  461.                                     ++$columnID;
  462.                                 }
  463.                                 ++$rowID;
  464.                                 break;
  465.                         }
  466.                     }
  467.                     ++$worksheetID;
  468.                 }
  469.             }
  470.  
  471.         }
  472.  
  473.         // Return
  474.         return $objPHPExcel;
  475.     }
  476.  
  477.     /**
  478.      * Get sheet index
  479.      *
  480.      * @return int 
  481.      */
  482.     public function getSheetIndex({
  483.         return $this->_sheetIndex;
  484.     }
  485.  
  486.     /**
  487.      * Set sheet index
  488.      *
  489.      * @param    int        $pValue        Sheet index
  490.      * @return PHPExcel_Reader_OOCalc 
  491.      */
  492.     public function setSheetIndex($pValue 0{
  493.         $this->_sheetIndex $pValue;
  494.         return $this;
  495.     }
  496. }

Documentation generated on Tue, 01 Jun 2010 17:05:27 +0200 by phpDocumentor 1.4.3