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

Documentation generated on Mon, 11 Jan 2010 08:12:35 +0100 by phpDocumentor 1.4.1