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

Source for file Excel2007.php

Documentation is available at Excel2007.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 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 tshhe 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  26.  */
  27.  
  28.  
  29. /** PHPExcel */
  30. require_once 'PHPExcel.php';
  31.  
  32. /** PHPExcel_Reader_IReader */
  33. require_once 'PHPExcel/Reader/IReader.php';
  34.  
  35. /** PHPExcel_Worksheet */
  36. require_once 'PHPExcel/Worksheet.php';
  37.  
  38. /** PHPExcel_Cell */
  39. require_once 'PHPExcel/Cell.php';
  40.  
  41. /** PHPExcel_Style */
  42. require_once 'PHPExcel/Style.php';
  43.  
  44. /** PHPExcel_Style_Borders */
  45. require_once 'PHPExcel/Style/Borders.php';
  46.  
  47. /** PHPExcel_Style_Conditional */
  48. require_once 'PHPExcel/Style/Conditional.php';
  49.  
  50. /** PHPExcel_Style_Protection */
  51. require_once 'PHPExcel/Style/Protection.php';
  52.  
  53. /** PHPExcel_Style_NumberFormat */
  54. require_once 'PHPExcel/Style/NumberFormat.php';
  55.  
  56. /** PHPExcel_Worksheet_BaseDrawing */
  57. require_once 'PHPExcel/Worksheet/BaseDrawing.php';
  58.  
  59. /** PHPExcel_Worksheet_Drawing */
  60. require_once 'PHPExcel/Worksheet/Drawing.php';
  61.  
  62. /** PHPExcel_Shared_Drawing */
  63. require_once 'PHPExcel/Shared/Drawing.php';
  64.  
  65. /** PHPExcel_Shared_Date */
  66. require_once 'PHPExcel/Shared/Date.php';
  67.  
  68. /** PHPExcel_Shared_File */
  69. require_once 'PHPExcel/Shared/File.php';
  70.  
  71. /** PHPExcel_Shared_String */
  72. require_once 'PHPExcel/Shared/String.php';
  73.  
  74. /** PHPExcel_ReferenceHelper */
  75. require_once 'PHPExcel/ReferenceHelper.php';
  76.  
  77.  
  78. /**
  79.  * PHPExcel_Reader_Excel2007
  80.  *
  81.  * @category   PHPExcel
  82.  * @package    PHPExcel_Reader
  83.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  84.  */
  85. class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
  86. {
  87.     /**
  88.      * Read data only?
  89.      *
  90.      * @var boolean 
  91.      */
  92.     private $_readDataOnly = false;
  93.     
  94.     /**
  95.      * Read data only?
  96.      *
  97.      * @return boolean 
  98.      */
  99.     public function getReadDataOnly() {
  100.         return $this->_readDataOnly;
  101.     }
  102.     
  103.     /**
  104.      * Set read data only
  105.      *
  106.      * @param boolean $pValue 
  107.      */
  108.     public function setReadDataOnly($pValue = false) {
  109.         $this->_readDataOnly = $pValue;
  110.     }
  111.     
  112.     /**
  113.      * Loads PHPExcel from file
  114.      *
  115.      * @param     string         $pFilename 
  116.      * @throws     Exception
  117.      */    
  118.     public function load($pFilename)
  119.     {
  120.         // Check if file exists
  121.         if (!file_exists($pFilename)) {
  122.             throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  123.         }
  124.             
  125.         // Initialisations
  126.         $excel = new PHPExcel;
  127.         $excel->removeSheetByIndex(0);
  128.         $zip = new ZipArchive;
  129.         $zip->open($pFilename);
  130.         
  131.         $rels = simplexml_load_string($zip->getFromName("_rels/.rels")); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  132.         foreach ($rels->Relationship as $rel) {
  133.             switch ($rel["Type"]) {
  134.                 case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties":
  135.                     $xmlCore = simplexml_load_string($zip->getFromName("{$rel['Target']}"));
  136.                     $xmlCore->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/");
  137.                     $xmlCore->registerXPathNamespace("dcterms", "http://purl.org/dc/terms/");
  138.                     $xmlCore->registerXPathNamespace("cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
  139.                     $docProps = $excel->getProperties();
  140.                     $docProps->setCreator((string) self::array_item($xmlCore->xpath("dc:creator")));
  141.                     $docProps->setLastModifiedBy((string) self::array_item($xmlCore->xpath("cp:lastModifiedBy")));
  142.                     $docProps->setCreated(strtotime(self::array_item($xmlCore->xpath("dcterms:created")))); //! respect xsi:type
  143.                     $docProps->setModified(strtotime(self::array_item($xmlCore->xpath("dcterms:modified")))); //! respect xsi:type
  144.                     $docProps->setTitle((string) self::array_item($xmlCore->xpath("dc:title")));
  145.                     $docProps->setDescription((string) self::array_item($xmlCore->xpath("dc:description")));
  146.                     $docProps->setSubject((string) self::array_item($xmlCore->xpath("dc:subject")));
  147.                     $docProps->setKeywords((string) self::array_item($xmlCore->xpath("cp:keywords")));
  148.                     $docProps->setCategory((string) self::array_item($xmlCore->xpath("cp:category")));
  149.                 break;
  150.                 
  151.                 case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument":
  152.                     $dir = dirname($rel["Target"]);
  153.                     $relsWorkbook = simplexml_load_string($zip->getFromName("$dir/_rels/" . basename($rel["Target"]) . ".rels"));  //~ http://schemas.openxmlformats.org/package/2006/relationships");
  154.                     $relsWorkbook->registerXPathNamespace("rel", "http://schemas.openxmlformats.org/package/2006/relationships");
  155.                     
  156.                     $sharedStrings = array();
  157.                     $xpath = self::array_item($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings']"));
  158.                     $xmlStrings = simplexml_load_string($zip->getFromName("$dir/$xpath[Target]"));  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
  159.                     if (isset($xmlStrings) && isset($xmlStrings->si)) {
  160.                         foreach ($xmlStrings->si as $val) {
  161.                             if (isset($val->t)) {
  162.                                 $sharedStrings[] = PHPExcel_Shared_String::ControlCharacterOOXML2PHP( (string) $val->t );
  163.                             } elseif (isset($val->r)) {
  164.                                 $sharedStrings[] = $this->_parseRichText($val);
  165.                             }
  166.                         }
  167.                     }
  168.                     
  169.                     $worksheets = array();
  170.                     foreach ($relsWorkbook->Relationship as $ele) {
  171.                         if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet") {
  172.                             $worksheets[(string) $ele["Id"]] = $ele["Target"];
  173.                         }
  174.                     }
  175.                     
  176.                     $styles = array();
  177.                     $xpath = self::array_item($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles']"));
  178.                     $xmlStyles = simplexml_load_string($zip->getFromName("$dir/$xpath[Target]")); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
  179.                     $numFmts = $xmlStyles->numFmts[0];
  180.                     if ($numFmts) {
  181.                         $numFmts->registerXPathNamespace("sml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
  182.                     }
  183.                     if (!$this->_readDataOnly) {
  184.                         foreach ($xmlStyles->cellXfs->xf as $xf) {
  185.                             $numFmt = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  186.                             if ($numFmts && $xf["numFmtId"]) {
  187.                                 $tmpNumFmt = self::array_item($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]"));
  188.                                 if (isset($tmpNumFmt["formatCode"])) {
  189.                                     $numFmt = (string) $tmpNumFmt["formatCode"];
  190.                                 } else if ((int)$xf["numFmtId"] < 165) {
  191.                                     $numFmt = PHPExcel_Style_NumberFormat::builtInFormatCode((int)$xf["numFmtId"]);
  192.                                 }
  193.                             }
  194.                             //$numFmt = str_replace('mm', 'i', $numFmt);
  195.                             //$numFmt = str_replace('h', 'H', $numFmt);
  196.                             
  197.                             $styles[] = (object) array(
  198.                                 "numFmt" => $numFmt,
  199.                                 "font" => $xmlStyles->fonts->font[intval($xf["fontId"])],
  200.                                 "fill" => $xmlStyles->fills->fill[intval($xf["fillId"])],
  201.                                 "border" => $xmlStyles->borders->border[intval($xf["borderId"])],
  202.                                 "alignment" => $xf->alignment,
  203.                                 "protection" => $xf->protection
  204.                             );;
  205.                         }
  206.                     }
  207.                     
  208.                     $dxfs = array();
  209.                     if (!$this->_readDataOnly) {
  210.                         foreach ($xmlStyles->dxfs->dxf as $dxf) {
  211.                             $style = new PHPExcel_Style;
  212.                             $this->_readStyle($style, $dxf);
  213.                             $dxfs[] = $style;
  214.                         }
  215.                     }
  216.  
  217.                     $xmlWorkbook = simplexml_load_string($zip->getFromName("{$rel['Target']}"));  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
  218.                     $sheetId = 0;
  219.                     foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
  220.                         $docSheet = $excel->createSheet();
  221.                         $docSheet->setTitle((string) $eleSheet["name"]);
  222.                         $fileWorksheet = $worksheets[(string) self::array_item($eleSheet->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")];
  223.                         $xmlSheet = simplexml_load_string($zip->getFromName("$dir/$fileWorksheet"));  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
  224.  
  225.                         $sharedFormulas = array();
  226.                         
  227.                         if (isset($xmlSheet->sheetViews) && isset($xmlSheet->sheetViews->sheetView)) {
  228.                             if (isset($xmlSheet->sheetViews->sheetView['showGridLines'])) { 
  229.                                 $docSheet->setShowGridLines($xmlSheet->sheetViews->sheetView['showGridLines'] ? true : false);
  230.                             }
  231.                         }
  232.                         
  233.                         if (isset($xmlSheet->sheetPr) && isset($xmlSheet->sheetPr->outlinePr)) {
  234.                             if (isset($xmlSheet->sheetPr->outlinePr['summaryRight']) && $xmlSheet->sheetPr->outlinePr['summaryRight'] == false) {
  235.                                 $docSheet->setShowSummaryRight(false);
  236.                             } else {
  237.                                 $docSheet->setShowSummaryRight(true);
  238.                             }
  239.                             
  240.                             if (isset($xmlSheet->sheetPr->outlinePr['summaryBelow']) && $xmlSheet->sheetPr->outlinePr['summaryBelow'] == false) {
  241.                                 $docSheet->setShowSummaryBelow(false);
  242.                             } else {
  243.                                 $docSheet->setShowSummaryBelow(true);
  244.                             }
  245.                         }
  246.                         
  247.                         if (isset($xmlSheet->sheetFormatPr)) {
  248.                             if (isset($xmlSheet->sheetFormatPr['customHeight']) && ((string)$xmlSheet->sheetFormatPr['customHeight'] == '1' || strtolower((string)$xmlSheet->sheetFormatPr['customHeight']) == 'true') && isset($xmlSheet->sheetFormatPr['defaultRowHeight'])) {
  249.                                 $docSheet->getDefaultRowDimension()->setRowHeight( (float)$xmlSheet->sheetFormatPr['defaultRowHeight'] );
  250.                             }
  251.                             if (isset($xmlSheet->sheetFormatPr['defaultColWidth'])) {
  252.                                 $docSheet->getDefaultColumnDimension()->setWidth( (float)$xmlSheet->sheetFormatPr['defaultColWidth'] );
  253.                             }
  254.                         }
  255.                         
  256.                         if (isset($xmlSheet->cols) && !$this->_readDataOnly) {
  257.                             foreach ($xmlSheet->cols->col as $col) {
  258.                                 for ($i = intval($col["min"]) - 1; $i < intval($col["max"]); $i++) {
  259.                                     if ($col["bestFit"]) {
  260.                                         $docSheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($i))->setAutoSize(true);
  261.                                     }
  262.                                     if ($col["hidden"]) {
  263.                                         $docSheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($i))->setVisible(false);
  264.                                     }
  265.                                     if ($col["collapsed"]) {
  266.                                         $docSheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($i))->setCollapsed(true);
  267.                                     }
  268.                                     if ($col["outlineLevel"] > 0) {
  269.                                         $docSheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($i))->setOutlineLevel(intval($col["outlineLevel"]));
  270.                                     }
  271.                                     $docSheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($i))->setWidth(floatval($col["width"]));
  272.                                     
  273.                                     if (intval($col["max"]) == 16384) {
  274.                                         break;
  275.                                     }
  276.                                 }
  277.                             }
  278.                         }
  279.                         
  280.                         if (isset($xmlSheet->printOptions) && !$this->_readDataOnly) {
  281.                             if ($xmlSheet->printOptions['gridLinesSet'] == 'true' && $xmlSheet->printOptions['gridLinesSet'] == '1') {
  282.                                 $docSheet->setShowGridlines(true);
  283.                             }
  284.                             
  285.                             if ($xmlSheet->printOptions['gridLines'] == 'true' || $xmlSheet->printOptions['gridLines'] == '1') {
  286.                                 $docSheet->setPrintGridlines(true);
  287.                             }
  288.                             
  289.                             if ($xmlSheet->printOptions['horizontalCentered']) {
  290.                                 $docSheet->getPageSetup()->setHorizontalCentered(true);
  291.                             }
  292.                             if ($xmlSheet->printOptions['verticalCentered']) {
  293.                                 $docSheet->getPageSetup()->setVerticalCentered(true);
  294.                             }
  295.                         }
  296.  
  297.                         foreach ($xmlSheet->sheetData->row as $row) {
  298.                             if ($row["ht"] && !$this->_readDataOnly) {
  299.                                 $docSheet->getRowDimension(intval($row["r"]))->setRowHeight(floatval($row["ht"]));
  300.                             }
  301.                             if ($row["hidden"] && !$this->_readDataOnly) {
  302.                                 $docSheet->getRowDimension(intval($row["r"]))->setVisible(false);
  303.                             }
  304.                             if ($row["collapsed"]) {
  305.                                 $docSheet->getRowDimension(intval($row["r"]))->setCollapsed(true);
  306.                             }
  307.                             if ($row["outlineLevel"] > 0) {
  308.                                 $docSheet->getRowDimension(intval($row["r"]))->setOutlineLevel(intval($row["outlineLevel"]));
  309.                             }
  310.  
  311.                             foreach ($row->c as $c) {
  312.                                 $r = (string) $c["r"];
  313.  
  314.                                 switch ($c["t"]) {
  315.                                     case "s":
  316.                                         if ((string)$c->v != '') {
  317.                                             $value = $sharedStrings[intval($c->v)];
  318.                                             
  319.                                             if ($value instanceof PHPExcel_RichText) {
  320.                                                 $value = clone $value;
  321.                                             }
  322.                                         } else {
  323.                                             $value = '';
  324.                                         }
  325.                                         
  326.                                         break;
  327.                                     case "b":
  328.                                         $value = (string)$c->v;
  329.                                         if ($value == '0') {
  330.                                             $value = false;
  331.                                         } else if ($value == '1') {
  332.                                             $value = true;
  333.                                         } else {
  334.                                             $value = (bool)$c->v;
  335.                                         }
  336.                                     
  337.                                         break;
  338.                                     case "inlineStr":
  339.                                         $value = $this->_parseRichText($c->is);
  340.                                         $value->setParent($docSheet->getCell($r));
  341.                                         
  342.                                         break;
  343.                                     
  344.                                     default:
  345.                                         if (!isset($c->f)) {
  346.                                             $value = (string) $c->v;
  347.                                         } else {
  348.                                             // Formula
  349.                                             $value = "={$c->f}";
  350.                                             
  351.                                             // Shared formula?
  352.                                             if (isset($c->f['t']) && strtolower((string)$c->f['t']) == 'shared') {
  353.                                                 $instance = (string)$c->f['si'];
  354.  
  355.                                                 if (!isset($sharedFormulas[(string)$c->f['si']])) {
  356.                                                     $sharedFormulas[$instance] = array('master' => $r,
  357.                                                                                         'formula' => $value);
  358.                                                 } else {                                            
  359.                                                     $master = PHPExcel_Cell::coordinateFromString($sharedFormulas[$instance]['master']);
  360.                                                     $current = PHPExcel_Cell::coordinateFromString($r);
  361.  
  362.                                                     $difference = array(0, 0);
  363.                                                     $difference[0] = PHPExcel_Cell::columnIndexFromString($current[0]) - PHPExcel_Cell::columnIndexFromString( $master[0]);
  364.                                                     $difference[1] = $current[1] - $master[1];
  365.  
  366.                                                     $helper = PHPExcel_ReferenceHelper::getInstance();
  367.                                                     $x = $helper->updateFormulaReferences(
  368.                                                         $sharedFormulas[$instance]['formula'],
  369.                                                         'A1',
  370.                                                         $difference[0],
  371.                                                         $difference[1]
  372.                                                     );
  373.  
  374.                                                     $value = $x;
  375.                                                 }
  376.                                             }
  377.                                         }
  378.                                         
  379.                                         break;
  380.                                 }
  381.                                 
  382.                                 // Check for numeric values
  383.                                 if (is_numeric($value)) {
  384.                                     if ($value == (int)$value) $value = (int)$value;
  385.                                     elseif ($value == (float)$value) $value = (float)$value;
  386.                                     elseif ($value == (double)$value) $value = (double)$value;
  387.                                 }
  388.  
  389.                                 $docSheet->setCellValue($r, $value);
  390.  
  391.                                 if ($c["s"] && !$this->_readDataOnly) {
  392.                                     if (isset($styles[intval($c["s"])])) {
  393.                                         $this->_readStyle($docSheet->getStyle($r), $styles[intval($c["s"])]);
  394.                                     }
  395.                                     
  396.                                     if (PHPExcel_Shared_Date::isDateTimeFormat($docSheet->getStyle($r)->getNumberFormat())) {
  397.                                         if (preg_match ("/^([0-9.,-]+)$/", $value)) {
  398.                                             $docSheet->setCellValue($r, PHPExcel_Shared_Date::ExcelToPHP($value));
  399.                                         }
  400.                                     }
  401.                                 }
  402.                             }
  403.                         }
  404.  
  405.                         $conditionals = array();
  406.                         if (!$this->_readDataOnly) {
  407.                             foreach ($xmlSheet->conditionalFormatting as $conditional) {
  408.                                 foreach ($conditional->cfRule as $cfRule) {
  409.                                     if (
  410.                                         (
  411.                                             (string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_NONE ||
  412.                                             (string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_CELLIS ||
  413.                                             (string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  414.                                         ) && isset($dxfs[intval($cfRule["dxfId"])])
  415.                                     ) {
  416.                                         $conditionals[(string) $conditional["sqref"]][intval($cfRule["priority"])] = $cfRule;
  417.                                     }
  418.                                 }
  419.                             }
  420.                                 
  421.                             foreach ($conditionals as $ref => $cfRules) {
  422.                                 ksort($cfRules);
  423.                                 $conditionalStyles = array();
  424.                                 foreach ($cfRules as $cfRule) {
  425.                                     $objConditional = new PHPExcel_Style_Conditional();
  426.                                     $objConditional->setConditionType((string) $cfRule["type"]);
  427.                                     $objConditional->setOperatorType((string) $cfRule["operator"]);
  428.                                     $objConditional->setCondition((string) $cfRule->formula);
  429.                                     $objConditional->setStyle(clone $dxfs[intval($cfRule["dxfId"])]);
  430.                                     $conditionalStyles[] = $objConditional;
  431.                                 }
  432.                                 
  433.                                 // Extract all cell references in $ref
  434.                                 $aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($ref);
  435.                                 foreach ($aReferences as $reference) {
  436.                                     $docSheet->getStyle($reference)->setConditionalStyles($conditionalStyles);
  437.                                 }
  438.                             }
  439.                         }
  440.  
  441.                         $aKeys = array("sheet", "objects", "scenarios", "formatCells", "formatColumns", "formatRows", "insertColumns", "insertRows", "insertHyperlinks", "deleteColumns", "deleteRows", "selectLockedCells", "sort", "autoFilter", "pivotTables", "selectUnlockedCells");
  442.                         if (!$this->_readDataOnly) {
  443.                             foreach ($aKeys as $key) {
  444.                                 $method = "set" . ucfirst($key);
  445.                                 $docSheet->getProtection()->$method($xmlSheet->sheetProtection[$key] == "true");
  446.                             }
  447.                         }
  448.                         
  449.                         if (!$this->_readDataOnly) {
  450.                             $docSheet->getProtection()->setPassword((string) $xmlSheet->sheetProtection["password"], true);
  451.                             if ($xmlSheet->protectedRanges->protectedRange) {
  452.                                 foreach ($xmlSheet->protectedRanges->protectedRange as $protectedRange) {
  453.                                     $docSheet->protectCells((string) $protectedRange["sqref"], (string) $protectedRange["password"], true);
  454.                                 }
  455.                             }
  456.                         }
  457.  
  458.                         if ($xmlSheet->autoFilter && !$this->_readDataOnly) {
  459.                             $docSheet->setAutoFilter((string) $xmlSheet->autoFilter["ref"]);
  460.                         }
  461.  
  462.                         if ($xmlSheet->mergeCells->mergeCell && !$this->_readDataOnly) {
  463.                             foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) {
  464.                                 $docSheet->mergeCells((string) $mergeCell["ref"]);
  465.                             }
  466.                         }
  467.  
  468.                         if (!$this->_readDataOnly) {
  469.                             $docPageMargins = $docSheet->getPageMargins();
  470.                             $docPageMargins->setLeft(floatval($xmlSheet->pageMargins["left"]));
  471.                             $docPageMargins->setRight(floatval($xmlSheet->pageMargins["right"]));
  472.                             $docPageMargins->setTop(floatval($xmlSheet->pageMargins["top"]));
  473.                             $docPageMargins->setBottom(floatval($xmlSheet->pageMargins["bottom"]));
  474.                             $docPageMargins->setHeader(floatval($xmlSheet->pageMargins["header"]));
  475.                             $docPageMargins->setFooter(floatval($xmlSheet->pageMargins["footer"]));
  476.                         }
  477.                         
  478.                         if (!$this->_readDataOnly) {
  479.                             $docPageSetup = $docSheet->getPageSetup();
  480.                             
  481.                             if (isset($xmlSheet->pageSetup["orientation"])) {
  482.                                 $docPageSetup->setOrientation((string) $xmlSheet->pageSetup["orientation"]);
  483.                             }
  484.                             if (isset($xmlSheet->pageSetup["paperSize"])) {
  485.                                 $docPageSetup->setPaperSize(intval($xmlSheet->pageSetup["paperSize"]));
  486.                             }
  487.                             if (isset($xmlSheet->pageSetup["scale"])) {
  488.                                 $docPageSetup->setScale(intval($xmlSheet->pageSetup["scale"]));
  489.                             }
  490.                             if (isset($xmlSheet->pageSetup["fitToHeight"])) {
  491.                                 $docPageSetup->setFitToHeight(intval($xmlSheet->pageSetup["fitToHeight"]));
  492.                             }
  493.                             if (isset($xmlSheet->pageSetup["fitToWidth"])) {
  494.                                 $docPageSetup->setFitToWidth(intval($xmlSheet->pageSetup["fitToWidth"]));
  495.                             }
  496.                         }
  497.                         
  498.                         if (!$this->_readDataOnly) {
  499.                             $docHeaderFooter = $docSheet->getHeaderFooter();
  500.                             $docHeaderFooter->setDifferentOddEven($xmlSheet->headerFooter["differentOddEven"] == 'true');
  501.                             $docHeaderFooter->setDifferentFirst($xmlSheet->headerFooter["differentFirst"] == 'true');
  502.                             $docHeaderFooter->setScaleWithDocument($xmlSheet->headerFooter["scaleWithDoc"] == 'true');
  503.                             $docHeaderFooter->setAlignWithMargins($xmlSheet->headerFooter["alignWithMargins"] == 'true');
  504.                             $docHeaderFooter->setOddHeader((string) $xmlSheet->headerFooter->oddHeader);
  505.                             $docHeaderFooter->setOddFooter((string) $xmlSheet->headerFooter->oddFooter);
  506.                             $docHeaderFooter->setEvenHeader((string) $xmlSheet->headerFooter->evenHeader);
  507.                             $docHeaderFooter->setEvenFooter((string) $xmlSheet->headerFooter->evenFooter);
  508.                             $docHeaderFooter->setFirstHeader((string) $xmlSheet->headerFooter->firstHeader);
  509.                             $docHeaderFooter->setFirstFooter((string) $xmlSheet->headerFooter->firstFooter);
  510.                         }
  511.                         
  512.                         if ($xmlSheet->rowBreaks->brk && !$this->_readDataOnly) {
  513.                             foreach ($xmlSheet->rowBreaks->brk as $brk) {
  514.                                 if ($brk["man"]) {
  515.                                     $docSheet->setBreak("A$brk[id]", PHPExcel_Worksheet::BREAK_ROW);
  516.                                 }
  517.                             }
  518.                         }
  519.                         if ($xmlSheet->colBreaks->brk && !$this->_readDataOnly) {
  520.                             foreach ($xmlSheet->colBreaks->brk as $brk) {
  521.                                 if ($brk["man"]) {
  522.                                     $docSheet->setBreak(PHPExcel_Cell::stringFromColumnIndex($brk["id"]) . "1", PHPExcel_Worksheet::BREAK_COLUMN);
  523.                                 }
  524.                             }
  525.                         }
  526.                     
  527.                         if ($xmlSheet->dataValidations && !$this->_readDataOnly) {
  528.                             foreach ($xmlSheet->dataValidations->dataValidation as $dataValidation) {
  529.                                 // Uppercase coordinate
  530.                                 $range = strtoupper($dataValidation["sqref"]);
  531.                                 
  532.                                 // Extract all cell references in $range
  533.                                 $aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($range);
  534.                                 foreach ($aReferences as $reference) {                                
  535.                                     // Create validation
  536.                                     $docValidation = $docSheet->getCell($reference)->getDataValidation();
  537.                                     $docValidation->setType((string) $dataValidation["type"]);
  538.                                     $docValidation->setErrorStyle((string) $dataValidation["errorStyle"]);
  539.                                     $docValidation->setOperator((string) $dataValidation["operator"]);
  540.                                     $docValidation->setAllowBlank($dataValidation["allowBlank"] != 0);
  541.                                     $docValidation->setShowDropDown($dataValidation["showDropDown"] == 0);
  542.                                     $docValidation->setShowInputMessage($dataValidation["showInputMessage"] != 0);
  543.                                     $docValidation->setShowErrorMessage($dataValidation["showErrorMessage"] != 0);
  544.                                     $docValidation->setErrorTitle((string) $dataValidation["errorTitle"]);
  545.                                     $docValidation->setError((string) $dataValidation["error"]);
  546.                                     $docValidation->setPromptTitle((string) $dataValidation["promptTitle"]);
  547.                                     $docValidation->setPrompt((string) $dataValidation["prompt"]);
  548.                                     $docValidation->setFormula1((string) $dataValidation->formula1);
  549.                                     $docValidation->setFormula2((string) $dataValidation->formula2);
  550.                                 }
  551.                             }
  552.                         }
  553.                         
  554.                         // Add hyperlinks
  555.                         $hyperlinks = array();
  556.                         if (!$this->_readDataOnly) {
  557.                             // Locate hyperlink relations
  558.                             if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) {
  559.                                 $relsWorksheet = simplexml_load_string($zip->getFromName( dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  560.                                 foreach ($relsWorksheet->Relationship as $ele) {
  561.                                     if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink") {
  562.                                         $hyperlinks[(string)$ele["Id"]] = (string)$ele["Target"];
  563.                                     }
  564.                                 }
  565.                             }
  566.  
  567.                             // Loop trough hyperlinks
  568.                             if ($xmlSheet->hyperlinks) {
  569.                                 foreach ($xmlSheet->hyperlinks->hyperlink as $hyperlink) {
  570.                                     // Link url
  571.                                     $linkRel = $hyperlink->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  572.                                     
  573.                                     if (isset($linkRel['id'])) {
  574.                                         $docSheet->getCell( $hyperlink['ref'] )->getHyperlink()->setUrl( $hyperlinks[ (string)$linkRel['id'] ] );
  575.                                     }
  576.                                     if (isset($hyperlink['location'])) {
  577.                                         $docSheet->getCell( $hyperlink['ref'] )->getHyperlink()->setUrl( 'sheet://' . (string)$hyperlink['location'] );
  578.                                     }
  579.                                         
  580.                                     // Tooltip
  581.                                     if (isset($hyperlink['tooltip'])) {
  582.                                         $docSheet->getCell( $hyperlink['ref'] )->getHyperlink()->setTooltip( (string)$hyperlink['tooltip'] );
  583.                                     }                                
  584.                                 }
  585.                             }
  586.                         }
  587.                         
  588.                         // Add comments
  589.                         $comments = array();
  590.                         if (!$this->_readDataOnly) {
  591.                             // Locate comment relations
  592.                             if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) {
  593.                                 $relsWorksheet = simplexml_load_string($zip->getFromName( dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  594.                                 foreach ($relsWorksheet->Relationship as $ele) {
  595.                                     if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments") {
  596.                                         $comments[(string)$ele["Id"]] = (string)$ele["Target"];
  597.                                     }
  598.                                 }
  599.                             }
  600.  
  601.                             // Loop trough comments
  602.                             foreach ($comments as $relName => $relPath) {
  603.                                 // Load comments file
  604.                                 $relPath = PHPExcel_Shared_File::realpath(dirname("$dir/$fileWorksheet") . "/" . $relPath);
  605.                                 $commentsFile = simplexml_load_string($zip->getFromName($relPath) );
  606.                                 
  607.                                 // Utility variables
  608.                                 $authors = array();
  609.                                 
  610.                                 // Loop trough authors
  611.                                 foreach ($commentsFile->authors->author as $author) {
  612.                                     $authors[] = (string)$author;
  613.                                 }
  614.                                 
  615.                                 // Loop trough contents
  616.                                 foreach ($commentsFile->commentList->comment as $comment) {
  617.                                     $docSheet->getComment( (string)$comment['ref'] )->setAuthor( $authors[(string)$comment['authorId']] );
  618.                                     $docSheet->getComment( (string)$comment['ref'] )->setText( $this->_parseRichText($comment->text) );
  619.                                 }
  620.                             }
  621.                                     
  622.                             // Header/footer images            
  623.                             if ($xmlSheet->legacyDrawingHF && !$this->_readDataOnly) {
  624.                                 if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) {
  625.                                     $relsWorksheet = simplexml_load_string($zip->getFromName( dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  626.                                     $vmlRelationship = '';
  627.                                     
  628.                                     foreach ($relsWorksheet->Relationship as $ele) {
  629.                                         if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing") {
  630.                                             $vmlRelationship = self::dir_add("$dir/$fileWorksheet", $ele["Target"]);
  631.                                         }
  632.                                     }
  633.                                     
  634.                                     if ($vmlRelationship != '') {
  635.                                         // Fetch linked images
  636.                                         $relsVML = simplexml_load_string($zip->getFromName( dirname($vmlRelationship) . '/_rels/' . basename($vmlRelationship) . '.rels' )); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  637.                                         $drawings = array();
  638.                                         foreach ($relsVML->Relationship as $ele) {
  639.                                             if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image") {
  640.                                                 $drawings[(string) $ele["Id"]] = self::dir_add($vmlRelationship, $ele["Target"]);
  641.                                             }
  642.                                         }
  643.                             
  644.                                         // Fetch VML document
  645.                                         $vmlDrawing = simplexml_load_string($zip->getFromName($vmlRelationship));
  646.                                         $vmlDrawing->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml');
  647.                                         
  648.                                         $hfImages = array();
  649.                                         
  650.                                         $shapes = $vmlDrawing->xpath('//v:shape');
  651.                                         foreach ($shapes as $shape) {                                            
  652.                                             $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml');
  653.                                             $imageData = $shape->xpath('//v:imagedata');
  654.                                             $imageData = $imageData[0];
  655.                                             
  656.                                             $imageData = $imageData->attributes('urn:schemas-microsoft-com:office:office');
  657.                                             $style = self::toCSSArray( (string)$shape['style'] );
  658.                                             
  659.                                             $hfImages[ (string)$shape['id'] ] = new PHPExcel_Worksheet_HeaderFooterDrawing();
  660.                                             if (isset($imageData['title'])) {
  661.                                                 $hfImages[ (string)$shape['id'] ]->setName( (string)$imageData['title'] );
  662.                                             }
  663.                                             
  664.                                             $hfImages[ (string)$shape['id'] ]->setPath("zip://$pFilename#" . $drawings[(string)$imageData['relid']], false);
  665.                                             $hfImages[ (string)$shape['id'] ]->setResizeProportional(false);
  666.                                             $hfImages[ (string)$shape['id'] ]->setWidth($style['width']);
  667.                                             $hfImages[ (string)$shape['id'] ]->setHeight($style['height']);
  668.                                             $hfImages[ (string)$shape['id'] ]->setOffsetX($style['margin-left']);
  669.                                             $hfImages[ (string)$shape['id'] ]->setOffsetY($style['margin-top']);
  670.                                             $hfImages[ (string)$shape['id'] ]->setResizeProportional(true);
  671.                                         }
  672.  
  673.                                         $docSheet->getHeaderFooter()->setImages($hfImages);
  674.                                     }
  675.                                 }
  676.                             }
  677.  
  678.                         }
  679.                         
  680. // TODO: Make sure drawings and graph are loaded differently!
  681.                         if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) {
  682.                             $relsWorksheet = simplexml_load_string($zip->getFromName( dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  683.                             $drawings = array();
  684.                             foreach ($relsWorksheet->Relationship as $ele) {
  685.                                 if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing") {
  686.                                     $drawings[(string) $ele["Id"]] = self::dir_add("$dir/$fileWorksheet", $ele["Target"]);
  687.                                 }
  688.                             }
  689.                             if ($xmlSheet->drawing && !$this->_readDataOnly) {
  690.                                 foreach ($xmlSheet->drawing as $drawing) {
  691.                                     $fileDrawing = $drawings[(string) self::array_item($drawing->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")];
  692.                                     $relsDrawing = simplexml_load_string($zip->getFromName( dirname($fileDrawing) . "/_rels/" . basename($fileDrawing) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships");
  693.                                     $images = array();
  694.                                     
  695.                                     if ($relsDrawing && $relsDrawing->Relationship) {
  696.                                         foreach ($relsDrawing->Relationship as $ele) {
  697.                                             if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image") {
  698.                                                 $images[(string) $ele["Id"]] = self::dir_add($fileDrawing, $ele["Target"]);
  699.                                             }
  700.                                         }
  701.                                     }
  702.                                     $xmlDrawing = simplexml_load_string($zip->getFromName($fileDrawing))->children("http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing");
  703.                                     
  704.                                     if ($xmlDrawing->oneCellAnchor) {
  705.                                         foreach ($xmlDrawing->oneCellAnchor as $oneCellAnchor) {
  706.                                             if ($oneCellAnchor->pic->blipFill) {
  707.                                                 $blip = $oneCellAnchor->pic->blipFill->children("http://schemas.openxmlformats.org/drawingml/2006/main")->blip;
  708.                                                 $xfrm = $oneCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->xfrm;
  709.                                                 $outerShdw = $oneCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->effectLst->outerShdw;
  710.                                                 $objDrawing = new PHPExcel_Worksheet_Drawing;
  711.                                                 $objDrawing->setName((string) self::array_item($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), "name"));
  712.                                                 $objDrawing->setDescription((string) self::array_item($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), "descr"));
  713.                                                 $objDrawing->setPath("zip://$pFilename#" . $images[(string) self::array_item($blip->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "embed")], false);                                
  714.                                                 $objDrawing->setCoordinates(PHPExcel_Cell::stringFromColumnIndex($oneCellAnchor->from->col) . ($oneCellAnchor->from->row + 1));
  715.                                                 $objDrawing->setOffsetX(PHPExcel_Shared_Drawing::EMUToPixels($oneCellAnchor->from->colOff));
  716.                                                 $objDrawing->setOffsetY(PHPExcel_Shared_Drawing::EMUToPixels($oneCellAnchor->from->rowOff));
  717.                                                 $objDrawing->setResizeProportional(false);
  718.                                                 $objDrawing->setWidth(PHPExcel_Shared_Drawing::EMUToPixels(self::array_item($oneCellAnchor->ext->attributes(), "cx")));
  719.                                                 $objDrawing->setHeight(PHPExcel_Shared_Drawing::EMUToPixels(self::array_item($oneCellAnchor->ext->attributes(), "cy")));
  720.                                                 if ($xfrm) {
  721.                                                     $objDrawing->setRotation(PHPExcel_Shared_Drawing::angleToDegrees(self::array_item($xfrm->attributes(), "rot")));
  722.                                                 }
  723.                                                 if ($outerShdw) {
  724.                                                     $shadow = $objDrawing->getShadow();
  725.                                                     $shadow->setVisible(true);
  726.                                                     $shadow->setBlurRadius(PHPExcel_Shared_Drawing::EMUTopixels(self::array_item($outerShdw->attributes(), "blurRad")));
  727.                                                     $shadow->setDistance(PHPExcel_Shared_Drawing::EMUTopixels(self::array_item($outerShdw->attributes(), "dist")));
  728.                                                     $shadow->setDirection(PHPExcel_Shared_Drawing::angleToDegrees(self::array_item($outerShdw->attributes(), "dir")));
  729.                                                     $shadow->setAlignment((string) self::array_item($outerShdw->attributes(), "algn"));
  730.                                                     $shadow->getColor()->setRGB(self::array_item($outerShdw->srgbClr->attributes(), "val"));
  731.                                                     $shadow->setAlpha(self::array_item($outerShdw->srgbClr->alpha->attributes(), "val") / 1000);
  732.                                                 }
  733.                                                 $objDrawing->setWorksheet($docSheet);
  734.                                             }
  735.                                         }
  736.                                     }
  737.                                     if ($xmlDrawing->twoCellAnchor) {
  738.                                         foreach ($xmlDrawing->twoCellAnchor as $twoCellAnchor) {
  739.                                             if ($twoCellAnchor->pic->blipFill) {
  740.                                                 $blip = $twoCellAnchor->pic->blipFill->children("http://schemas.openxmlformats.org/drawingml/2006/main")->blip;
  741.                                                 $xfrm = $twoCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->xfrm;
  742.                                                 $outerShdw = $twoCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->effectLst->outerShdw;
  743.                                                 $objDrawing = new PHPExcel_Worksheet_Drawing;
  744.                                                 $objDrawing->setName((string) self::array_item($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), "name"));
  745.                                                 $objDrawing->setDescription((string) self::array_item($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), "descr"));
  746.                                                 $objDrawing->setPath("zip://$pFilename#" . $images[(string) self::array_item($blip->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "embed")], false);                                
  747.                                                 $objDrawing->setCoordinates(PHPExcel_Cell::stringFromColumnIndex($twoCellAnchor->from->col) . ($twoCellAnchor->from->row + 1));
  748.                                                 $objDrawing->setOffsetX(PHPExcel_Shared_Drawing::EMUToPixels($twoCellAnchor->from->colOff));
  749.                                                 $objDrawing->setOffsetY(PHPExcel_Shared_Drawing::EMUToPixels($twoCellAnchor->from->rowOff));
  750.                                                 $objDrawing->setResizeProportional(false);
  751.                                                 
  752.                                                 $objDrawing->setWidth(PHPExcel_Shared_Drawing::EMUToPixels(self::array_item($xfrm->ext->attributes(), "cx")));
  753.                                                 $objDrawing->setHeight(PHPExcel_Shared_Drawing::EMUToPixels(self::array_item($xfrm->ext->attributes(), "cy")));
  754.                                                 
  755.                                                 if ($xfrm) {
  756.                                                     $objDrawing->setRotation(PHPExcel_Shared_Drawing::angleToDegrees(self::array_item($xfrm->attributes(), "rot")));
  757.                                                 }
  758.                                                 if ($outerShdw) {
  759.                                                     $shadow = $objDrawing->getShadow();
  760.                                                     $shadow->setVisible(true);
  761.                                                     $shadow->setBlurRadius(PHPExcel_Shared_Drawing::EMUTopixels(self::array_item($outerShdw->attributes(), "blurRad")));
  762.                                                     $shadow->setDistance(PHPExcel_Shared_Drawing::EMUTopixels(self::array_item($outerShdw->attributes(), "dist")));
  763.                                                     $shadow->setDirection(PHPExcel_Shared_Drawing::angleToDegrees(self::array_item($outerShdw->attributes(), "dir")));
  764.                                                     $shadow->setAlignment((string) self::array_item($outerShdw->attributes(), "algn"));
  765.                                                     $shadow->getColor()->setRGB(self::array_item($outerShdw->srgbClr->attributes(), "val"));
  766.                                                     $shadow->setAlpha(self::array_item($outerShdw->srgbClr->alpha->attributes(), "val") / 1000);
  767.                                                 }
  768.                                                 $objDrawing->setWorksheet($docSheet);
  769.                                             }
  770.                                         }
  771.                                     }
  772.                                     
  773.                                 }
  774.                             }
  775.                         }
  776.  
  777.                         // Loop trough definedNames
  778.                         if ($xmlWorkbook->definedNames) {
  779.                             foreach ($xmlWorkbook->definedNames->definedName as $definedName) {
  780.                                 // Extract range
  781.                                 $extractedRange = (string)$definedName;
  782.                                 if (strpos($extractedRange, '!') !== false) {
  783.                                     $extractedRange = substr($extractedRange, strpos($extractedRange, '!') + 1);
  784.                                 }
  785.                                 $extractedRange = str_replace('$', '', $extractedRange);
  786.             
  787.                                 // Valid range?
  788.                                 if (stripos((string)$definedName, '#REF!') !== false || $extractedRange == '') {
  789.                                     continue;
  790.                                 }
  791.                                 
  792.                                 // Some definedNames are only applicable if we are on the same sheet...
  793.                                 if ($definedName['localSheetId'] == $sheetId) {                            
  794.                                     // Switch on type
  795.                                     switch ((string)$definedName['name']) {
  796.                                         
  797.                                         case '_xlnm._FilterDatabase':                                
  798.                                             $docSheet->setAutoFilter($extractedRange);
  799.                                             break;
  800.                                             
  801.                                         case '_xlnm.Print_Titles':                                
  802.                                             // Split $extractedRange
  803.                                             $extractedRange = explode(',', $extractedRange);
  804.                                             
  805.                                             // Set print titles
  806.                                             if (isset($extractedRange[0])) {
  807.                                                 $docSheet->getPageSetup()->setColumnsToRepeatAtLeft( explode(':', $extractedRange[0]) );
  808.                                             }
  809.                                             if (isset($extractedRange[1])) {
  810.                                                 $docSheet->getPageSetup()->setRowsToRepeatAtTop( explode(':', $extractedRange[1]) );
  811.                                             }
  812.                                             
  813.                                             break;
  814.                                             
  815.                                         case '_xlnm.Print_Area':
  816.                                             $docSheet->getPageSetup()->setPrintArea($extractedRange);
  817.                                             break;
  818.                                             
  819.                                         default:
  820.                                             $excel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $docSheet, $extractedRange, true) );
  821.                                             break;
  822.                                     }
  823.                                 } else {
  824.                                     // "Global" definedNames
  825.                                     $locatedSheet = null;
  826.                                     $extractedSheetName = '';
  827.                                     if (strpos( (string)$definedName, '!' ) !== false) {
  828.                                         // Extract sheet name
  829.                                         $extractedSheetName = PHPExcel_Worksheet::extractSheetTitle( (string)$definedName );
  830.                                         
  831.                                         // Locate sheet
  832.                                         $locatedSheet = $excel->getSheetByName($extractedSheetName);
  833.                                     }
  834.                                     
  835.                                     if (!is_null($locatedSheet)) {
  836.                                         $excel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $locatedSheet, $extractedRange, false) );
  837.                                     }
  838.                                 }
  839.                             }
  840.                         }
  841.                         
  842.                         // Garbage collect...
  843.                         $docSheet->garbageCollect();
  844.         
  845.                         // Next sheet id
  846.                         $sheetId++;
  847.                     }
  848.                     
  849.                     if (!$this->_readDataOnly) {
  850.                         $excel->setActiveSheetIndex(intval($xmlWorkbook->bookView->workbookView["activeTab"]));
  851.                     }
  852.                 break;
  853.             }
  854.     
  855.         }
  856.         
  857.         return $excel;
  858.     }
  859.     
  860.     private function _readColor($color) {
  861.         if (isset($color["rgb"])) {
  862.             return $color["rgb"];
  863.         } else if (isset($color["indexed"])) {
  864.             return PHPExcel_Style_Color::indexedColor($color["indexed"])->getARGB();
  865.         }
  866.     }
  867.     
  868.     private function _readStyle($docStyle, $style) {
  869.         // format code
  870.         $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
  871.  
  872.         // font
  873.         if (isset($style->font)) {
  874.             $docStyle->getFont()->setName((string) $style->font->name["val"]);
  875.             $docStyle->getFont()->setSize((string) $style->font->sz["val"]);
  876.             if (isset($style->font->b)) {
  877.                 $docStyle->getFont()->setBold(!isset($style->font->b["val"]) || $style->font->b["val"] == 'true');        
  878.             }
  879.             if (isset($style->font->i)) {
  880.                 $docStyle->getFont()->setItalic(!isset($style->font->i["val"]) || $style->font->i["val"] == 'true');
  881.             }
  882.             if (isset($style->font->strike)) {
  883.                 $docStyle->getFont()->setStriketrough(!isset($style->font->strike["val"]) || $style->font->strike["val"] == 'true');
  884.             }
  885.             $docStyle->getFont()->getColor()->setARGB($this->_readColor($style->font->color));
  886.             
  887.             if (isset($style->font->u) && !isset($style->font->u["val"])) {
  888.                 $docStyle->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  889.             } else if (isset($style->font->u) && isset($style->font->u["val"])) {
  890.                 $docStyle->getFont()->setUnderline((string)$style->font->u["val"]);
  891.             }
  892.         }
  893.         
  894.         // fill
  895.         if (isset($style->fill)) {
  896.             if ($style->fill->gradientFill) {
  897.                 $gradientFill = $style->fill->gradientFill[0];
  898.                 $docStyle->getFill()->setFillType((string) $gradientFill["type"]);
  899.                 $docStyle->getFill()->setRotation(floatval($gradientFill["degree"]));
  900.                 $gradientFill->registerXPathNamespace("sml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
  901.                 $docStyle->getFill()->getStartColor()->setARGB($this->_readColor( self::array_item($gradientFill->xpath("sml:stop[@position=0]"))->color) );
  902.                 $docStyle->getFill()->getEndColor()->setARGB($this->_readColor( self::array_item($gradientFill->xpath("sml:stop[@position=1]"))->color) );
  903.             } elseif ($style->fill->patternFill) {
  904.                 $docStyle->getFill()->setFillType((string) $style->fill->patternFill["patternType"]);
  905.                 if ($style->fill->patternFill->fgColor) {
  906.                     $docStyle->getFill()->getStartColor()->setARGB($this->_readColor($style->fill->patternFill->fgColor));
  907.                 }
  908.                 if ($style->fill->patternFill->bgColor) {
  909.                     $docStyle->getFill()->getEndColor()->setARGB($this->_readColor($style->fill->patternFill->bgColor));
  910.                 }
  911.             }
  912.         }
  913.         
  914.         // border
  915.         if (isset($style->border)) {
  916.             if ($style->border["diagonalUp"] == 'true') {
  917.                 $docStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_UP);
  918.             } elseif ($style->border["diagonalDown"] == 'true') {
  919.                 $docStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_DOWN);
  920.             }
  921.             $docStyle->getBorders()->setOutline($style->border["outline"] == 'true');
  922.             $this->_readBorder($docStyle->getBorders()->getLeft(), $style->border->left);
  923.             $this->_readBorder($docStyle->getBorders()->getRight(), $style->border->right);
  924.             $this->_readBorder($docStyle->getBorders()->getTop(), $style->border->top);
  925.             $this->_readBorder($docStyle->getBorders()->getBottom(), $style->border->bottom);
  926.             $this->_readBorder($docStyle->getBorders()->getDiagonal(), $style->border->diagonal);
  927.             $this->_readBorder($docStyle->getBorders()->getVertical(), $style->border->vertical);
  928.             $this->_readBorder($docStyle->getBorders()->getHorizontal(), $style->border->horizontal);
  929.         }
  930.         
  931.         // alignment
  932.         if (isset($style->alignment)) {
  933.             $docStyle->getAlignment()->setHorizontal((string) $style->alignment["horizontal"]);
  934.             $docStyle->getAlignment()->setVertical((string) $style->alignment["vertical"]);
  935.         
  936.             $textRotation = 0;
  937.             if ((int)$style->alignment["textRotation"] <= 90) {
  938.                 $textRotation = (int)$style->alignment["textRotation"];
  939.             } else if ((int)$style->alignment["textRotation"] > 90) {
  940.                 $textRotation = 90 - (int)$style->alignment["textRotation"];
  941.             }
  942.                 
  943.             $docStyle->getAlignment()->setTextRotation(intval($textRotation));
  944.             $docStyle->getAlignment()->setWrapText( (string)$style->alignment["wrapText"] == "true" || (string)$style->alignment["wrapText"] == "1" );
  945.             $docStyle->getAlignment()->setShrinkToFit( (string)$style->alignment["shrinkToFit"] == "true" || (string)$style->alignment["shrinkToFit"] == "1" );
  946.         }
  947.         
  948.         // protection
  949.         if (isset($style->protection)) {
  950.             if (isset($style->protection['locked'])) {
  951.                 if ((string)$style->protection['locked'] == 'true') {
  952.                     $docStyle->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED);
  953.                 } else {
  954.                     $docStyle->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  955.                 }
  956.             }
  957.  
  958.             if (isset($style->protection['hidden'])) {
  959.                 if ((string)$style->protection['hidden'] == 'true') {
  960.                     $docStyle->getProtection()->setHidden(PHPExcel_Style_Protection::PROTECTION_PROTECTED);
  961.                 } else {
  962.                     $docStyle->getProtection()->setHidden(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  963.                 }
  964.             }
  965.         }
  966.     }
  967.     
  968.     private function _readBorder($docBorder, $eleBorder) {
  969.         if (isset($eleBorder["style"])) {
  970.             $docBorder->setBorderStyle((string) $eleBorder["style"]);
  971.         }
  972.         if (isset($eleBorder->color)) {
  973.             $docBorder->getColor()->setARGB($this->_readColor($eleBorder->color));
  974.         }
  975.     }
  976.     
  977.     private function _parseRichText($is = null) {
  978.         $value = new PHPExcel_RichText();
  979.  
  980.         if (isset($is->t)) {
  981.             $value->createText( PHPExcel_Shared_String::ControlCharacterOOXML2PHP( (string) $is->t ) );
  982.         } else {
  983.             foreach ($is->r as $run) {
  984.                 $objText = $value->createTextRun( PHPExcel_Shared_String::ControlCharacterOOXML2PHP( (string) $run->t ) );
  985.                             
  986.                 if (isset($run->rPr->rFont["val"])) {
  987.                     $objText->getFont()->setName((string) $run->rPr->rFont["val"]);
  988.                 }
  989.                                                 
  990.                 if (isset($run->rPr->sz["val"])) {
  991.                     $objText->getFont()->setSize((string) $run->rPr->sz["val"]);
  992.                 }
  993.                                                 
  994.                 if (isset($run->rPr->color)) {
  995.                     $objText->getFont()->setColor( new PHPExcel_Style_Color( $this->_readColor($run->rPr->color) ) );
  996.                 }
  997.                                                 
  998.                 if ( (isset($run->rPr->b["val"]) && ((string) $run->rPr->b["val"] == 'true' || (string) $run->rPr->b["val"] == '1'))
  999.                      || (isset($run->rPr->b) && !isset($run->rPr->b["val"])) ) {
  1000.                     $objText->getFont()->setBold(true);
  1001.                 }
  1002.                                                 
  1003.                 if ( (isset($run->rPr->i["val"]) && ((string) $run->rPr->i["val"] == 'true' || (string) $run->rPr->i["val"] == '1'))
  1004.                      || (isset($run->rPr->i) && !isset($run->rPr->i["val"])) ) {
  1005.                     $objText->getFont()->setItalic(true);
  1006.                 }
  1007.  
  1008.                 if (isset($run->rPr->u) && !isset($run->rPr->u["val"])) {
  1009.                     $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  1010.                 } else if (isset($run->rPr->u) && isset($run->rPr->u["val"])) {
  1011.                     $objText->getFont()->setUnderline((string)$run->rPr->u["val"]);
  1012.                 }
  1013.                                             
  1014.                 if ( (isset($run->rPr->strike["val"])  && ((string) $run->rPr->strike["val"] == 'true' || (string) $run->rPr->strike["val"] == '1'))
  1015.                      || (isset($run->rPr->strike) && !isset($run->rPr->strike["val"])) ) {
  1016.                     $objText->getFont()->setStriketrough(true);
  1017.                 }
  1018.             }
  1019.         }
  1020.                                     
  1021.         return $value;
  1022.     }
  1023.  
  1024.     private static function array_item($array, $key = 0) {
  1025.         return (isset($array[$key]) ? $array[$key] : null);
  1026.     }
  1027.     
  1028.     private static function dir_add($base, $add) {
  1029.         return preg_replace('~[^/]+/\.\./~', '', dirname($base) . "/$add");
  1030.     }
  1031.     
  1032.     private static function toCSSArray($style) {
  1033.         $style = str_replace("\r", "", $style);
  1034.         $style = str_replace("\n", "", $style);
  1035.     
  1036.         $temp = explode(';', $style);
  1037.     
  1038.         $style = array();
  1039.         foreach ($temp as $item) {
  1040.             $item = explode(':', $item);
  1041.             
  1042.             if (strpos($item[1], 'px') !== false) {
  1043.                 $item[1] = str_replace('px', '', $item[1]);
  1044.             }
  1045.             if (strpos($item[1], 'pt') !== false) {
  1046.                 $item[1] = str_replace('pt', '', $item[1]);
  1047.                 $item[1] = PHPExcel_Shared_Font::fontSizeToPixels($item[1]);
  1048.             }
  1049.             
  1050.             $style[$item[0]] = $item[1];
  1051.         }
  1052.     
  1053.         return $style;
  1054.     }
  1055. }

Documentation generated on Mon, 25 Aug 2008 13:37:51 +0200 by phpDocumentor 1.4.1