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

Source for file Excel5.php

Documentation is available at Excel5.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_Excel5
  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. // Original file header of ParseXL (used as the base for this class):
  29. // --------------------------------------------------------------------------------
  30. // Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
  31. // trex005, and mmp11 (SourceForge.net)
  32. // http://sourceforge.net/projects/phpexcelreader/
  33. // Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
  34. //     Modelled moreso after Perl Excel Parse/Write modules
  35. //     Added Parse_Excel_Spreadsheet object
  36. //         Reads a whole worksheet or tab as row,column array or as
  37. //         associated hash of indexed rows and named column fields
  38. //     Added variables for worksheet (tab) indexes and names
  39. //     Added an object call for loading individual woorksheets
  40. //     Changed default indexing defaults to 0 based arrays
  41. //     Fixed date/time and percent formats
  42. //     Includes patches found at SourceForge...
  43. //         unicode patch by nobody
  44. //         unpack("d") machine depedency patch by matchy
  45. //         boundsheet utf16 patch by bjaenichen
  46. //     Renamed functions for shorter names
  47. //     General code cleanup and rigor, including <80 column width
  48. //     Included a testcase Excel file and PHP example calls
  49. //     Code works for PHP 5.x
  50.  
  51. // Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
  52. // http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
  53. //     Decoding of formula conditions, results, and tokens.
  54. //     Support for user-defined named cells added as an array "namedcells"
  55. //         Patch code for user-defined named cells supports single cells only.
  56. //         NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
  57. //         external sheet reference structure
  58.  
  59.  
  60. /** PHPExcel root directory */
  61. if (!defined('PHPEXCEL_ROOT')) {
  62.     /**
  63.      * @ignore
  64.      */
  65.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  66. }
  67.  
  68. /** PHPExcel */
  69. require_once PHPEXCEL_ROOT 'PHPExcel.php';
  70.  
  71. /** PHPExcel_Reader_IReader */
  72. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/IReader.php';
  73.  
  74. /** PHPExcel_Reader_Excel5_Escher */
  75. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/Excel5/Escher.php';
  76.  
  77. /** PHPExcel_Shared_Date */
  78. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Date.php';
  79.  
  80. /** PHPExcel_Shared_Excel5 */
  81. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Excel5.php';
  82.  
  83. /** PHPExcel_Shared_Escher */
  84. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Escher.php';
  85.  
  86. /** PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE */
  87. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Escher/DggContainer/BstoreContainer/BSE.php';
  88.  
  89. /** PHPExcel_Shared_OLERead */
  90. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/OLERead.php';
  91.  
  92. /** PHPExcel_Shared_String */
  93. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/String.php';
  94.  
  95. /** PHPExcel_Cell */
  96. require_once PHPEXCEL_ROOT 'PHPExcel/Cell.php';
  97.  
  98. /** PHPExcel_NamedRange */
  99. require_once PHPEXCEL_ROOT 'PHPExcel/NamedRange.php';
  100.  
  101. /** PHPExcel_Reader_IReadFilter */
  102. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/IReadFilter.php';
  103.  
  104. /** PHPExcel_Reader_DefaultReadFilter */
  105. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/DefaultReadFilter.php';
  106.  
  107. /** PHPExcel_Worksheet_MemoryDrawing */
  108. require_once PHPEXCEL_ROOT 'PHPExcel/Worksheet/MemoryDrawing.php';
  109.  
  110. /** PHPExcel_Style_Borders */
  111. require_once PHPEXCEL_ROOT 'PHPExcel/Style/Borders.php';
  112.  
  113.  
  114. /**
  115.  * PHPExcel_Reader_Excel5
  116.  *
  117.  * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
  118.  *
  119.  * @category   PHPExcel
  120.  * @package    PHPExcel_Reader_Excel5
  121.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  122.  */
  123. class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
  124. {
  125.     // ParseXL definitions
  126.     const XLS_BIFF8                        0x0600;
  127.     const XLS_BIFF7                        0x0500;
  128.     const XLS_WorkbookGlobals            0x0005;
  129.     const XLS_Worksheet                    0x0010;
  130.  
  131.     // record identifiers
  132.     const XLS_Type_FORMULA                0x0006;
  133.     const XLS_Type_EOF                    0x000a;
  134.     const XLS_Type_PROTECT                0x0012;
  135.     const XLS_Type_OBJECTPROTECT        0x0063;
  136.     const XLS_Type_SCENPROTECT            0x00dd;
  137.     const XLS_Type_PASSWORD                0x0013;
  138.     const XLS_Type_HEADER                0x0014;
  139.     const XLS_Type_FOOTER                0x0015;
  140.     const XLS_Type_EXTERNSHEET            0x0017;
  141.     const XLS_Type_DEFINEDNAME            0x0018;
  142.     const XLS_Type_VERTICALPAGEBREAKS    0x001a;
  143.     const XLS_Type_HORIZONTALPAGEBREAKS    0x001b;
  144.     const XLS_Type_NOTE                    0x001c;
  145.     const XLS_Type_SELECTION            0x001d;
  146.     const XLS_Type_DATEMODE                0x0022;
  147.     const XLS_Type_LEFTMARGIN            0x0026;
  148.     const XLS_Type_RIGHTMARGIN            0x0027;
  149.     const XLS_Type_TOPMARGIN            0x0028;
  150.     const XLS_Type_BOTTOMMARGIN            0x0029;
  151.     const XLS_Type_PRINTGRIDLINES        0x002b;
  152.     const XLS_Type_FILEPASS                0x002f;
  153.     const XLS_Type_FONT                    0x0031;
  154.     const XLS_Type_CONTINUE                0x003c;
  155.     const XLS_Type_PANE                    0x0041;
  156.     const XLS_Type_CODEPAGE                0x0042;
  157.     const XLS_Type_DEFCOLWIDTH             0x0055;
  158.     const XLS_Type_OBJ                    0x005d;
  159.     const XLS_Type_COLINFO                0x007d;
  160.     const XLS_Type_IMDATA                0x007f;
  161.     const XLS_Type_SHEETPR                0x0081;
  162.     const XLS_Type_HCENTER                0x0083;
  163.     const XLS_Type_VCENTER                0x0084;
  164.     const XLS_Type_SHEET                0x0085;
  165.     const XLS_Type_PALETTE                0x0092;
  166.     const XLS_Type_SCL                    0x00a0;
  167.     const XLS_Type_PAGESETUP            0x00a1;
  168.     const XLS_Type_MULRK                0x00bd;
  169.     const XLS_Type_MULBLANK                0x00be;
  170.     const XLS_Type_DBCELL                0x00d7;
  171.     const XLS_Type_XF                    0x00e0;
  172.     const XLS_Type_MERGEDCELLS            0x00e5;
  173.     const XLS_Type_MSODRAWINGGROUP        0x00eb;
  174.     const XLS_Type_MSODRAWING            0x00ec;
  175.     const XLS_Type_SST                    0x00fc;
  176.     const XLS_Type_LABELSST                0x00fd;
  177.     const XLS_Type_EXTSST                0x00ff;
  178.     const XLS_Type_EXTERNALBOOK            0x01ae;
  179.     const XLS_Type_TXO                    0x01b6;
  180.     const XLS_Type_HYPERLINK            0x01b8;
  181.     const XLS_Type_DIMENSION            0x0200;
  182.     const XLS_Type_BLANK                0x0201;
  183.     const XLS_Type_NUMBER                0x0203;
  184.     const XLS_Type_LABEL                0x0204;
  185.     const XLS_Type_BOOLERR                0x0205;
  186.     const XLS_Type_STRING                0x0207;
  187.     const XLS_Type_ROW                    0x0208;
  188.     const XLS_Type_INDEX                0x020b;
  189.     const XLS_Type_ARRAY                0x0221;
  190.     const XLS_Type_DEFAULTROWHEIGHT     0x0225;
  191.     const XLS_Type_WINDOW2                0x023e;
  192.     const XLS_Type_RK                    0x027e;
  193.     const XLS_Type_STYLE                0x0293;
  194.     const XLS_Type_FORMAT                0x041e;
  195.     const XLS_Type_SHAREDFMLA            0x04bc;
  196.     const XLS_Type_BOF                    0x0809;
  197.     const XLS_Type_SHEETPROTECTION        0x0867;
  198.     const XLS_Type_RANGEPROTECTION        0x0868;
  199.     const XLS_Type_SHEETLAYOUT            0x0862;
  200.     const XLS_Type_XFEXT                0x087d;
  201.     const XLS_Type_UNKNOWN                0xffff;
  202.  
  203.     /**
  204.      * Read data only?
  205.      *
  206.      * @var boolean 
  207.      */
  208.     private $_readDataOnly = false;
  209.  
  210.     /**
  211.      * Restict which sheets should be loaded?
  212.      *
  213.      * @var array 
  214.      */
  215.     private $_loadSheetsOnly = null;
  216.  
  217.     /**
  218.      * PHPExcel_Reader_IReadFilter instance
  219.      *
  220.      * @var PHPExcel_Reader_IReadFilter 
  221.      */
  222.     private $_readFilter = null;
  223.  
  224.     /**
  225.      * OLE reader
  226.      *
  227.      * @var PHPExcel_Shared_OLERead 
  228.      */
  229.     private $_ole;
  230.  
  231.     /**
  232.      * Stream data that is read. Includes workbook globals substream as well as sheet substreams
  233.      *
  234.      * @var string 
  235.      */
  236.     private $_data;
  237.  
  238.     /**
  239.      * Size in bytes of $this->_data
  240.      *
  241.      * @var int 
  242.      */
  243.     private $_dataSize;
  244.  
  245.     /**
  246.      * Current position in stream
  247.      *
  248.      * @var integer 
  249.      */
  250.     private $_pos;
  251.  
  252.     /**
  253.      * Workbook to be returned by the reader.
  254.      *
  255.      * @var PHPExcel 
  256.      */
  257.     private $_phpExcel;
  258.  
  259.     /**
  260.      * Worksheet that is currently being built by the reader.
  261.      *
  262.      * @var PHPExcel_Worksheet 
  263.      */
  264.     private $_phpSheet;
  265.  
  266.     /**
  267.      * BIFF version
  268.      *
  269.      * @var int 
  270.      */
  271.     private $_version;
  272.  
  273.     /**
  274.      * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
  275.      * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
  276.      *
  277.      * @var string 
  278.      */
  279.     private $_codepage;
  280.  
  281.     /**
  282.      * Shared formats
  283.      *
  284.      * @var array 
  285.      */
  286.     private $_formats;
  287.  
  288.     /**
  289.      * Shared fonts
  290.      *
  291.      * @var array 
  292.      */
  293.     private $_objFonts;
  294.  
  295.     /**
  296.      * Color palette
  297.      *
  298.      * @var array 
  299.      */
  300.     private $_palette;
  301.  
  302.     /**
  303.      * Worksheets
  304.      *
  305.      * @var array 
  306.      */
  307.     private $_sheets;
  308.  
  309.     /**
  310.      * External books
  311.      *
  312.      * @var array 
  313.      */
  314.     private $_externalBooks;
  315.  
  316.     /**
  317.      * REF structures. Only applies to BIFF8.
  318.      *
  319.      * @var array 
  320.      */
  321.     private $_ref;
  322.  
  323.     /**
  324.      * Defined names
  325.      *
  326.      * @var array 
  327.      */
  328.     private $_definedname;
  329.  
  330.     /**
  331.      * Shared strings. Only applies to BIFF8.
  332.      *
  333.      * @var array 
  334.      */
  335.     private $_sst;
  336.  
  337.     /**
  338.      * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
  339.      *
  340.      * @var boolean 
  341.      */
  342.     private $_frozen;
  343.  
  344.     /**
  345.      * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
  346.      *
  347.      * @var boolean 
  348.      */
  349.     private $_isFitToPages;
  350.  
  351.     /**
  352.      * Objects. One OBJ record contributes with one entry.
  353.      *
  354.      * @var array 
  355.      */
  356.     private $_objs;
  357.  
  358.     /**
  359.      * The combined MSODRAWINGGROUP data
  360.      *
  361.      * @var string 
  362.      */
  363.     private $_drawingGroupData;
  364.  
  365.     /**
  366.      * The combined MSODRAWING data (per sheet)
  367.      *
  368.      * @var string 
  369.      */
  370.     private $_drawingData;
  371.  
  372.     /**
  373.      * Keep track of XF index
  374.      *
  375.      * @var int 
  376.      */
  377.     private $_xfIndex;
  378.  
  379.     /**
  380.      * Mapping of XF index (that is a cell XF) to final index in cellXf collection
  381.      *
  382.      * @var array 
  383.      */
  384.     private $_mapCellXfIndex;
  385.  
  386.     /**
  387.      * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
  388.      *
  389.      * @var array 
  390.      */
  391.     private $_mapCellStyleXfIndex;
  392.  
  393.     /**
  394.      * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
  395.      *
  396.      * @var array 
  397.      */
  398.     private $_sharedFormulas;
  399.  
  400.     /**
  401.      * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
  402.      * refers to a shared formula.
  403.      *
  404.      * @var array 
  405.      */
  406.     private $_sharedFormulaParts;
  407.  
  408.     /**
  409.      * Read data only?
  410.      *
  411.      * @return boolean 
  412.      */
  413.     public function getReadDataOnly()
  414.     {
  415.         return $this->_readDataOnly;
  416.     }
  417.  
  418.     /**
  419.      * Set read data only
  420.      *
  421.      * @param boolean $pValue 
  422.      * @return PHPExcel_Reader_Excel5 
  423.      */
  424.     public function setReadDataOnly($pValue false)
  425.     {
  426.         $this->_readDataOnly = $pValue;
  427.         return $this;
  428.     }
  429.  
  430.     /**
  431.      * Get which sheets to load
  432.      *
  433.      * @return mixed 
  434.      */
  435.     public function getLoadSheetsOnly()
  436.     {
  437.         return $this->_loadSheetsOnly;
  438.     }
  439.  
  440.     /**
  441.      * Set which sheets to load
  442.      *
  443.      * @param mixed $value 
  444.      * @return PHPExcel_Reader_Excel5 
  445.      */
  446.     public function setLoadSheetsOnly($value null)
  447.     {
  448.         $this->_loadSheetsOnly = is_array($value?
  449.             $value array($value);
  450.         return $this;
  451.     }
  452.  
  453.     /**
  454.      * Set all sheets to load
  455.      *
  456.      * @return PHPExcel_Reader_Excel5 
  457.      */
  458.     public function setLoadAllSheets()
  459.     {
  460.         $this->_loadSheetsOnly = null;
  461.         return $this;
  462.     }
  463.  
  464.     /**
  465.      * Read filter
  466.      *
  467.      * @return PHPExcel_Reader_IReadFilter 
  468.      */
  469.     public function getReadFilter({
  470.         return $this->_readFilter;
  471.     }
  472.  
  473.     /**
  474.      * Set read filter
  475.      *
  476.      * @param PHPExcel_Reader_IReadFilter $pValue 
  477.      * @return PHPExcel_Reader_Excel5 
  478.      */
  479.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  480.         $this->_readFilter = $pValue;
  481.         return $this;
  482.     }
  483.  
  484.     /**
  485.      * Create a new PHPExcel_Reader_Excel5 instance
  486.      */
  487.     public function __construct({
  488.         $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  489.     }
  490.  
  491.     /**
  492.      * Can the current PHPExcel_Reader_IReader read the file?
  493.      *
  494.      * @param     string         $pFileName 
  495.      * @return     boolean 
  496.      */
  497.     public function canRead($pFilename)
  498.     {
  499.         // Check if file exists
  500.         if (!file_exists($pFilename)) {
  501.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  502.         }
  503.  
  504.         try {
  505.             // Use ParseXL for the hard work.
  506.             $this->_ole = new PHPExcel_Shared_OLERead();
  507.  
  508.             // get excel data
  509.             $res $this->_ole->read($pFilename);
  510.             return true;
  511.  
  512.         catch (Exception $e{
  513.             return false;
  514.         }
  515.     }
  516.  
  517.     /**
  518.      * Loads PHPExcel from file
  519.      *
  520.      * @param     string         $pFilename 
  521.      * @throws     Exception
  522.      */
  523.     public function load($pFilename)
  524.     {
  525.         // Initialisations
  526.         $this->_phpExcel = new PHPExcel;
  527.         $this->_phpExcel->removeSheetByIndex(0)// remove 1st sheet
  528.         if (!$this->_readDataOnly{
  529.             $this->_phpExcel->removeCellStyleXfByIndex(0)// remove the default style
  530.             $this->_phpExcel->removeCellXfByIndex(0)// remove the default style
  531.         }
  532.  
  533.         // Use ParseXL for the hard work.
  534.         $this->_ole = new PHPExcel_Shared_OLERead();
  535.  
  536.         // get excel data
  537.         $res $this->_ole->read($pFilename);
  538.         $this->_data = $this->_ole->getWorkBook();
  539.  
  540.         // total byte size of Excel data (workbook global substream + sheet substreams)
  541.         $this->_dataSize = strlen($this->_data);
  542.  
  543.         // initialize
  544.         $this->_pos                    = 0;
  545.         $this->_codepage            = 'CP1252';
  546.         $this->_formats                = array();
  547.         $this->_objFonts            = array();
  548.         $this->_palette                = array();
  549.         $this->_sheets                = array();
  550.         $this->_externalBooks        = array();
  551.         $this->_ref                    = array();
  552.         $this->_definedname            = array();
  553.         $this->_sst                    = array();
  554.         $this->_drawingGroupData    = '';
  555.         $this->_xfIndex                = '';
  556.         $this->_mapCellXfIndex        = array();
  557.         $this->_mapCellStyleXfIndex    = array();
  558.  
  559.         // Parse Workbook Global Substream
  560.         while ($this->_pos < $this->_dataSize{
  561.             $code $this->_GetInt2d($this->_data$this->_pos);
  562.  
  563.             switch ($code{
  564.                 case self::XLS_Type_BOF:
  565.                     $pos $this->_pos;
  566.                     $length $this->_GetInt2d($this->_data$pos 2);
  567.                     $recordData substr($this->_data$pos 4$length);
  568.  
  569.                     // offset: 0; size: 2; BIFF version
  570.                     $this->_version = $this->_GetInt2d($this->_data$pos 4);
  571.  
  572.                     if (($this->_version != self::XLS_BIFF8&& ($this->_version != self::XLS_BIFF7)) {
  573.                         return false;
  574.                     }
  575.  
  576.                     // offset: 2; size: 2; type of stream
  577.                     $substreamType $this->_GetInt2d($this->_data$pos 6);
  578.                     if ($substreamType != self::XLS_WorkbookGlobals{
  579.                         return false;
  580.                     }
  581.                     $this->_pos += $length;
  582.                     break;
  583.  
  584.                 case self::XLS_Type_FILEPASS:        $this->_readFilepass();            break;
  585.                 case self::XLS_Type_CODEPAGE:        $this->_readCodepage();            break;
  586.                 case self::XLS_Type_DATEMODE:        $this->_readDateMode();            break;
  587.                 case self::XLS_Type_FONT:            $this->_readFont();                break;
  588.                 case self::XLS_Type_FORMAT:            $this->_readFormat();            break;
  589.                 case self::XLS_Type_XF:                $this->_readXf();                break;
  590.                 case self::XLS_Type_XFEXT:            $this->_readXfExt();            break;
  591.                 case self::XLS_Type_STYLE:            $this->_readStyle();            break;
  592.                 case self::XLS_Type_PALETTE:        $this->_readPalette();            break;
  593.                 case self::XLS_Type_SHEET:            $this->_readSheet();            break;
  594.                 case self::XLS_Type_EXTERNALBOOK:    $this->_readExternalBook();        break;
  595.                 case self::XLS_Type_EXTERNSHEET:    $this->_readExternSheet();        break;
  596.                 case self::XLS_Type_DEFINEDNAME:    $this->_readDefinedName();        break;
  597.                 case self::XLS_Type_MSODRAWINGGROUP:    $this->_readMsoDrawingGroup();    break;
  598.                 case self::XLS_Type_SST:            $this->_readSst();                break;
  599.                 case self::XLS_Type_EOF:            $this->_readDefault();            break 2;
  600.                 default:                            $this->_readDefault();            break;
  601.             }
  602.         }
  603.  
  604.         // Resolve indexed colors for font, fill, and border colors
  605.         // Cannot be resolved already in XF record, because PALETTE record comes afterwards
  606.         if (!$this->_readDataOnly{
  607.             foreach ($this->_objFonts as $objFont{
  608.                 if (isset($objFont->colorIndex)) {
  609.                     $color $this->_readColor($objFont->colorIndex);
  610.                     $objFont->getColor()->setRGB($color['rgb']);
  611.                 }
  612.             }
  613.  
  614.             foreach ($this->_phpExcel->getCellXfCollection(as $objStyle{
  615.                 // fill start and end color
  616.                 $fill $objStyle->getFill();
  617.  
  618.                 if (isset($fill->startcolorIndex)) {
  619.                     $startColor $this->_readColor($fill->startcolorIndex);
  620.                     $fill->getStartColor()->setRGB($startColor['rgb']);
  621.                 }
  622.  
  623.                 if (isset($fill->endcolorIndex)) {
  624.                     $endColor $this->_readColor($fill->endcolorIndex);
  625.                     $fill->getEndColor()->setRGB($endColor['rgb']);
  626.                 }
  627.  
  628.                 // border colors
  629.                 $top      $objStyle->getBorders()->getTop();
  630.                 $right    $objStyle->getBorders()->getRight();
  631.                 $bottom   $objStyle->getBorders()->getBottom();
  632.                 $left     $objStyle->getBorders()->getLeft();
  633.                 $diagonal $objStyle->getBorders()->getDiagonal();
  634.  
  635.                 if (isset($top->colorIndex)) {
  636.                     $borderTopColor $this->_readColor($top->colorIndex);
  637.                     $top->getColor()->setRGB($borderTopColor['rgb']);
  638.                 }
  639.  
  640.                 if (isset($right->colorIndex)) {
  641.                     $borderRightColor $this->_readColor($right->colorIndex);
  642.                     $right->getColor()->setRGB($borderRightColor['rgb']);
  643.                 }
  644.  
  645.                 if (isset($bottom->colorIndex)) {
  646.                     $borderBottomColor $this->_readColor($bottom->colorIndex);
  647.                     $bottom->getColor()->setRGB($borderBottomColor['rgb']);
  648.                 }
  649.  
  650.                 if (isset($left->colorIndex)) {
  651.                     $borderLeftColor $this->_readColor($left->colorIndex);
  652.                     $left->getColor()->setRGB($borderLeftColor['rgb']);
  653.                 }
  654.  
  655.                 if (isset($diagonal->colorIndex)) {
  656.                     $borderDiagonalColor $this->_readColor($diagonal->colorIndex);
  657.                     $diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
  658.                 }
  659.             }
  660.         }
  661.  
  662.         // treat MSODRAWINGGROUP records, workbook-level Escher
  663.         if (!$this->_readDataOnly && $this->_drawingGroupData{
  664.             $escherWorkbook new PHPExcel_Shared_Escher();
  665.             $reader new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
  666.             $escherWorkbook $reader->load($this->_drawingGroupData);
  667.  
  668.             // debug Escher stream
  669.             //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  670.             //$debug->load($this->_drawingGroupData);
  671.         }
  672.  
  673.         // Parse the individual sheets
  674.         foreach ($this->_sheets as $sheet{
  675.  
  676.             // check if sheet should be skipped
  677.             if (isset($this->_loadSheetsOnly&& !in_array($sheet['name']$this->_loadSheetsOnly)) {
  678.                 continue;
  679.             }
  680.  
  681.             // add sheet to PHPExcel object
  682.             $this->_phpSheet = $this->_phpExcel->createSheet();
  683.             $this->_phpSheet->setTitle($sheet['name']);
  684.             $this->_phpSheet->setSheetState($sheet['sheetState']);
  685.  
  686.             $this->_pos = $sheet['offset'];
  687.  
  688.             // Initialize isFitToPages. May change after reading SHEETPR record.
  689.             $this->_isFitToPages = false;
  690.  
  691.             // Initialize drawingData
  692.             $this->_drawingData = '';
  693.  
  694.             // Initialize objs
  695.             $this->_objs = array();
  696.  
  697.             // Initialize shared formula parts
  698.             $this->_sharedFormulaParts = array();
  699.  
  700.             // Initialize shared formulas
  701.             $this->_sharedFormulas = array();
  702.  
  703.             while ($this->_pos < $this->_dataSize{
  704.                 $code $this->_GetInt2d($this->_data$this->_pos);
  705.  
  706.                 switch ($code{
  707.                     case self::XLS_Type_BOF:
  708.                         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  709.                         $recordData substr($this->_data$this->_pos + 4$length);
  710.  
  711.                         // move stream pointer to next record
  712.                         $this->_pos += $length;
  713.  
  714.                         // do not use this version information for anything
  715.                         // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
  716.  
  717.                         // offset: 2; size: 2; type of the following data
  718.                         $substreamType $this->_GetInt2d($recordData2);
  719.                         if ($substreamType != self::XLS_Worksheet{
  720.                             break 2;
  721.                         }
  722.                         break;
  723.  
  724.                     case self::XLS_Type_PRINTGRIDLINES:            $this->_readPrintGridlines();            break;
  725.                     case self::XLS_Type_DEFAULTROWHEIGHT:        $this->_readDefaultRowHeight();            break;
  726.                     case self::XLS_Type_SHEETPR:                $this->_readSheetPr();                    break;
  727.                     case self::XLS_Type_HORIZONTALPAGEBREAKS:    $this->_readHorizontalPageBreaks();        break;
  728.                     case self::XLS_Type_VERTICALPAGEBREAKS:        $this->_readVerticalPageBreaks();        break;
  729.                     case self::XLS_Type_HEADER:                    $this->_readHeader();                    break;
  730.                     case self::XLS_Type_FOOTER:                    $this->_readFooter();                    break;
  731.                     case self::XLS_Type_HCENTER:                $this->_readHcenter();                    break;
  732.                     case self::XLS_Type_VCENTER:                $this->_readVcenter();                    break;
  733.                     case self::XLS_Type_LEFTMARGIN:                $this->_readLeftMargin();                break;
  734.                     case self::XLS_Type_RIGHTMARGIN:            $this->_readRightMargin();                break;
  735.                     case self::XLS_Type_TOPMARGIN:                $this->_readTopMargin();                break;
  736.                     case self::XLS_Type_BOTTOMMARGIN:            $this->_readBottomMargin();                break;
  737.                     case self::XLS_Type_PAGESETUP:                $this->_readPageSetup();                break;
  738.                     case self::XLS_Type_PROTECT:                $this->_readProtect();                    break;
  739.                     case self::XLS_Type_SCENPROTECT:            $this->_readScenProtect();                break;
  740.                     case self::XLS_Type_OBJECTPROTECT:            $this->_readObjectProtect();            break;
  741.                     case self::XLS_Type_PASSWORD:                $this->_readPassword();                    break;
  742.                     case self::XLS_Type_DEFCOLWIDTH:            $this->_readDefColWidth();                break;
  743.                     case self::XLS_Type_COLINFO:                $this->_readColInfo();                    break;
  744.                     case self::XLS_Type_DIMENSION:                $this->_readDefault();                    break;
  745.                     case self::XLS_Type_ROW:                    $this->_readRow();                        break;
  746.                     case self::XLS_Type_DBCELL:                    $this->_readDefault();                    break;
  747.                     case self::XLS_Type_RK:                        $this->_readRk();                        break;
  748.                     case self::XLS_Type_LABELSST:                $this->_readLabelSst();                    break;
  749.                     case self::XLS_Type_MULRK:                    $this->_readMulRk();                    break;
  750.                     case self::XLS_Type_NUMBER:                    $this->_readNumber();                    break;
  751.                     case self::XLS_Type_FORMULA:                $this->_readFormula();                    break;
  752.                     case self::XLS_Type_SHAREDFMLA:                $this->_readSharedFmla();                break;
  753.                     case self::XLS_Type_BOOLERR:                $this->_readBoolErr();                    break;
  754.                     case self::XLS_Type_MULBLANK:                $this->_readMulBlank();                    break;
  755.                     case self::XLS_Type_LABEL:                    $this->_readLabel();                    break;
  756.                     case self::XLS_Type_BLANK:                    $this->_readBlank();                    break;
  757.                     case self::XLS_Type_MSODRAWING:                $this->_readMsoDrawing();                break;
  758.                     case self::XLS_Type_OBJ:                    $this->_readObj();                        break;
  759.                     case self::XLS_Type_WINDOW2:                $this->_readWindow2();                    break;
  760.                     case self::XLS_Type_SCL:                    $this->_readScl();                        break;
  761.                     case self::XLS_Type_PANE:                    $this->_readPane();                        break;
  762.                     case self::XLS_Type_SELECTION:                $this->_readSelection();                break;
  763.                     case self::XLS_Type_MERGEDCELLS:            $this->_readMergedCells();                break;
  764.                     case self::XLS_Type_HYPERLINK:                $this->_readHyperLink();                break;
  765.                     case self::XLS_Type_SHEETLAYOUT:            $this->_readSheetLayout();                break;
  766.                     case self::XLS_Type_SHEETPROTECTION:        $this->_readSheetProtection();            break;
  767.                     case self::XLS_Type_RANGEPROTECTION:        $this->_readRangeProtection();            break;
  768.                     //case self::XLS_Type_IMDATA:                $this->_readImData();                    break;
  769.                     case self::XLS_Type_CONTINUE:                $this->_readContinue();                    break;
  770.                     case self::XLS_Type_EOF:                    $this->_readDefault();                    break 2;
  771.                     default:                                    $this->_readDefault();                    break;
  772.                 }
  773.  
  774.             }
  775.  
  776.             // treat MSODRAWING records, sheet-level Escher
  777.             if (!$this->_readDataOnly && $this->_drawingData{
  778.                 $escherWorksheet new PHPExcel_Shared_Escher();
  779.                 $reader new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
  780.                 $escherWorksheet $reader->load($this->_drawingData);
  781.  
  782.                 // debug Escher stream
  783.                 //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  784.                 //$debug->load($this->_drawingData);
  785.  
  786.                 // get all spContainers in one long array, so they can be mapped to OBJ records
  787.                 $allSpContainers $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
  788.             }
  789.  
  790.             // treat OBJ records
  791.             foreach ($this->_objs as $n => $obj{
  792.  
  793.                 // the first shape container never has a corresponding OBJ record, hence $n + 1
  794.                 $spContainer $allSpContainers[$n 1];
  795.  
  796.                 // we skip all spContainers that are a part of a group shape since we cannot yet handle those
  797.                 if ($spContainer->getNestingLevel(1{
  798.                     continue;
  799.                 }
  800.  
  801.                 // calculate the width and height of the shape
  802.                 list($startColumn$startRowPHPExcel_Cell::coordinateFromString($spContainer->getStartCoordinates());
  803.                 list($endColumn$endRowPHPExcel_Cell::coordinateFromString($spContainer->getEndCoordinates());
  804.  
  805.                 $startOffsetX $spContainer->getStartOffsetX();
  806.                 $startOffsetY $spContainer->getStartOffsetY();
  807.                 $endOffsetX $spContainer->getEndOffsetX();
  808.                 $endOffsetY $spContainer->getEndOffsetY();
  809.  
  810.                 $width PHPExcel_Shared_Excel5::getDistanceX($this->_phpSheet$startColumn$startOffsetX$endColumn$endOffsetX);
  811.                 $height PHPExcel_Shared_Excel5::getDistanceY($this->_phpSheet$startRow$startOffsetY$endRow$endOffsetY);
  812.  
  813.                 // calculate offsetX and offsetY of the shape
  814.                 $offsetX $startOffsetX PHPExcel_Shared_Excel5::sizeCol($this->_phpSheet$startColumn1024;
  815.                 $offsetY $startOffsetY PHPExcel_Shared_Excel5::sizeRow($this->_phpSheet$startRow256;
  816.  
  817.                 switch ($obj['type']{
  818.  
  819.                 case 0x08:
  820.                     // picture
  821.  
  822.                     // get index to BSE entry (1-based)
  823.                     $BSEindex $spContainer->getOPT(0x0104);
  824.                     $BSECollection $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
  825.                     $BSE $BSECollection[$BSEindex 1];
  826.                     $blipType $BSE->getBlipType();
  827.  
  828.                     // need check because some blip types are not supported by Escher reader such as EMF
  829.                     if ($blip $BSE->getBlip()) {
  830.                         $ih imagecreatefromstring($blip->getData());
  831.                         $drawing new PHPExcel_Worksheet_MemoryDrawing();
  832.                         $drawing->setImageResource($ih);
  833.  
  834.                         // width, height, offsetX, offsetY
  835.                         $drawing->setResizeProportional(false);
  836.                         $drawing->setWidth($width);
  837.                         $drawing->setHeight($height);
  838.                         $drawing->setOffsetX($offsetX);
  839.                         $drawing->setOffsetY($offsetY);
  840.  
  841.                         switch ($blipType{
  842.  
  843.                         case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
  844.                             $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
  845.                             $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG);
  846.                             break;
  847.  
  848.                         case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
  849.                             $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
  850.                             $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG);
  851.                             break;
  852.                         }
  853.  
  854.                         $drawing->setWorksheet($this->_phpSheet);
  855.                         $drawing->setCoordinates($spContainer->getStartCoordinates());
  856.                     }
  857.  
  858.                     break;
  859.  
  860.                 default:
  861.                     // other object type
  862.                     break;
  863.  
  864.                 }
  865.             }
  866.  
  867.             // treat SHAREDFMLA records
  868.             if ($this->_version == self::XLS_BIFF8{
  869.                 foreach ($this->_sharedFormulaParts as $cell => $baseCell{
  870.                     $formula $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell]$cell);
  871.                     $this->_phpSheet->getCell($cell)->setValueExplicit('=' $formulaPHPExcel_Cell_DataType::TYPE_FORMULA);
  872.                 }
  873.             }
  874.         }
  875.  
  876.         // add the named ranges (defined names)
  877.         foreach ($this->_definedname as $definedName{
  878.             if ($definedName['isBuiltInName']{
  879.                 switch ($definedName['name']{
  880.  
  881.                 case pack('C'0x06):
  882.                     // print area
  883.                     //    in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
  884.  
  885.                     $ranges explode(','$definedName['formula'])// FIXME: what if sheetname contains comma?
  886.  
  887.                     $extractedRanges array();
  888.                     foreach ($ranges as $range{
  889.                         // $range should look like one of these
  890.                         //        Foo!$C$7:$J$66
  891.                         //        Bar!$A$1:$IV$2
  892.  
  893.                         $explodes explode('!'$range);
  894.                         $sheetName $explodes[0];
  895.  
  896.                         if (count($explodes== 2{
  897.                             $extractedRanges[str_replace('$'''$explodes[1])// C7:J66
  898.                         }
  899.                     }
  900.                     if ($docSheet $this->_phpExcel->getSheetByName($sheetName)) {
  901.                         $docSheet->getPageSetup()->setPrintArea(implode(','$extractedRanges))// C7:J66,A1:IV2
  902.                     }
  903.                     break;
  904.  
  905.                 case pack('C'0x07):
  906.                     // print titles (repeating rows)
  907.                     // Assuming BIFF8, there are 3 cases
  908.                     // 1. repeating rows
  909.                     //        formula looks like this: Sheet!$A$1:$IV$2
  910.                     //        rows 1-2 repeat
  911.                     // 2. repeating columns
  912.                     //        formula looks like this: Sheet!$A$1:$B$65536
  913.                     //        columns A-B repeat
  914.                     // 3. both repeating rows and repeating columns
  915.                     //        formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
  916.  
  917.                     $ranges explode(','$definedName['formula'])// FIXME: what if sheetname contains comma?
  918.  
  919.                     foreach ($ranges as $range{
  920.                         // $range should look like this one of these
  921.                         //        Sheet!$A$1:$B$65536
  922.                         //        Sheet!$A$1:$IV$2
  923.  
  924.                         $explodes explode('!'$range);
  925.  
  926.                         if (count($explodes== 2{
  927.                             if ($docSheet $this->_phpExcel->getSheetByName($explodes[0])) {
  928.  
  929.                                 $extractedRange $explodes[1];
  930.                                 $extractedRange str_replace('$'''$extractedRange);
  931.  
  932.                                 $coordinateStrings explode(':'$extractedRange);
  933.                                 if (count($coordinateStrings== 2{
  934.                                     list($firstColumn$firstRowPHPExcel_Cell::coordinateFromString($coordinateStrings[0]);
  935.                                     list($lastColumn$lastRowPHPExcel_Cell::coordinateFromString($coordinateStrings[1]);
  936.  
  937.                                     if ($firstColumn == 'A' and $lastColumn == 'IV'{
  938.                                         // then we have repeating rows
  939.                                         $docSheet->getPageSetup()->setRowsToRepeatAtTop(array($firstRow$lastRow));
  940.                                     elseif ($firstRow == and $lastRow == 65536{
  941.                                         // then we have repeating columns
  942.                                         $docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($firstColumn$lastColumn));
  943.                                     }
  944.                                 }
  945.                             }
  946.                         }
  947.                     }
  948.                     break;
  949.  
  950.                 }
  951.             else {
  952.                 // Extract range
  953.                 $explodes explode('!'$definedName['formula']);
  954.  
  955.                 if (count($explodes== 2{
  956.                     if ($docSheet $this->_phpExcel->getSheetByName($explodes[0])) {
  957.                         $extractedRange $explodes[1];
  958.                         $extractedRange str_replace('$'''$extractedRange);
  959.  
  960.                         $this->_phpExcel->addNamedRangenew PHPExcel_NamedRange((string)$definedName['name']$docSheet$extractedRangefalse) );
  961.                     }
  962.                 }
  963.             }
  964.         }
  965.  
  966.         return $this->_phpExcel;
  967.     }
  968.  
  969.     /**
  970.      * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
  971.      */
  972.     private function _readDefault()
  973.     {
  974.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  975.         $recordData substr($this->_data$this->_pos + 4$length);
  976.  
  977.         // move stream pointer to next record
  978.         $this->_pos += $length;
  979.     }
  980.  
  981.     /**
  982.      * FILEPASS
  983.      *
  984.      * This record is part of the File Protection Block. It
  985.      * contains information about the read/write password of the
  986.      * file. All record contents following this record will be
  987.      * encrypted.
  988.      *
  989.      * --    "OpenOffice.org's Documentation of the Microsoft
  990.      *         Excel File Format"
  991.      */
  992.     private function _readFilepass()
  993.     {
  994.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  995.         $recordData substr($this->_data$this->_pos + 4$length);
  996.  
  997.         // move stream pointer to next record
  998.         $this->_pos += $length;
  999.  
  1000.         throw new Exception('Cannot read encrypted file');
  1001.     }
  1002.  
  1003.     /**
  1004.      * CODEPAGE
  1005.      *
  1006.      * This record stores the text encoding used to write byte
  1007.      * strings, stored as MS Windows code page identifier.
  1008.      *
  1009.      * --    "OpenOffice.org's Documentation of the Microsoft
  1010.      *         Excel File Format"
  1011.      */
  1012.     private function _readCodepage()
  1013.     {
  1014.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1015.         $recordData substr($this->_data$this->_pos + 4$length);
  1016.  
  1017.         // move stream pointer to next record
  1018.         $this->_pos += $length;
  1019.  
  1020.         // offset: 0; size: 2; code page identifier
  1021.         $codepage $this->_GetInt2d($recordData0);
  1022.  
  1023.         switch ($codepage{
  1024.  
  1025.         case 367// ASCII
  1026.             $this->_codepage ="ASCII";
  1027.             break;
  1028.  
  1029.         case 437//OEM US
  1030.             $this->_codepage ="CP437";
  1031.             break;
  1032.  
  1033.         case 720//OEM Arabic
  1034.             // currently not supported by libiconv
  1035.             $this->_codepage = "";
  1036.             break;
  1037.  
  1038.         case 737//OEM Greek
  1039.             $this->_codepage ="CP737";
  1040.             break;
  1041.  
  1042.         case 775//OEM Baltic
  1043.             $this->_codepage ="CP775";
  1044.             break;
  1045.  
  1046.         case 850//OEM Latin I
  1047.             $this->_codepage ="CP850";
  1048.             break;
  1049.  
  1050.         case 852//OEM Latin II (Central European)
  1051.             $this->_codepage ="CP852";
  1052.             break;
  1053.  
  1054.         case 855//OEM Cyrillic
  1055.             $this->_codepage ="CP855";
  1056.             break;
  1057.  
  1058.         case 857//OEM Turkish
  1059.             $this->_codepage ="CP857";
  1060.             break;
  1061.  
  1062.         case 858//OEM Multilingual Latin I with Euro
  1063.             $this->_codepage ="CP858";
  1064.             break;
  1065.  
  1066.         case 860//OEM Portugese
  1067.             $this->_codepage ="CP860";
  1068.             break;
  1069.  
  1070.         case 861//OEM Icelandic
  1071.             $this->_codepage ="CP861";
  1072.             break;
  1073.  
  1074.         case 862//OEM Hebrew
  1075.             $this->_codepage ="CP862";
  1076.             break;
  1077.  
  1078.         case 863//OEM Canadian (French)
  1079.             $this->_codepage ="CP863";
  1080.             break;
  1081.  
  1082.         case 864//OEM Arabic
  1083.             $this->_codepage ="CP864";
  1084.             break;
  1085.  
  1086.         case 865//OEM Nordic
  1087.             $this->_codepage ="CP865";
  1088.             break;
  1089.  
  1090.         case 866//OEM Cyrillic (Russian)
  1091.             $this->_codepage ="CP866";
  1092.             break;
  1093.  
  1094.         case 869//OEM Greek (Modern)
  1095.             $this->_codepage ="CP869";
  1096.             break;
  1097.  
  1098.         case 874//ANSI Thai
  1099.             $this->_codepage ="CP874";
  1100.             break;
  1101.  
  1102.         case 932//ANSI Japanese Shift-JIS
  1103.             $this->_codepage ="CP932";
  1104.             break;
  1105.  
  1106.         case 936//ANSI Chinese Simplified GBK
  1107.             $this->_codepage ="CP936";
  1108.             break;
  1109.  
  1110.         case 949//ANSI Korean (Wansung)
  1111.             $this->_codepage ="CP949";
  1112.             break;
  1113.  
  1114.         case 950//ANSI Chinese Traditional BIG5
  1115.             $this->_codepage ="CP950";
  1116.             break;
  1117.  
  1118.         case 1200//UTF-16 (BIFF8)
  1119.             $this->_codepage ="UTF-16LE";
  1120.             break;
  1121.  
  1122.         case 1250:// ANSI Latin II (Central European)
  1123.             $this->_codepage ="CP1250";
  1124.             break;
  1125.  
  1126.         case 1251//ANSI Cyrillic
  1127.             $this->_codepage ="CP1251";
  1128.             break;
  1129.  
  1130.         case 1252//ANSI Latin I (BIFF4-BIFF7)
  1131.             $this->_codepage ="CP1252";
  1132.             break;
  1133.  
  1134.         case 1253//ANSI Greek
  1135.             $this->_codepage ="CP1253";
  1136.             break;
  1137.  
  1138.         case 1254//ANSI Turkish
  1139.             $this->_codepage ="CP1254";
  1140.             break;
  1141.  
  1142.         case 1255//ANSI Hebrew
  1143.             $this->_codepage ="CP1255";
  1144.             break;
  1145.  
  1146.         case 1256//ANSI Arabic
  1147.             $this->_codepage ="CP1256";
  1148.             break;
  1149.  
  1150.         case 1257//ANSI Baltic
  1151.             $this->_codepage ="CP1257";
  1152.             break;
  1153.  
  1154.         case 1258//ANSI Vietnamese
  1155.             $this->_codepage ="CP1258";
  1156.             break;
  1157.  
  1158.         case 1361//ANSI Korean (Johab)
  1159.             $this->_codepage ="CP1361";
  1160.             break;
  1161.  
  1162.         case 10000//Apple Roman
  1163.             $this->_codepage = 'MAC';
  1164.             break;
  1165.  
  1166.         case 32768//Apple Roman
  1167.             $this->_codepage = 'MAC';
  1168.             break;
  1169.  
  1170.         case 32769//ANSI Latin I (BIFF2-BIFF3)
  1171.             // currently not supported by libiconv
  1172.             $this->_codepage = "";
  1173.             break;
  1174.  
  1175.         }
  1176.     }
  1177.  
  1178.     /**
  1179.      * DATEMODE
  1180.      *
  1181.      * This record specifies the base date for displaying date
  1182.      * values. All dates are stored as count of days past this
  1183.      * base date. In BIFF2-BIFF4 this record is part of the
  1184.      * Calculation Settings Block. In BIFF5-BIFF8 it is
  1185.      * stored in the Workbook Globals Substream.
  1186.      *
  1187.      * --    "OpenOffice.org's Documentation of the Microsoft
  1188.      *         Excel File Format"
  1189.      */
  1190.     private function _readDateMode()
  1191.     {
  1192.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1193.         $recordData substr($this->_data$this->_pos + 4$length);
  1194.  
  1195.         // move stream pointer to next record
  1196.         $this->_pos += $length;
  1197.  
  1198.         // offset: 0; size: 2; 0 = base 1900, 1 = base 1904
  1199.         PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  1200.         if (ord($recordData{0}== 1{
  1201.             PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  1202.         }
  1203.     }
  1204.  
  1205.     /**
  1206.      * Read a FONT record
  1207.      */
  1208.     private function _readFont()
  1209.     {
  1210.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1211.         $recordData substr($this->_data$this->_pos + 4$length);
  1212.  
  1213.         // move stream pointer to next record
  1214.         $this->_pos += $length;
  1215.  
  1216.         if (!$this->_readDataOnly{
  1217.             $objFont new PHPExcel_Style_Font();
  1218.  
  1219.             // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
  1220.             $size $this->_GetInt2d($recordData0);
  1221.             $objFont->setSize($size 20);
  1222.  
  1223.             // offset: 2; size: 2; option flags
  1224.                 // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
  1225.                 // bit: 1; mask 0x0002; italic
  1226.                 $isItalic (0x0002 $this->_GetInt2d($recordData2)) >> 1;
  1227.                 if ($isItalic$objFont->setItalic(true);
  1228.  
  1229.                 // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
  1230.                 // bit: 3; mask 0x0008; strike
  1231.                 $isStrike (0x0008 $this->_GetInt2d($recordData2)) >> 3;
  1232.                 if ($isStrike$objFont->setStrikethrough(true);
  1233.  
  1234.             // offset: 4; size: 2; colour index
  1235.             $colorIndex $this->_GetInt2d($recordData4);
  1236.             $objFont->colorIndex $colorIndex;
  1237.  
  1238.             // offset: 6; size: 2; font weight
  1239.             $weight $this->_GetInt2d($recordData6);
  1240.             switch ($weight{
  1241.                 case 0x02BC:
  1242.                     $objFont->setBold(true);
  1243.                     break;
  1244.             }
  1245.  
  1246.             // offset: 8; size: 2; escapement type
  1247.             $escapement $this->_GetInt2d($recordData8);
  1248.             switch ($escapement{
  1249.                 case 0x0001:
  1250.                     $objFont->setSuperScript(true);
  1251.                     break;
  1252.                 case 0x0002:
  1253.                     $objFont->setSubScript(true);
  1254.                     break;
  1255.             }
  1256.  
  1257.             // offset: 10; size: 1; underline type
  1258.             $underlineType ord($recordData{10});
  1259.             switch ($underlineType{
  1260.                 case 0x00:
  1261.                     break// no underline
  1262.                 case 0x01:
  1263.                     $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  1264.                     break;
  1265.                 case 0x02:
  1266.                     $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  1267.                     break;
  1268.                 case 0x21:
  1269.                     $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING);
  1270.                     break;
  1271.                 case 0x22:
  1272.                     $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING);
  1273.                     break;
  1274.             }
  1275.  
  1276.             // offset: 11; size: 1; font family
  1277.             // offset: 12; size: 1; character set
  1278.             // offset: 13; size: 1; not used
  1279.             // offset: 14; size: var; font name
  1280.             if ($this->_version == self::XLS_BIFF8{
  1281.                 $string $this->_readUnicodeStringShort(substr($recordData14));
  1282.             else {
  1283.                 $string $this->_readByteStringShort(substr($recordData14));
  1284.             }
  1285.             $objFont->setName($string['value']);
  1286.  
  1287.             $this->_objFonts[$objFont;
  1288.         }
  1289.     }
  1290.  
  1291.     /**
  1292.      * FORMAT
  1293.      *
  1294.      * This record contains information about a number format.
  1295.      * All FORMAT records occur together in a sequential list.
  1296.      *
  1297.      * In BIFF2-BIFF4 other records referencing a FORMAT record
  1298.      * contain a zero-based index into this list. From BIFF5 on
  1299.      * the FORMAT record contains the index itself that will be
  1300.      * used by other records.
  1301.      *
  1302.      * --    "OpenOffice.org's Documentation of the Microsoft
  1303.      *         Excel File Format"
  1304.      */
  1305.     private function _readFormat()
  1306.     {
  1307.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1308.         $recordData substr($this->_data$this->_pos + 4$length);
  1309.  
  1310.         // move stream pointer to next record
  1311.         $this->_pos += $length;
  1312.  
  1313.         if (!$this->_readDataOnly{
  1314.             $indexCode $this->_GetInt2d($recordData0);
  1315.  
  1316.             if ($this->_version == self::XLS_BIFF8{
  1317.                 $string $this->_readUnicodeStringLong(substr($recordData2));
  1318.             else {
  1319.                 // BIFF7
  1320.                 $string $this->_readByteStringShort(substr($recordData2));
  1321.             }
  1322.  
  1323.             $formatString $string['value'];
  1324.             $this->_formats[$indexCode$formatString;
  1325.         }
  1326.     }
  1327.  
  1328.     /**
  1329.      * XF - Extended Format
  1330.      *
  1331.      * This record contains formatting information for cells, rows, columns or styles.
  1332.      * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
  1333.      * and 1 cell XF.
  1334.      * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
  1335.      * and XF record 15 is a cell XF
  1336.      * We only read the first cell style XF and skip the remaining cell style XF records
  1337.      * We read all cell XF records.
  1338.      *
  1339.      * --    "OpenOffice.org's Documentation of the Microsoft
  1340.      *         Excel File Format"
  1341.      */
  1342.     private function _readXf()
  1343.     {
  1344.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1345.         $recordData substr($this->_data$this->_pos + 4$length);
  1346.  
  1347.         // move stream pointer to next record
  1348.         $this->_pos += $length;
  1349.  
  1350.         $objStyle new PHPExcel_Style();
  1351.  
  1352.         if (!$this->_readDataOnly{
  1353.             // offset:  0; size: 2; Index to FONT record
  1354.             if ($this->_GetInt2d($recordData04{
  1355.                 $fontIndex $this->_GetInt2d($recordData0);
  1356.             else {
  1357.                 // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1358.                 // check the OpenOffice documentation of the FONT record
  1359.                 $fontIndex $this->_GetInt2d($recordData01;
  1360.             }
  1361.             $objStyle->setFont($this->_objFonts[$fontIndex]);
  1362.  
  1363.             // offset:  2; size: 2; Index to FORMAT record
  1364.             $numberFormatIndex $this->_GetInt2d($recordData2);
  1365.             if (isset($this->_formats[$numberFormatIndex])) {
  1366.                 // then we have user-defined format code
  1367.                 $numberformat array('code' => $this->_formats[$numberFormatIndex]);
  1368.             elseif (($code PHPExcel_Style_NumberFormat::builtInFormatCode($numberFormatIndex)) !== ''{
  1369.                 // then we have built-in format code
  1370.                 $numberformat array('code' => $code);
  1371.             else {
  1372.                 // we set the general format code
  1373.                 $numberformat array('code' => 'General');
  1374.             }
  1375.             $objStyle->getNumberFormat()->setFormatCode($numberformat['code']);
  1376.  
  1377.             // offset:  4; size: 2; XF type, cell protection, and parent style XF
  1378.             // bit 2-0; mask 0x0007; XF_TYPE_PROT
  1379.             $xfTypeProt $this->_GetInt2d($recordData4);
  1380.             // bit 0; mask 0x01; 1 = cell is locked
  1381.             $isLocked (0x01 $xfTypeProt>> 0;
  1382.             $objStyle->getProtection()->setLocked($isLocked ?
  1383.                 PHPExcel_Style_Protection::PROTECTION_INHERIT PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1384.  
  1385.             // bit 1; mask 0x02; 1 = Formula is hidden
  1386.             $isHidden (0x02 $xfTypeProt>> 1;
  1387.             $objStyle->getProtection()->setHidden($isHidden ?
  1388.                 PHPExcel_Style_Protection::PROTECTION_PROTECTED PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1389.  
  1390.             // bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
  1391.             $isCellStyleXf (0x04 $xfTypeProt>> 2;
  1392.  
  1393.             // offset:  6; size: 1; Alignment and text break
  1394.             // bit 2-0, mask 0x07; horizontal alignment
  1395.             $horAlign (0x07 ord($recordData{6})) >> 0;
  1396.             switch ($horAlign{
  1397.                 case 0:
  1398.                     $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_GENERAL);
  1399.                     break;
  1400.                 case 1:
  1401.                     $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1402.                     break;
  1403.                 case 2:
  1404.                     $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1405.                     break;
  1406.                 case 3:
  1407.                     $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  1408.                     break;
  1409.                 case 5:
  1410.                     $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  1411.                     break;
  1412.                 case 6:
  1413.                     $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
  1414.                     break;
  1415.             }
  1416.             // bit 3, mask 0x08; wrap text
  1417.             $wrapText (0x08 ord($recordData{6})) >> 3;
  1418.             switch ($wrapText{
  1419.                 case 0:
  1420.                     $objStyle->getAlignment()->setWrapText(false);
  1421.                     break;
  1422.                 case 1:
  1423.                     $objStyle->getAlignment()->setWrapText(true);
  1424.                     break;
  1425.             }
  1426.             // bit 6-4, mask 0x70; vertical alignment
  1427.             $vertAlign (0x70 ord($recordData{6})) >> 4;
  1428.             switch ($vertAlign{
  1429.                 case 0:
  1430.                     $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
  1431.                     break;
  1432.                 case 1:
  1433.                     $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1434.                     break;
  1435.                 case 2:
  1436.                     $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
  1437.                     break;
  1438.                 case 3:
  1439.                     $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY);
  1440.                     break;
  1441.             }
  1442.  
  1443.             if ($this->_version == self::XLS_BIFF8{
  1444.                 // offset:  7; size: 1; XF_ROTATION: Text rotation angle
  1445.                     $angle ord($recordData{7});
  1446.                     $rotation 0;
  1447.                     if ($angle <= 90{
  1448.                         $rotation $angle;
  1449.                     else if ($angle <= 180{
  1450.                         $rotation 90 $angle;
  1451.                     else if ($angle == 255{
  1452.                         $rotation = -165;
  1453.                     }
  1454.                     $objStyle->getAlignment()->setTextRotation($rotation);
  1455.  
  1456.                 // offset:  8; size: 1; Indentation, shrink to cell size, and text direction
  1457.                     // bit: 3-0; mask: 0x0F; indent level
  1458.                     $indent (0x0F ord($recordData{8})) >> 0;
  1459.                     $objStyle->getAlignment()->setIndent($indent);
  1460.  
  1461.                     // bit: 4; mask: 0x10; 1 = shrink content to fit into cell
  1462.                     $shrinkToFit (0x10 ord($recordData{8})) >> 4;
  1463.                     switch ($shrinkToFit{
  1464.                         case 0:
  1465.                             $objStyle->getAlignment()->setShrinkToFit(false);
  1466.                             break;
  1467.                         case 1:
  1468.                             $objStyle->getAlignment()->setShrinkToFit(true);
  1469.                             break;
  1470.                     }
  1471.  
  1472.                 // offset:  9; size: 1; Flags used for attribute groups
  1473.  
  1474.                 // offset: 10; size: 4; Cell border lines and background area
  1475.                     // bit: 3-0; mask: 0x0000000F; left style
  1476.                     if ($bordersLeftStyle $this->_mapBorderStyle((0x0000000F $this->_GetInt4d($recordData10)) >> 0)) {
  1477.                         $objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
  1478.                     }
  1479.                     // bit: 7-4; mask: 0x000000F0; right style
  1480.                     if ($bordersRightStyle $this->_mapBorderStyle((0x000000F0 $this->_GetInt4d($recordData10)) >> 4)) {
  1481.                         $objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
  1482.                     }
  1483.                     // bit: 11-8; mask: 0x00000F00; top style
  1484.                     if ($bordersTopStyle $this->_mapBorderStyle((0x00000F00 $this->_GetInt4d($recordData10)) >> 8)) {
  1485.                         $objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
  1486.                     }
  1487.                     // bit: 15-12; mask: 0x0000F000; bottom style
  1488.                     if ($bordersBottomStyle $this->_mapBorderStyle((0x0000F000 $this->_GetInt4d($recordData10)) >> 12)) {
  1489.                         $objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
  1490.                     }
  1491.                     // bit: 22-16; mask: 0x007F0000; left color
  1492.                     $objStyle->getBorders()->getLeft()->colorIndex (0x007F0000 $this->_GetInt4d($recordData10)) >> 16;
  1493.  
  1494.                     // bit: 29-23; mask: 0x3F800000; right color
  1495.                     $objStyle->getBorders()->getRight()->colorIndex (0x3F800000 $this->_GetInt4d($recordData10)) >> 23;
  1496.  
  1497.                     // bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
  1498.                     $diagonalDown (0x40000000 $this->_GetInt4d($recordData10)) >> 30 ?
  1499.                         true false;
  1500.  
  1501.                     // bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
  1502.                     $diagonalUp (0x80000000 $this->_GetInt4d($recordData10)) >> 31 ?
  1503.                         true false;
  1504.  
  1505.                     if ($diagonalUp == false && $diagonalDown == false{
  1506.                         $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_NONE);
  1507.                     elseif ($diagonalUp == true && $diagonalDown == false{
  1508.                         $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_UP);
  1509.                     elseif ($diagonalUp == false && $diagonalDown == true{
  1510.                         $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_DOWN);
  1511.                     elseif ($diagonalUp == true && $diagonalDown == true{
  1512.                         $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_BOTH);
  1513.                     }
  1514.  
  1515.                 // offset: 14; size: 4;
  1516.                     // bit: 6-0; mask: 0x0000007F; top color
  1517.                     $objStyle->getBorders()->getTop()->colorIndex (0x0000007F $this->_GetInt4d($recordData14)) >> 0;
  1518.  
  1519.                     // bit: 13-7; mask: 0x00003F80; bottom color
  1520.                     $objStyle->getBorders()->getBottom()->colorIndex (0x00003F80 $this->_GetInt4d($recordData14)) >> 7;
  1521.  
  1522.                     // bit: 20-14; mask: 0x001FC000; diagonal color
  1523.                     $objStyle->getBorders()->getDiagonal()->colorIndex (0x001FC000 $this->_GetInt4d($recordData14)) >> 14;
  1524.  
  1525.                     // bit: 24-21; mask: 0x01E00000; diagonal style
  1526.                     if ($bordersDiagonalStyle $this->_mapBorderStyle((0x01E00000 $this->_GetInt4d($recordData14)) >> 21)) {
  1527.                         $objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
  1528.                     }
  1529.  
  1530.                     // bit: 31-26; mask: 0xFC000000 fill pattern
  1531.                     if ($fillType $this->_mapFillPattern((0xFC000000 $this->_GetInt4d($recordData14)) >> 26)) {
  1532.                         $objStyle->getFill()->setFillType($fillType);
  1533.                     }
  1534.                 // offset: 18; size: 2; pattern and background colour
  1535.                     // bit: 6-0; mask: 0x007F; color index for pattern color
  1536.                     $objStyle->getFill()->startcolorIndex (0x007F $this->_GetInt2d($recordData18)) >> 0;
  1537.  
  1538.                     // bit: 13-7; mask: 0x3F80; color index for pattern background
  1539.                     $objStyle->getFill()->endcolorIndex (0x3F80 $this->_GetInt2d($recordData18)) >> 7;
  1540.             else {
  1541.                 // BIFF5
  1542.  
  1543.                 // offset: 7; size: 1; Text orientation and flags
  1544.                 $orientationAndFlags ord($recordData{7});
  1545.  
  1546.                 // bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
  1547.                 $xfOrientation (0x03 $orientationAndFlags>> 0;
  1548.                 switch ($xfOrientation{
  1549.                     case 0:
  1550.                         $objStyle->getAlignment()->setTextRotation(0);
  1551.                         break;
  1552.                     case 1:
  1553.                         $objStyle->getAlignment()->setTextRotation(-165);
  1554.                         break;
  1555.                     case 2:
  1556.                         $objStyle->getAlignment()->setTextRotation(90);
  1557.                         break;
  1558.                     case 3:
  1559.                         $objStyle->getAlignment()->setTextRotation(-90);
  1560.                         break;
  1561.                 }
  1562.  
  1563.                 // offset: 8; size: 4; cell border lines and background area
  1564.                 $borderAndBackground $this->_GetInt4d($recordData8);
  1565.  
  1566.                 // bit: 6-0; mask: 0x0000007F; color index for pattern color
  1567.                 $objStyle->getFill()->startcolorIndex (0x0000007F $borderAndBackground>> 0;
  1568.  
  1569.                 // bit: 13-7; mask: 0x00003F80; color index for pattern background
  1570.                 $objStyle->getFill()->endcolorIndex (0x00003F80 $borderAndBackground>> 7;
  1571.  
  1572.                 // bit: 21-16; mask: 0x003F0000; fill pattern
  1573.                 $objStyle->getFill()->setFillType($this->_mapFillPattern((0x003F0000 $borderAndBackground>> 16));
  1574.  
  1575.                 // bit: 24-22; mask: 0x01C00000; bottom line style
  1576.                 $objStyle->getBorders()->getBottom()->setBorderStyle($this->_mapBorderStyle((0x01C00000 $borderAndBackground>> 22));
  1577.  
  1578.                 // bit: 31-25; mask: 0xFE000000; bottom line color
  1579.                 $objStyle->getBorders()->getBottom()->colorIndex (0xFE000000 $borderAndBackground>> 25;
  1580.  
  1581.                 // offset: 12; size: 4; cell border lines
  1582.                 $borderLines $this->_GetInt4d($recordData12);
  1583.  
  1584.                 // bit: 2-0; mask: 0x00000007; top line style
  1585.                 $objStyle->getBorders()->getTop()->setBorderStyle($this->_mapBorderStyle((0x00000007 $borderLines>> 0));
  1586.  
  1587.                 // bit: 5-3; mask: 0x00000038; left line style
  1588.                 $objStyle->getBorders()->getLeft()->setBorderStyle($this->_mapBorderStyle((0x00000038 $borderLines>> 3));
  1589.  
  1590.                 // bit: 8-6; mask: 0x000001C0; right line style
  1591.                 $objStyle->getBorders()->getRight()->setBorderStyle($this->_mapBorderStyle((0x000001C0 $borderLines>> 6));
  1592.  
  1593.                 // bit: 15-9; mask: 0x0000FE00; top line color index
  1594.                 $objStyle->getBorders()->getTop()->colorIndex (0x0000FE00 $borderLines>> 9;
  1595.  
  1596.                 // bit: 22-16; mask: 0x007F0000; left line color index
  1597.                 $objStyle->getBorders()->getLeft()->colorIndex (0x007F0000 $borderLines>> 16;
  1598.  
  1599.                 // bit: 29-23; mask: 0x3F800000; right line color index
  1600.                 $objStyle->getBorders()->getRight()->colorIndex (0x3F800000 $borderLines>> 23;
  1601.             }
  1602.  
  1603.             // add cellStyleXf or cellXf and update mapping
  1604.             if ($isCellStyleXf{
  1605.                 // we only read one style XF record which is always the first
  1606.                 if ($this->_xfIndex == 0{
  1607.                     $this->_phpExcel->addCellStyleXf($objStyle);
  1608.                     $this->_mapCellStyleXfIndex[$this->_xfIndex0;
  1609.                 }
  1610.             else {
  1611.                 // we read all cell XF records
  1612.                 $this->_phpExcel->addCellXf($objStyle);
  1613.                 $this->_mapCellXfIndex[$this->_xfIndexcount($this->_phpExcel->getCellXfCollection()) 1;
  1614.             }
  1615.  
  1616.             // update XF index for when we read next record
  1617.             ++$this->_xfIndex;
  1618.         }
  1619.     }
  1620.  
  1621.     /**
  1622.      *
  1623.      */
  1624.     private function _readXfExt()
  1625.     {
  1626.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1627.         $recordData substr($this->_data$this->_pos + 4$length);
  1628.  
  1629.         // move stream pointer to next record
  1630.         $this->_pos += $length;
  1631.  
  1632.         if (!$this->_readDataOnly{
  1633.             // offset: 0; size: 2; 0x087D = repeated header
  1634.  
  1635.             // offset: 2; size: 2
  1636.             
  1637.             // offset: 4; size: 8; not used
  1638.  
  1639.             // offset: 12; size: 2; record version
  1640.  
  1641.             // offset: 14; size: 2; index to XF record which this record modifies
  1642.             $ixfe $this->_GetInt2d($recordData14);
  1643.  
  1644.             // offset: 16; size: 2; not used
  1645.  
  1646.             // offset: 18; size: 2; number of extension properties that follow
  1647.             $cexts $this->_GetInt2d($recordData18);
  1648.             
  1649.             // start reading the actual extension data
  1650.             $offset 20;
  1651.             while ($offset $length{
  1652.                 // extension type
  1653.                 $extType $this->_GetInt2d($recordData$offset);
  1654.                 
  1655.                 // extension length
  1656.                 $cb $this->_GetInt2d($recordData$offset 2);
  1657.                 
  1658.                 // extension data
  1659.                 $extData substr($recordData$offset 4$cb);
  1660.                 
  1661.                 switch ($extType{
  1662.                     case 4:        // fill start color
  1663.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1664.                         $xclrValue substr($extData44)// color value (value based on color type)
  1665.  
  1666.                         if ($xclfType == 2{
  1667.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1668.  
  1669.                             // modify the relevant style property
  1670.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1671.                                 $fill $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  1672.                                 $fill->getStartColor()->setRGB($rgb);
  1673.                                 unset($fill->startcolorIndex)// normal color index does not apply, discard
  1674.                             }
  1675.                         }
  1676.                         break;
  1677.  
  1678.                     case 5:        // fill end color
  1679.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1680.                         $xclrValue substr($extData44)// color value (value based on color type)
  1681.  
  1682.                         if ($xclfType == 2{
  1683.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1684.  
  1685.                             // modify the relevant style property
  1686.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1687.                                 $fill $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  1688.                                 $fill->getEndColor()->setRGB($rgb);
  1689.                                 unset($fill->endcolorIndex)// normal color index does not apply, discard
  1690.                             }
  1691.                         }
  1692.                         break;
  1693.  
  1694.                     case 7:        // border color top
  1695.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1696.                         $xclrValue substr($extData44)// color value (value based on color type)
  1697.  
  1698.                         if ($xclfType == 2{
  1699.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1700.  
  1701.                             // modify the relevant style property
  1702.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1703.                                 $top $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getTop();
  1704.                                 $top->getColor()->setRGB($rgb);
  1705.                                 unset($top->colorIndex)// normal color index does not apply, discard
  1706.                             }
  1707.                         }
  1708.                         break;
  1709.  
  1710.                     case 8:        // border color bottom
  1711.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1712.                         $xclrValue substr($extData44)// color value (value based on color type)
  1713.  
  1714.                         if ($xclfType == 2{
  1715.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1716.  
  1717.                             // modify the relevant style property
  1718.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1719.                                 $bottom $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getBottom();
  1720.                                 $bottom->getColor()->setRGB($rgb);
  1721.                                 unset($bottom->colorIndex)// normal color index does not apply, discard
  1722.                             }
  1723.                         }
  1724.                         break;
  1725.  
  1726.                     case 9:        // border color left
  1727.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1728.                         $xclrValue substr($extData44)// color value (value based on color type)
  1729.  
  1730.                         if ($xclfType == 2{
  1731.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1732.  
  1733.                             // modify the relevant style property
  1734.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1735.                                 $left $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getLeft();
  1736.                                 $left->getColor()->setRGB($rgb);
  1737.                                 unset($left->colorIndex)// normal color index does not apply, discard
  1738.                             }
  1739.                         }
  1740.                         break;
  1741.  
  1742.                     case 10:        // border color right
  1743.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1744.                         $xclrValue substr($extData44)// color value (value based on color type)
  1745.  
  1746.                         if ($xclfType == 2{
  1747.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1748.  
  1749.                             // modify the relevant style property
  1750.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1751.                                 $right $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getRight();
  1752.                                 $right->getColor()->setRGB($rgb);
  1753.                                 unset($right->colorIndex)// normal color index does not apply, discard
  1754.                             }
  1755.                         }
  1756.                         break;
  1757.  
  1758.                     case 11:        // border color diagonal
  1759.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1760.                         $xclrValue substr($extData44)// color value (value based on color type)
  1761.  
  1762.                         if ($xclfType == 2{
  1763.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1764.  
  1765.                             // modify the relevant style property
  1766.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1767.                                 $diagonal $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getDiagonal();
  1768.                                 $diagonal->getColor()->setRGB($rgb);
  1769.                                 unset($diagonal->colorIndex)// normal color index does not apply, discard
  1770.                             }
  1771.                         }
  1772.                         break;
  1773.  
  1774.                     case 13:    // font color
  1775.                         $xclfType  $this->_GetInt2d($extData0)// color type
  1776.                         $xclrValue substr($extData44)// color value (value based on color type)
  1777.  
  1778.                         if ($xclfType == 2{
  1779.                             $rgb sprintf('%02X%02X%02X'ord($xclrValue{0})ord($xclrValue{1})ord($xclrValue{2}));
  1780.  
  1781.                             // modify the relevant style property
  1782.                             if isset($this->_mapCellXfIndex[$ixfe]) ) {
  1783.                                 $font $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFont();
  1784.                                 $font->getColor()->setRGB($rgb);
  1785.                                 unset($font->colorIndex)// normal color index does not apply, discard
  1786.                             }
  1787.                         }
  1788.                         break;
  1789.                 }
  1790.  
  1791.                 $offset += $cb;
  1792.             }
  1793.         }
  1794.  
  1795.     }
  1796.     
  1797.     /**
  1798.      * Read STYLE record
  1799.      */
  1800.     private function _readStyle()
  1801.     {
  1802.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1803.         $recordData substr($this->_data$this->_pos + 4$length);
  1804.  
  1805.         // move stream pointer to next record
  1806.         $this->_pos += $length;
  1807.  
  1808.         if (!$this->_readDataOnly{
  1809.             // offset: 0; size: 2; index to XF record and flag for built-in style
  1810.             $ixfe $this->_GetInt2d($recordData0);
  1811.  
  1812.             // bit: 11-0; mask 0x0FFF; index to XF record
  1813.             $xfIndex (0x0FFF $ixfe>> 0;
  1814.  
  1815.             // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
  1816.             $isBuiltIn = (bool) ((0x8000 $ixfe>> 15);
  1817.  
  1818.             if ($isBuiltIn{
  1819.                 // offset: 2; size: 1; identifier for built-in style
  1820.                 $builtInId ord($recordData{2});
  1821.  
  1822.                 switch ($builtInId{
  1823.                 case 0x00:
  1824.                     // currently, we are not using this for anything
  1825.                     break;
  1826.  
  1827.                 default:
  1828.                     break;
  1829.                 }
  1830.  
  1831.             else {
  1832.                 // user-defined; not supported by PHPExcel
  1833.             }
  1834.         }
  1835.     }
  1836.  
  1837.     /**
  1838.      * Read PALETTE record
  1839.      */
  1840.     private function _readPalette()
  1841.     {
  1842.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1843.         $recordData substr($this->_data$this->_pos + 4$length);
  1844.  
  1845.         // move stream pointer to next record
  1846.         $this->_pos += $length;
  1847.  
  1848.         if (!$this->_readDataOnly{
  1849.             // offset: 0; size: 2; number of following colors
  1850.             $nm $this->_GetInt2d($recordData0);
  1851.  
  1852.             // list of RGB colors
  1853.             for ($i 0$i $nm++$i{
  1854.                 $rgb substr($recordData$i4);
  1855.                 $this->_palette[$this->_readRGB($rgb);
  1856.             }
  1857.         }
  1858.     }
  1859.  
  1860.     /**
  1861.      * SHEET
  1862.      *
  1863.      * This record is  located in the  Workbook Globals
  1864.      * Substream  and represents a sheet inside the workbook.
  1865.      * One SHEET record is written for each sheet. It stores the
  1866.      * sheet name and a stream offset to the BOF record of the
  1867.      * respective Sheet Substream within the Workbook Stream.
  1868.      *
  1869.      * --    "OpenOffice.org's Documentation of the Microsoft
  1870.      *         Excel File Format"
  1871.      */
  1872.     private function _readSheet()
  1873.     {
  1874.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1875.         $recordData substr($this->_data$this->_pos + 4$length);
  1876.  
  1877.         // move stream pointer to next record
  1878.         $this->_pos += $length;
  1879.  
  1880.         // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
  1881.         $rec_offset $this->_GetInt4d($recordData0);
  1882.  
  1883.         // offset: 4; size: 1; sheet state
  1884.         $rec_typeFlag ord($recordData{4});
  1885.         switch (ord($recordData{4})) {
  1886.             case 0x00$sheetState PHPExcel_Worksheet::SHEETSTATE_VISIBLE;    break;
  1887.             case 0x01$sheetState PHPExcel_Worksheet::SHEETSTATE_HIDDEN;     break;
  1888.             case 0x02$sheetState PHPExcel_Worksheet::SHEETSTATE_VERYHIDDENbreak;
  1889.             default$sheetState PHPExcel_Worksheet::SHEETSTATE_VISIBLE;      break;
  1890.         }
  1891.  
  1892.         // offset: 5; size: 1; sheet type
  1893.         $rec_visibilityFlag ord($recordData{5});
  1894.  
  1895.         // offset: 6; size: var; sheet name
  1896.         if ($this->_version == self::XLS_BIFF8{
  1897.             $string $this->_readUnicodeStringShort(substr($recordData6));
  1898.             $rec_name $string['value'];
  1899.         elseif ($this->_version == self::XLS_BIFF7{
  1900.             $string $this->_readByteStringShort(substr($recordData6));
  1901.             $rec_name $string['value'];
  1902.         }
  1903.         $this->_sheets[array(
  1904.             'name' => $rec_name,
  1905.             'offset' => $rec_offset,
  1906.             'sheetState' => $sheetState,
  1907.         );
  1908.     }
  1909.  
  1910.     /**
  1911.      * Read EXTERNALBOOK record
  1912.      */
  1913.     private function _readExternalBook()
  1914.     {
  1915.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1916.         $recordData substr($this->_data$this->_pos + 4$length);
  1917.  
  1918.         // move stream pointer to next record
  1919.         $this->_pos += $length;
  1920.  
  1921.         // offset within record data
  1922.         $offset 0;
  1923.  
  1924.         // there are 4 types of records
  1925.         if (strlen($recordData4{
  1926.             // external reference
  1927.             // offset: 0; size: 2; number of sheet names ($nm)
  1928.             $nm $this->_GetInt2d($recordData0);
  1929.             $offset += 2;
  1930.  
  1931.             // offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
  1932.             $encodedUrlString $this->_readUnicodeStringLong(substr($recordData2));
  1933.             $offset += $encodedUrlString['size'];
  1934.  
  1935.             // offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
  1936.             $externalSheetNames array();
  1937.             for ($i 0$i $nm++$i{
  1938.                 $externalSheetNameString $this->_readUnicodeStringLong(substr($recordData$offset));
  1939.                 $externalSheetNames[$externalSheetNameString['value'];
  1940.                 $offset += $externalSheetNameString['size'];
  1941.             }
  1942.  
  1943.             // store the record data
  1944.             $this->_externalBooks[array(
  1945.                 'type' => 'external',
  1946.                 'encodedUrl' => $encodedUrlString['value'],
  1947.                 'externalSheetNames' => $externalSheetNames,
  1948.             );
  1949.  
  1950.         elseif (substr($recordData22== pack('CC'0x010x04)) {
  1951.             // internal reference
  1952.             // offset: 0; size: 2; number of sheet in this document
  1953.             // offset: 2; size: 2; 0x01 0x04
  1954.             $this->_externalBooks[array(
  1955.                 'type' => 'internal',
  1956.             );
  1957.         elseif (substr($recordData04== pack('VCC'0x00010x010x3A)) {
  1958.             // add-in function
  1959.             // offset: 0; size: 2; 0x0001
  1960.             $this->_externalBooks[array(
  1961.                 'type' => 'addInFunction',
  1962.             );
  1963.         elseif (substr($recordData02== pack('V'0x0000)) {
  1964.             // DDE links, OLE links
  1965.             // offset: 0; size: 2; 0x0000
  1966.             // offset: 2; size: var; encoded source document name
  1967.             $this->_externalBooks[array(
  1968.                 'type' => 'DDEorOLE',
  1969.             );
  1970.         }
  1971.     }
  1972.  
  1973.     /**
  1974.      * Read EXTERNSHEET record
  1975.      */
  1976.     private function _readExternSheet()
  1977.     {
  1978.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  1979.         $recordData substr($this->_data$this->_pos + 4$length);
  1980.  
  1981.         // move stream pointer to next record
  1982.         $this->_pos += $length;
  1983.  
  1984.         // external sheet references provided for named cells
  1985.         if ($this->_version == self::XLS_BIFF8{
  1986.             // offset: 0; size: 2; number of following ref structures
  1987.             $nm $this->_GetInt2d($recordData0);
  1988.             for ($i 0$i $nm++$i{
  1989.                 $this->_ref[array(
  1990.                     // offset: 2 + 6 * $i; index to EXTERNALBOOK record
  1991.                     'externalBookIndex' => $this->_GetInt2d($recordData$i),
  1992.                     // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
  1993.                     'firstSheetIndex' => $this->_GetInt2d($recordData$i),
  1994.                     // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
  1995.                     'lastSheetIndex' => $this->_GetInt2d($recordData$i),
  1996.                 );
  1997.             }
  1998.         }
  1999.     }
  2000.  
  2001.     /**
  2002.      * DEFINEDNAME
  2003.      *
  2004.      * This record is part of a Link Table. It contains the name
  2005.      * and the token array of an internal defined name. Token
  2006.      * arrays of defined names contain tokens with aberrant
  2007.      * token classes.
  2008.      *
  2009.      * --    "OpenOffice.org's Documentation of the Microsoft
  2010.      *         Excel File Format"
  2011.      */
  2012.     private function _readDefinedName()
  2013.     {
  2014.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2015.         $recordData substr($this->_data$this->_pos + 4$length);
  2016.  
  2017.         // move stream pointer to next record
  2018.         $this->_pos += $length;
  2019.  
  2020.         if ($this->_version == self::XLS_BIFF8{
  2021.             // retrieves named cells
  2022.  
  2023.             // offset: 0; size: 2; option flags
  2024.             $opts $this->_GetInt2d($recordData0);
  2025.  
  2026.                 // bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
  2027.                 $isBuiltInName (0x0020 $opts>> 5;
  2028.  
  2029.             // offset: 2; size: 1; keyboard shortcut
  2030.  
  2031.             // offset: 3; size: 1; length of the name (character count)
  2032.             $nlen ord($recordData{3});
  2033.  
  2034.             // offset: 4; size: 2; size of the formula data (it can happen that this is zero)
  2035.             // note: there can also be additional data, this is not included in $flen
  2036.             $flen $this->_GetInt2d($recordData4);
  2037.  
  2038.             // offset: 14; size: var; Name (Unicode string without length field)
  2039.             $string $this->_readUnicodeString(substr($recordData14)$nlen);
  2040.  
  2041.             // offset: var; size: $flen; formula data
  2042.             $offset 14 $string['size'];
  2043.             $formulaStructure pack('v'$flensubstr($recordData$offset);
  2044.  
  2045.             try {
  2046.                 $formula $this->_getFormulaFromStructure($formulaStructure);
  2047.             catch (Exception $e{
  2048.                 $formula '';
  2049.             }
  2050.  
  2051.             $this->_definedname[array(
  2052.                 'isBuiltInName' => $isBuiltInName,
  2053.                 'name' => $string['value'],
  2054.                 'formula' => $formula,
  2055.             );
  2056.         }
  2057.     }
  2058.  
  2059.     /**
  2060.      * Read MSODRAWINGGROUP record
  2061.      */
  2062.     private function _readMsoDrawingGroup()
  2063.     {
  2064.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2065.  
  2066.         // get spliced record data
  2067.         $splicedRecordData $this->_getSplicedRecordData();
  2068.         $recordData $splicedRecordData['recordData'];
  2069.  
  2070.         $this->_drawingGroupData .= $recordData;
  2071.     }
  2072.  
  2073.     /**
  2074.      * SST - Shared String Table
  2075.      *
  2076.      * This record contains a list of all strings used anywhere
  2077.      * in the workbook. Each string occurs only once. The
  2078.      * workbook uses indexes into the list to reference the
  2079.      * strings.
  2080.      *
  2081.      * --    "OpenOffice.org's Documentation of the Microsoft
  2082.      *         Excel File Format"
  2083.      **/
  2084.  
  2085.     private function _readSst()
  2086.     {
  2087.         // offset within (spliced) record data
  2088.         $pos 0;
  2089.  
  2090.         // get spliced record data
  2091.         $splicedRecordData $this->_getSplicedRecordData();
  2092.  
  2093.         $recordData $splicedRecordData['recordData'];
  2094.         $spliceOffsets $splicedRecordData['spliceOffsets'];
  2095.  
  2096.         // offset: 0; size: 4; total number of strings in the workbook
  2097.         $pos += 4;
  2098.  
  2099.         // offset: 4; size: 4; number of following strings ($nm)
  2100.         $nm $this->_GetInt4d($recordData4);
  2101.         $pos += 4;
  2102.  
  2103.         // loop through the Unicode strings (16-bit length)
  2104.         for ($i 0$i $nm++$i{
  2105.  
  2106.             // number of characters in the Unicode string
  2107.             $numChars $this->_GetInt2d($recordData$pos);
  2108.             $pos += 2;
  2109.  
  2110.             // option flags
  2111.             $optionFlags ord($recordData{$pos});
  2112.             ++$pos;
  2113.  
  2114.             // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  2115.             $isCompressed (($optionFlags 0x01== 0;
  2116.  
  2117.             // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  2118.             $hasAsian (($optionFlags 0x04!= 0);
  2119.  
  2120.             // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  2121.             $hasRichText (($optionFlags 0x08!= 0);
  2122.  
  2123.             if ($hasRichText{
  2124.                 // number of Rich-Text formatting runs
  2125.                 $formattingRuns $this->_GetInt2d($recordData$pos);
  2126.                 $pos += 2;
  2127.             }
  2128.  
  2129.             if ($hasAsian{
  2130.                 // size of Asian phonetic setting
  2131.                 $extendedRunLength $this->_GetInt4d($recordData$pos);
  2132.                 $pos += 4;
  2133.             }
  2134.  
  2135.             // expected byte length of character array if not split
  2136.             $len ($isCompressed$numChars $numChars 2;
  2137.  
  2138.             // look up limit position
  2139.             foreach ($spliceOffsets as $spliceOffset{
  2140.                 // it can happen that the string is empty, therefore we need
  2141.                 // <= and not just <
  2142.                 if ($pos <= $spliceOffset{
  2143.                     $limitpos $spliceOffset;
  2144.                     break;
  2145.                 }
  2146.             }
  2147.  
  2148.             if ($pos $len <= $limitpos{
  2149.                 // character array is not split between records
  2150.  
  2151.                 $retstr substr($recordData$pos$len);
  2152.                 $pos += $len;
  2153.  
  2154.             else {
  2155.                 // character array is split between records
  2156.  
  2157.                 // first part of character array
  2158.                 $retstr substr($recordData$pos$limitpos $pos);
  2159.  
  2160.                 $bytesRead $limitpos $pos;
  2161.  
  2162.                 // remaining characters in Unicode string
  2163.                 $charsLeft $numChars (($isCompressed$bytesRead ($bytesRead 2));
  2164.  
  2165.                 $pos $limitpos;
  2166.  
  2167.                 // keep reading the characters
  2168.                 while ($charsLeft 0{
  2169.  
  2170.                     // look up next limit position, in case the string span more than one continue record
  2171.                     foreach ($spliceOffsets as $spliceOffset{
  2172.                         if ($pos $spliceOffset{
  2173.                             $limitpos $spliceOffset;
  2174.                             break;
  2175.                         }
  2176.                     }
  2177.  
  2178.                     // repeated option flags
  2179.                     // OpenOffice.org documentation 5.21
  2180.                     $option ord($recordData{$pos});
  2181.                     ++$pos;
  2182.  
  2183.                     if ($isCompressed && ($option == 0)) {
  2184.                         // 1st fragment compressed
  2185.                         // this fragment compressed
  2186.                         $len min($charsLeft$limitpos $pos);
  2187.                         $retstr .= substr($recordData$pos$len);
  2188.                         $charsLeft -= $len;
  2189.                         $isCompressed true;
  2190.  
  2191.                     elseif (!$isCompressed && ($option != 0)) {
  2192.                         // 1st fragment uncompressed
  2193.                         // this fragment uncompressed
  2194.                         $len min($charsLeft 2$limitpos $pos);
  2195.                         $retstr .= substr($recordData$pos$len);
  2196.                         $charsLeft -= $len 2;
  2197.                         $isCompressed false;
  2198.  
  2199.                     elseif (!$isCompressed && ($option == 0)) {
  2200.                         // 1st fragment uncompressed
  2201.                         // this fragment compressed
  2202.                         $len min($charsLeft$limitpos $pos);
  2203.                         for ($j 0$j $len++$j{
  2204.                             $retstr .= $recordData{$pos $jchr(0);
  2205.                         }
  2206.                         $charsLeft -= $len;
  2207.                         $isCompressed false;
  2208.  
  2209.                     else {
  2210.                         // 1st fragment compressed
  2211.                         // this fragment uncompressed
  2212.                         $newstr '';
  2213.                         for ($j 0$j strlen($retstr)++$j{
  2214.                             $newstr .= $retstr[$jchr(0);
  2215.                         }
  2216.                         $retstr $newstr;
  2217.                         $len min($charsLeft 2$limitpos $pos);
  2218.                         $retstr .= substr($recordData$pos$len);
  2219.                         $charsLeft -= $len 2;
  2220.                         $isCompressed false;
  2221.                     }
  2222.  
  2223.                     $pos += $len;
  2224.                 }
  2225.             }
  2226.  
  2227.             // convert to UTF-8
  2228.             $retstr $this->_encodeUTF16($retstr$isCompressed);
  2229.  
  2230.             // read additional Rich-Text information, if any
  2231.             $fmtRuns array();
  2232.             if ($hasRichText{
  2233.                 // list of formatting runs
  2234.                 for ($j 0$j $formattingRuns++$j{
  2235.                     // first formatted character; zero-based
  2236.                     $charPos $this->_GetInt2d($recordData$pos $j 4);
  2237.  
  2238.                     // index to font record
  2239.                     $fontIndex $this->_GetInt2d($recordData$pos $j 4);
  2240.  
  2241.                     $fmtRuns[array(
  2242.                         'charPos' => $charPos,
  2243.                         'fontIndex' => $fontIndex,
  2244.                     );
  2245.                 }
  2246.                 $pos += $formattingRuns;
  2247.             }
  2248.  
  2249.             // read additional Asian phonetics information, if any
  2250.             if ($hasAsian{
  2251.                 // For Asian phonetic settings, we skip the extended string data
  2252.                 $pos += $extendedRunLength;
  2253.             }
  2254.  
  2255.             // store the shared sting
  2256.             $this->_sst[array(
  2257.                 'value' => $retstr,
  2258.                 'fmtRuns' => $fmtRuns,
  2259.             );
  2260.         }
  2261.  
  2262.         // _getSplicedRecordData() takes care of moving current position in data stream
  2263.     }
  2264.  
  2265.     /**
  2266.      * Read PRINTGRIDLINES record
  2267.      */
  2268.     private function _readPrintGridlines()
  2269.     {
  2270.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2271.         $recordData substr($this->_data$this->_pos + 4$length);
  2272.  
  2273.         // move stream pointer to next record
  2274.         $this->_pos += $length;
  2275.  
  2276.         if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly{
  2277.             // offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
  2278.             $printGridlines = (bool) $this->_GetInt2d($recordData0);
  2279.             $this->_phpSheet->setPrintGridlines($printGridlines);
  2280.         }
  2281.     }
  2282.  
  2283.     /**
  2284.      * Read DEFAULTROWHEIGHT record
  2285.      */
  2286.     private function _readDefaultRowHeight()
  2287.     {
  2288.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2289.         $recordData substr($this->_data$this->_pos + 4$length);
  2290.  
  2291.         // move stream pointer to next record
  2292.         $this->_pos += $length;
  2293.  
  2294.         // offset: 0; size: 2; option flags
  2295.         // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
  2296.         $height $this->_GetInt2d($recordData2);
  2297.         $this->_phpSheet->getDefaultRowDimension()->setRowHeight($height 20);
  2298.     }
  2299.  
  2300.     /**
  2301.      * Read SHEETPR record
  2302.      */
  2303.     private function _readSheetPr()
  2304.     {
  2305.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2306.         $recordData substr($this->_data$this->_pos + 4$length);
  2307.  
  2308.         // move stream pointer to next record
  2309.         $this->_pos += $length;
  2310.  
  2311.         // offset: 0; size: 2
  2312.  
  2313.         // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
  2314.         $isSummaryBelow (0x0040 $this->_GetInt2d($recordData0)) >> 6;
  2315.         $this->_phpSheet->setShowSummaryBelow($isSummaryBelow);
  2316.  
  2317.         // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
  2318.         $isSummaryRight (0x0080 $this->_GetInt2d($recordData0)) >> 7;
  2319.         $this->_phpSheet->setShowSummaryRight($isSummaryRight);
  2320.  
  2321.         // bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
  2322.         // this corresponds to radio button setting in page setup dialog in Excel
  2323.         $this->_isFitToPages = (bool) ((0x0100 $this->_GetInt2d($recordData0)) >> 8);
  2324.     }
  2325.  
  2326.     /**
  2327.      * Read HORIZONTALPAGEBREAKS record
  2328.      */
  2329.     private function _readHorizontalPageBreaks()
  2330.     {
  2331.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2332.         $recordData substr($this->_data$this->_pos + 4$length);
  2333.  
  2334.         // move stream pointer to next record
  2335.         $this->_pos += $length;
  2336.  
  2337.         if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly{
  2338.  
  2339.             // offset: 0; size: 2; number of the following row index structures
  2340.             $nm $this->_GetInt2d($recordData0);
  2341.  
  2342.             // offset: 2; size: 6 * $nm; list of $nm row index structures
  2343.             for ($i 0$i $nm++$i{
  2344.                 $r $this->_GetInt2d($recordData$i);
  2345.                 $cf $this->_GetInt2d($recordData$i 2);
  2346.                 $cl $this->_GetInt2d($recordData$i 4);
  2347.  
  2348.                 // not sure why two column indexes are necessary?
  2349.                 $this->_phpSheet->setBreakByColumnAndRow($cf$rPHPExcel_Worksheet::BREAK_ROW);
  2350.             }
  2351.         }
  2352.     }
  2353.  
  2354.     /**
  2355.      * Read VERTICALPAGEBREAKS record
  2356.      */
  2357.     private function _readVerticalPageBreaks()
  2358.     {
  2359.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2360.         $recordData substr($this->_data$this->_pos + 4$length);
  2361.  
  2362.         // move stream pointer to next record
  2363.         $this->_pos += $length;
  2364.  
  2365.         if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly{
  2366.             // offset: 0; size: 2; number of the following column index structures
  2367.             $nm $this->_GetInt2d($recordData0);
  2368.  
  2369.             // offset: 2; size: 6 * $nm; list of $nm row index structures
  2370.             for ($i 0$i $nm++$i{
  2371.                 $c $this->_GetInt2d($recordData$i);
  2372.                 $rf $this->_GetInt2d($recordData$i 2);
  2373.                 $rl $this->_GetInt2d($recordData$i 4);
  2374.  
  2375.                 // not sure why two row indexes are necessary?
  2376.                 $this->_phpSheet->setBreakByColumnAndRow($c$rfPHPExcel_Worksheet::BREAK_COLUMN);
  2377.             }
  2378.         }
  2379.     }
  2380.  
  2381.     /**
  2382.      * Read HEADER record
  2383.      */
  2384.     private function _readHeader()
  2385.     {
  2386.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2387.         $recordData substr($this->_data$this->_pos + 4$length);
  2388.  
  2389.         // move stream pointer to next record
  2390.         $this->_pos += $length;
  2391.  
  2392.         if (!$this->_readDataOnly{
  2393.             // offset: 0; size: var
  2394.             // realized that $recordData can be empty even when record exists
  2395.             if ($recordData{
  2396.                 if ($this->_version == self::XLS_BIFF8{
  2397.                     $string $this->_readUnicodeStringLong($recordData);
  2398.                 else {
  2399.                     $string $this->_readByteStringShort($recordData);
  2400.                 }
  2401.  
  2402.                 $this->_phpSheet->getHeaderFooter()->setOddHeader($string['value']);
  2403.                 $this->_phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
  2404.             }
  2405.         }
  2406.     }
  2407.  
  2408.     /**
  2409.      * Read FOOTER record
  2410.      */
  2411.     private function _readFooter()
  2412.     {
  2413.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2414.         $recordData substr($this->_data$this->_pos + 4$length);
  2415.  
  2416.         // move stream pointer to next record
  2417.         $this->_pos += $length;
  2418.  
  2419.         if (!$this->_readDataOnly{
  2420.             // offset: 0; size: var
  2421.             // realized that $recordData can be empty even when record exists
  2422.             if ($recordData{
  2423.                 if ($this->_version == self::XLS_BIFF8{
  2424.                     $string $this->_readUnicodeStringLong($recordData);
  2425.                 else {
  2426.                     $string $this->_readByteStringShort($recordData);
  2427.                 }
  2428.                 $this->_phpSheet->getHeaderFooter()->setOddFooter($string['value']);
  2429.                 $this->_phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
  2430.             }
  2431.         }
  2432.     }
  2433.  
  2434.     /**
  2435.      * Read HCENTER record
  2436.      */
  2437.     private function _readHcenter()
  2438.     {
  2439.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2440.         $recordData substr($this->_data$this->_pos + 4$length);
  2441.  
  2442.         // move stream pointer to next record
  2443.         $this->_pos += $length;
  2444.  
  2445.         if (!$this->_readDataOnly{
  2446.             // offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
  2447.             $isHorizontalCentered = (bool) $this->_GetInt2d($recordData0);
  2448.  
  2449.             $this->_phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
  2450.         }
  2451.     }
  2452.  
  2453.     /**
  2454.      * Read VCENTER record
  2455.      */
  2456.     private function _readVcenter()
  2457.     {
  2458.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2459.         $recordData substr($this->_data$this->_pos + 4$length);
  2460.  
  2461.         // move stream pointer to next record
  2462.         $this->_pos += $length;
  2463.  
  2464.         if (!$this->_readDataOnly{
  2465.             // offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
  2466.             $isVerticalCentered = (bool) $this->_GetInt2d($recordData0);
  2467.  
  2468.             $this->_phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
  2469.         }
  2470.     }
  2471.  
  2472.     /**
  2473.      * Read LEFTMARGIN record
  2474.      */
  2475.     private function _readLeftMargin()
  2476.     {
  2477.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2478.         $recordData substr($this->_data$this->_pos + 4$length);
  2479.  
  2480.         // move stream pointer to next record
  2481.         $this->_pos += $length;
  2482.  
  2483.         if (!$this->_readDataOnly{
  2484.             // offset: 0; size: 8
  2485.             $this->_phpSheet->getPageMargins()->setLeft($this->_extractNumber($recordData));
  2486.         }
  2487.     }
  2488.  
  2489.     /**
  2490.      * Read RIGHTMARGIN record
  2491.      */
  2492.     private function _readRightMargin()
  2493.     {
  2494.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2495.         $recordData substr($this->_data$this->_pos + 4$length);
  2496.  
  2497.         // move stream pointer to next record
  2498.         $this->_pos += $length;
  2499.  
  2500.         if (!$this->_readDataOnly{
  2501.             // offset: 0; size: 8
  2502.             $this->_phpSheet->getPageMargins()->setRight($this->_extractNumber($recordData));
  2503.         }
  2504.     }
  2505.  
  2506.     /**
  2507.      * Read TOPMARGIN record
  2508.      */
  2509.     private function _readTopMargin()
  2510.     {
  2511.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2512.         $recordData substr($this->_data$this->_pos + 4$length);
  2513.  
  2514.         // move stream pointer to next record
  2515.         $this->_pos += $length;
  2516.  
  2517.         if (!$this->_readDataOnly{
  2518.             // offset: 0; size: 8
  2519.             $this->_phpSheet->getPageMargins()->setTop($this->_extractNumber($recordData));
  2520.         }
  2521.     }
  2522.  
  2523.     /**
  2524.      * Read BOTTOMMARGIN record
  2525.      */
  2526.     private function _readBottomMargin()
  2527.     {
  2528.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2529.         $recordData substr($this->_data$this->_pos + 4$length);
  2530.  
  2531.         // move stream pointer to next record
  2532.         $this->_pos += $length;
  2533.  
  2534.         if (!$this->_readDataOnly{
  2535.             // offset: 0; size: 8
  2536.             $this->_phpSheet->getPageMargins()->setBottom($this->_extractNumber($recordData));
  2537.         }
  2538.     }
  2539.  
  2540.     /**
  2541.      * Read PAGESETUP record
  2542.      */
  2543.     private function _readPageSetup()
  2544.     {
  2545.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2546.         $recordData substr($this->_data$this->_pos + 4$length);
  2547.  
  2548.         // move stream pointer to next record
  2549.         $this->_pos += $length;
  2550.  
  2551.         if (!$this->_readDataOnly{
  2552.             // offset: 0; size: 2; paper size
  2553.             $paperSize $this->_GetInt2d($recordData0);
  2554.  
  2555.             // offset: 2; size: 2; scaling factor
  2556.             $scale $this->_GetInt2d($recordData2);
  2557.  
  2558.             // offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
  2559.             $fitToWidth $this->_GetInt2d($recordData6);
  2560.  
  2561.             // offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
  2562.             $fitToHeight $this->_GetInt2d($recordData8);
  2563.  
  2564.             // offset: 10; size: 2; option flags
  2565.  
  2566.                 // bit: 1; mask: 0x0002; 0=landscape, 1=portrait
  2567.                 $isPortrait (0x0002 $this->_GetInt2d($recordData10)) >> 1;
  2568.  
  2569.                 // bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
  2570.                 // when this bit is set, do not use flags for those properties
  2571.                 $isNotInit (0x0004 $this->_GetInt2d($recordData10)) >> 2;
  2572.  
  2573.             if (!$isNotInit{
  2574.                 $this->_phpSheet->getPageSetup()->setPaperSize($paperSize);
  2575.                 switch ($isPortrait{
  2576.                 case 0$this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)break;
  2577.                 case 1$this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT)break;
  2578.                 }
  2579.  
  2580.                 $this->_phpSheet->getPageSetup()->setScale($scalefalse);
  2581.                 $this->_phpSheet->getPageSetup()->setFitToPage((bool) $this->_isFitToPages);
  2582.                 $this->_phpSheet->getPageSetup()->setFitToWidth($fitToWidthfalse);
  2583.                 $this->_phpSheet->getPageSetup()->setFitToHeight($fitToHeightfalse);
  2584.             }
  2585.  
  2586.             // offset: 16; size: 8; header margin (IEEE 754 floating-point value)
  2587.             $marginHeader $this->_extractNumber(substr($recordData168));
  2588.             $this->_phpSheet->getPageMargins()->setHeader($marginHeader);
  2589.  
  2590.             // offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
  2591.             $marginFooter $this->_extractNumber(substr($recordData248));
  2592.             $this->_phpSheet->getPageMargins()->setFooter($marginFooter);
  2593.         }
  2594.     }
  2595.  
  2596.     /**
  2597.      * PROTECT - Sheet protection (BIFF2 through BIFF8)
  2598.      *   if this record is omitted, then it also means no sheet protection
  2599.      */
  2600.     private function _readProtect()
  2601.     {
  2602.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2603.         $recordData substr($this->_data$this->_pos + 4$length);
  2604.  
  2605.         // move stream pointer to next record
  2606.         $this->_pos += $length;
  2607.  
  2608.         if ($this->_readDataOnly{
  2609.             return;
  2610.         }
  2611.  
  2612.         // offset: 0; size: 2;
  2613.  
  2614.         // bit 0, mask 0x01; 1 = sheet is protected
  2615.         $bool (0x01 $this->_GetInt2d($recordData0)) >> 0;
  2616.         $this->_phpSheet->getProtection()->setSheet((bool)$bool);
  2617.     }
  2618.  
  2619.     /**
  2620.      * SCENPROTECT
  2621.      */
  2622.     private function _readScenProtect()
  2623.     {
  2624.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2625.         $recordData substr($this->_data$this->_pos + 4$length);
  2626.  
  2627.         // move stream pointer to next record
  2628.         $this->_pos += $length;
  2629.  
  2630.         if ($this->_readDataOnly{
  2631.             return;
  2632.         }
  2633.  
  2634.         // offset: 0; size: 2;
  2635.  
  2636.         // bit: 0, mask 0x01; 1 = scenarios are protected
  2637.         $bool (0x01 $this->_GetInt2d($recordData0)) >> 0;
  2638.  
  2639.         $this->_phpSheet->getProtection()->setScenarios((bool)$bool);
  2640.     }
  2641.  
  2642.     /**
  2643.      * OBJECTPROTECT
  2644.      */
  2645.     private function _readObjectProtect()
  2646.     {
  2647.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2648.         $recordData substr($this->_data$this->_pos + 4$length);
  2649.  
  2650.         // move stream pointer to next record
  2651.         $this->_pos += $length;
  2652.  
  2653.         if ($this->_readDataOnly{
  2654.             return;
  2655.         }
  2656.  
  2657.         // offset: 0; size: 2;
  2658.  
  2659.         // bit: 0, mask 0x01; 1 = objects are protected
  2660.         $bool (0x01 $this->_GetInt2d($recordData0)) >> 0;
  2661.  
  2662.         $this->_phpSheet->getProtection()->setObjects((bool)$bool);
  2663.     }
  2664.  
  2665.     /**
  2666.      * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
  2667.      */
  2668.     private function _readPassword()
  2669.     {
  2670.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2671.         $recordData substr($this->_data$this->_pos + 4$length);
  2672.  
  2673.         // move stream pointer to next record
  2674.         $this->_pos += $length;
  2675.  
  2676.         if (!$this->_readDataOnly{
  2677.             // offset: 0; size: 2; 16-bit hash value of password
  2678.             $password strtoupper(dechex($this->_GetInt2d($recordData0)))// the hashed password
  2679.             $this->_phpSheet->getProtection()->setPassword($passwordtrue);
  2680.         }
  2681.     }
  2682.  
  2683.     /**
  2684.      * Read DEFCOLWIDTH record
  2685.      */
  2686.     private function _readDefColWidth()
  2687.     {
  2688.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2689.         $recordData substr($this->_data$this->_pos + 4$length);
  2690.  
  2691.         // move stream pointer to next record
  2692.         $this->_pos += $length;
  2693.  
  2694.         // offset: 0; size: 2; default column width
  2695.         $width $this->_GetInt2d($recordData0);
  2696.         if ($width != 8{
  2697.             $this->_phpSheet->getDefaultColumnDimension()->setWidth($width);
  2698.         }
  2699.     }
  2700.  
  2701.     /**
  2702.      * Read COLINFO record
  2703.      */
  2704.     private function _readColInfo()
  2705.     {
  2706.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2707.         $recordData substr($this->_data$this->_pos + 4$length);
  2708.  
  2709.         // move stream pointer to next record
  2710.         $this->_pos += $length;
  2711.  
  2712.         if (!$this->_readDataOnly{
  2713.             // offset: 0; size: 2; index to first column in range
  2714.             $fc $this->_GetInt2d($recordData0)// first column index
  2715.  
  2716.             // offset: 2; size: 2; index to last column in range
  2717.             $lc $this->_GetInt2d($recordData2)// first column index
  2718.  
  2719.             // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
  2720.             $width $this->_GetInt2d($recordData4);
  2721.  
  2722.             // offset: 6; size: 2; index to XF record for default column formatting
  2723.             $xfIndex $this->_GetInt2d($recordData6);
  2724.  
  2725.             // offset: 8; size: 2; option flags
  2726.  
  2727.                 // bit: 0; mask: 0x0001; 1= columns are hidden
  2728.                 $isHidden (0x0001 $this->_GetInt2d($recordData8)) >> 0;
  2729.  
  2730.                 // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
  2731.                 $level (0x0700 $this->_GetInt2d($recordData8)) >> 8;
  2732.  
  2733.                 // bit: 12; mask: 0x1000; 1 = collapsed
  2734.                 $isCollapsed (0x1000 $this->_GetInt2d($recordData8)) >> 12;
  2735.  
  2736.             // offset: 10; size: 2; not used
  2737.  
  2738.             for ($i $fc$i <= $lc++$i{
  2739.                 if ($lc == 255 || $lc == 256{
  2740.                     $this->_phpSheet->getDefaultColumnDimension()->setWidth($width 256);
  2741.                     break;
  2742.                 }
  2743.                 $this->_phpSheet->getColumnDimensionByColumn($i)->setWidth($width 256);
  2744.                 $this->_phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
  2745.                 $this->_phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  2746.                 $this->_phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
  2747.                 $this->_phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2748.             }
  2749.         }
  2750.     }
  2751.  
  2752.     /**
  2753.      * ROW
  2754.      *
  2755.      * This record contains the properties of a single row in a
  2756.      * sheet. Rows and cells in a sheet are divided into blocks
  2757.      * of 32 rows.
  2758.      *
  2759.      * --    "OpenOffice.org's Documentation of the Microsoft
  2760.      *         Excel File Format"
  2761.      */
  2762.     private function _readRow()
  2763.     {
  2764.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2765.         $recordData substr($this->_data$this->_pos + 4$length);
  2766.  
  2767.         // move stream pointer to next record
  2768.         $this->_pos += $length;
  2769.  
  2770.         if (!$this->_readDataOnly{
  2771.             // offset: 0; size: 2; index of this row
  2772.             $r $this->_GetInt2d($recordData0);
  2773.  
  2774.             // offset: 2; size: 2; index to column of the first cell which is described by a cell record
  2775.  
  2776.             // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
  2777.  
  2778.             // offset: 6; size: 2;
  2779.  
  2780.                 // bit: 14-0; mask: 0x7FFF; height of the row, in twips = 1/20 of a point
  2781.                 $height (0x7FFF $this->_GetInt2d($recordData6)) >> 0;
  2782.  
  2783.                 // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
  2784.                 $useDefaultHeight (0x8000 $this->_GetInt2d($recordData6)) >> 15;
  2785.  
  2786.                 if (!$useDefaultHeight{
  2787.                     $this->_phpSheet->getRowDimension($r 1)->setRowHeight($height 20);
  2788.                 }
  2789.  
  2790.             // offset: 8; size: 2; not used
  2791.  
  2792.             // offset: 10; size: 2; not used in BIFF5-BIFF8
  2793.  
  2794.             // offset: 12; size: 4; option flags and default row formatting
  2795.  
  2796.                 // bit: 2-0: mask: 0x00000007; outline level of the row
  2797.                 $level (0x00000007 $this->_GetInt4d($recordData12)) >> 0;
  2798.                 $this->_phpSheet->getRowDimension($r 1)->setOutlineLevel($level);
  2799.  
  2800.                 // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
  2801.                 $isCollapsed (0x00000010 $this->_GetInt4d($recordData12)) >> 4;
  2802.                 $this->_phpSheet->getRowDimension($r 1)->setCollapsed($isCollapsed);
  2803.  
  2804.                 // bit: 5; mask: 0x00000020; 1 = row is hidden
  2805.                 $isHidden (0x00000020 $this->_GetInt4d($recordData12)) >> 5;
  2806.                 $this->_phpSheet->getRowDimension($r 1)->setVisible(!$isHidden);
  2807.  
  2808.                 // bit: 7; mask: 0x00000080; 1 = row has explicit format
  2809.                 $hasExplicitFormat (0x00000080 $this->_GetInt4d($recordData12)) >> 7;
  2810.  
  2811.                 // bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
  2812.                 $xfIndex (0x0FFF0000 $this->_GetInt4d($recordData12)) >> 16;
  2813.  
  2814.                 if ($hasExplicitFormat{
  2815.                     $this->_phpSheet->getRowDimension($r 1)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2816.                 }
  2817.         }
  2818.     }
  2819.  
  2820.     /**
  2821.      * Read RK record
  2822.      * This record represents a cell that contains an RK value
  2823.      * (encoded integer or floating-point value). If a
  2824.      * floating-point value cannot be encoded to an RK value,
  2825.      * a NUMBER record will be written. This record replaces the
  2826.      * record INTEGER written in BIFF2.
  2827.      *
  2828.      * --    "OpenOffice.org's Documentation of the Microsoft
  2829.      *         Excel File Format"
  2830.      */
  2831.     private function _readRk()
  2832.     {
  2833.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2834.         $recordData substr($this->_data$this->_pos + 4$length);
  2835.  
  2836.         // move stream pointer to next record
  2837.         $this->_pos += $length;
  2838.  
  2839.         // offset: 0; size: 2; index to row
  2840.         $row $this->_GetInt2d($recordData0);
  2841.  
  2842.         // offset: 2; size: 2; index to column
  2843.         $column $this->_GetInt2d($recordData2);
  2844.         $columnString PHPExcel_Cell::stringFromColumnIndex($column);
  2845.  
  2846.         // Read cell?
  2847.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  2848.             // offset: 4; size: 2; index to XF record
  2849.             $xfIndex $this->_GetInt2d($recordData4);
  2850.  
  2851.             // offset: 6; size: 4; RK value
  2852.             $rknum $this->_GetInt4d($recordData6);
  2853.             $numValue $this->_GetIEEE754($rknum);
  2854.  
  2855.             // add style information
  2856.             if (!$this->_readDataOnly{
  2857.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2858.             }
  2859.  
  2860.             // add cell
  2861.             $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$numValuePHPExcel_Cell_DataType::TYPE_NUMERIC);
  2862.         }
  2863.     }
  2864.  
  2865.     /**
  2866.      * Read LABELSST record
  2867.      * This record represents a cell that contains a string. It
  2868.      * replaces the LABEL record and RSTRING record used in
  2869.      * BIFF2-BIFF5.
  2870.      *
  2871.      * --    "OpenOffice.org's Documentation of the Microsoft
  2872.      *         Excel File Format"
  2873.      */
  2874.     private function _readLabelSst()
  2875.     {
  2876.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2877.         $recordData substr($this->_data$this->_pos + 4$length);
  2878.  
  2879.         // move stream pointer to next record
  2880.         $this->_pos += $length;
  2881.  
  2882.         // offset: 0; size: 2; index to row
  2883.         $row $this->_GetInt2d($recordData0);
  2884.  
  2885.         // offset: 2; size: 2; index to column
  2886.         $column $this->_GetInt2d($recordData2);
  2887.         $columnString PHPExcel_Cell::stringFromColumnIndex($column);
  2888.  
  2889.         // Read cell?
  2890.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  2891.             // offset: 4; size: 2; index to XF record
  2892.             $xfIndex $this->_GetInt2d($recordData4);
  2893.  
  2894.             // offset: 6; size: 4; index to SST record
  2895.             $index $this->_GetInt4d($recordData6);
  2896.  
  2897.             // add cell
  2898.             if (($fmtRuns $this->_sst[$index]['fmtRuns']&& !$this->_readDataOnly{
  2899.                 // then we should treat as rich text
  2900.                 $richText new PHPExcel_RichText($this->_phpSheet->getCell($columnString ($row 1)));
  2901.                 $charPos 0;
  2902.                 for ($i 0$i <= count($this->_sst[$index]['fmtRuns'])++$i{
  2903.                     if (isset($fmtRuns[$i])) {
  2904.                         $text mb_substr($this->_sst[$index]['value']$charPos$fmtRuns[$i]['charPos'$charPos'UTF-8');
  2905.                         $charPos $fmtRuns[$i]['charPos'];
  2906.                     else {
  2907.                         $text mb_substr($this->_sst[$index]['value']$charPosmb_strlen($this->_sst[$index]['value'])'UTF-8');
  2908.                     }
  2909.  
  2910.                     if (mb_strlen($text0{
  2911.                         if ($i == 0// first text run, no style
  2912.                             $richText->createText($text);
  2913.                         else {
  2914.                             $textRun $richText->createTextRun($text);
  2915.                             if (isset($fmtRuns[$i 1])) {
  2916.                                 if ($fmtRuns[$i 1]['fontIndex'4{
  2917.                                     $fontIndex $fmtRuns[$i 1]['fontIndex'];
  2918.                                 else {
  2919.                                     // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  2920.                                     // check the OpenOffice documentation of the FONT record
  2921.                                     $fontIndex $fmtRuns[$i 1]['fontIndex'1;
  2922.                                 }
  2923.                                 $textRun->setFont(clone $this->_objFonts[$fontIndex]);
  2924.                             }
  2925.                         }
  2926.                     }
  2927.                 }
  2928.             else {
  2929.                 $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$this->_sst[$index]['value']PHPExcel_Cell_DataType::TYPE_STRING);
  2930.             }
  2931.  
  2932.             // add style information
  2933.             if (!$this->_readDataOnly{
  2934.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2935.             }
  2936.         }
  2937.     }
  2938.  
  2939.     /**
  2940.      * Read MULRK record
  2941.      * This record represents a cell range containing RK value
  2942.      * cells. All cells are located in the same row.
  2943.      *
  2944.      * --    "OpenOffice.org's Documentation of the Microsoft
  2945.      *         Excel File Format"
  2946.      */
  2947.     private function _readMulRk()
  2948.     {
  2949.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  2950.         $recordData substr($this->_data$this->_pos + 4$length);
  2951.  
  2952.         // move stream pointer to next record
  2953.         $this->_pos += $length;
  2954.  
  2955.         // offset: 0; size: 2; index to row
  2956.         $row $this->_GetInt2d($recordData0);
  2957.  
  2958.         // offset: 2; size: 2; index to first column
  2959.         $colFirst $this->_GetInt2d($recordData2);
  2960.  
  2961.         // offset: var; size: 2; index to last column
  2962.         $colLast $this->_GetInt2d($recordData$length 2);
  2963.         $columns $colLast $colFirst 1;
  2964.  
  2965.         // offset within record data
  2966.         $offset 4;
  2967.  
  2968.         for ($i 0$i $columns++$i{
  2969.             $columnString PHPExcel_Cell::stringFromColumnIndex($colFirst $i);
  2970.  
  2971.             // Read cell?
  2972.             if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  2973.  
  2974.                 // offset: var; size: 2; index to XF record
  2975.                 $xfIndex $this->_GetInt2d($recordData$offset);
  2976.  
  2977.                 // offset: var; size: 4; RK value
  2978.                 $numValue $this->_GetIEEE754($this->_GetInt4d($recordData$offset 2));
  2979.                 if (!$this->_readDataOnly{
  2980.                     // add style
  2981.                     $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2982.                 }
  2983.  
  2984.                 // add cell value
  2985.                 $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$numValuePHPExcel_Cell_DataType::TYPE_NUMERIC);
  2986.             }
  2987.  
  2988.             $offset += 6;
  2989.         }
  2990.     }
  2991.  
  2992.     /**
  2993.      * Read NUMBER record
  2994.      * This record represents a cell that contains a
  2995.      * floating-point value.
  2996.      *
  2997.      * --    "OpenOffice.org's Documentation of the Microsoft
  2998.      *         Excel File Format"
  2999.      */
  3000.     private function _readNumber()
  3001.     {
  3002.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3003.         $recordData substr($this->_data$this->_pos + 4$length);
  3004.  
  3005.         // move stream pointer to next record
  3006.         $this->_pos += $length;
  3007.  
  3008.         // offset: 0; size: 2; index to row
  3009.         $row $this->_GetInt2d($recordData0);
  3010.  
  3011.         // offset: 2; size 2; index to column
  3012.         $column $this->_GetInt2d($recordData2);
  3013.         $columnString PHPExcel_Cell::stringFromColumnIndex($column);
  3014.  
  3015.         // Read cell?
  3016.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  3017.             // offset 4; size: 2; index to XF record
  3018.             $xfIndex $this->_GetInt2d($recordData4);
  3019.  
  3020.             $numValue $this->_extractNumber(substr($recordData68));
  3021.  
  3022.             // add cell style
  3023.             if (!$this->_readDataOnly{
  3024.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3025.             }
  3026.  
  3027.             // add cell value
  3028.             $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$numValuePHPExcel_Cell_DataType::TYPE_NUMERIC);
  3029.         }
  3030.     }
  3031.  
  3032.     /**
  3033.      * Read FORMULA record + perhaps a following STRING record if formula result is a string
  3034.      * This record contains the token array and the result of a
  3035.      * formula cell.
  3036.      *
  3037.      * --    "OpenOffice.org's Documentation of the Microsoft
  3038.      *         Excel File Format"
  3039.      */
  3040.     private function _readFormula()
  3041.     {
  3042.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3043.         $recordData substr($this->_data$this->_pos + 4$length);
  3044.  
  3045.         // move stream pointer to next record
  3046.         $this->_pos += $length;
  3047.  
  3048.         // offset: 0; size: 2; row index
  3049.         $row $this->_GetInt2d($recordData0);
  3050.  
  3051.         // offset: 2; size: 2; col index
  3052.         $column $this->_GetInt2d($recordData2);
  3053.         $columnString PHPExcel_Cell::stringFromColumnIndex($column);
  3054.  
  3055.         // Read cell?
  3056.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  3057.  
  3058.             // offset: 20: size: variable; formula structure
  3059.             $formulaStructure substr($recordData20);
  3060.  
  3061.             // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
  3062.             $options $this->_GetInt2d($recordData14);
  3063.  
  3064.             // bit: 0; mask: 0x0001; 1 = recalculate always
  3065.             // bit: 1; mask: 0x0002; 1 = calculate on open
  3066.             // bit: 2; mask: 0x0008; 1 = part of a shared formula
  3067.             $isPartOfSharedFormula = (bool) (0x0008 $options);
  3068.  
  3069.             // WARNING:
  3070.             // We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
  3071.             // the formula data may be ordinary formula data, therefore we need to check
  3072.             // explicitly for the tExp token (0x01)
  3073.             $isPartOfSharedFormula $isPartOfSharedFormula && ord($formulaStructure{2}== 0x01;
  3074.  
  3075.             if ($isPartOfSharedFormula{
  3076.                 // part of shared formula which means there will be a formula with a tExp token and nothing else
  3077.                 // get the base cell, grab tExp token
  3078.                 $baseRow $this->_GetInt2d($formulaStructure3);
  3079.                 $baseCol $this->_GetInt2d($formulaStructure5);
  3080.                 $this->_baseCell PHPExcel_Cell::stringFromColumnIndex($baseCol)($baseRow 1);
  3081.  
  3082.                 // formula is added to this cell after the sheet has been read
  3083.                 $this->_sharedFormulaParts[$columnString ($row 1)$this->_baseCell;
  3084.             }
  3085.  
  3086.             // offset: 16: size: 4; not used
  3087.  
  3088.             // offset: 4; size: 2; XF index
  3089.             $xfIndex $this->_GetInt2d($recordData4);
  3090.  
  3091.             // offset: 6; size: 8; result of the formula
  3092.             if ( (ord($recordData{6}== 0)
  3093.                 && (ord($recordData{12}== 255)
  3094.                 && (ord($recordData{13}== 255) ) {
  3095.  
  3096.                 // String formula. Result follows in appended STRING record
  3097.                 $dataType PHPExcel_Cell_DataType::TYPE_STRING;
  3098.  
  3099.                 // read possible SHAREDFMLA record
  3100.                 $code $this->_GetInt2d($this->_data$this->_pos);
  3101.                 if ($code == self::XLS_Type_SHAREDFMLA{
  3102.                     $this->_readSharedFmla();
  3103.                 }
  3104.  
  3105.                 // read STRING record
  3106.                 $value $this->_readString();
  3107.  
  3108.             elseif ((ord($recordData{6}== 1)
  3109.                 && (ord($recordData{12}== 255)
  3110.                 && (ord($recordData{13}== 255)) {
  3111.  
  3112.                 // Boolean formula. Result is in +2; 0=false, 1=true
  3113.                 $dataType PHPExcel_Cell_DataType::TYPE_BOOL;
  3114.                 $value = (bool) ord($recordData{8});
  3115.  
  3116.             elseif ((ord($recordData{6}== 2)
  3117.                 && (ord($recordData{12}== 255)
  3118.                 && (ord($recordData{13}== 255)) {
  3119.  
  3120.                 // Error formula. Error code is in +2
  3121.                 $dataType PHPExcel_Cell_DataType::TYPE_ERROR;
  3122.                 $value $this->_mapErrorCode(ord($recordData{8}));
  3123.  
  3124.             elseif ((ord($recordData{6}== 3)
  3125.                 && (ord($recordData{12}== 255)
  3126.                 && (ord($recordData{13}== 255)) {
  3127.  
  3128.                 // Formula result is a null string
  3129.                 $dataType PHPExcel_Cell_DataType::TYPE_NULL;
  3130.                 $value '';
  3131.  
  3132.             else {
  3133.  
  3134.                 // forumla result is a number, first 14 bytes like _NUMBER record
  3135.                 $dataType PHPExcel_Cell_DataType::TYPE_NUMERIC;
  3136.                 $value $this->_extractNumber(substr($recordData68));
  3137.  
  3138.             }
  3139.  
  3140.             // add cell style
  3141.             if (!$this->_readDataOnly{
  3142.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3143.             }
  3144.  
  3145.             // store the formula
  3146.             if (!$isPartOfSharedFormula{
  3147.                 // not part of shared formula
  3148.                 // add cell value. If we can read formula, populate with formula, otherwise just used cached value
  3149.                 try {
  3150.                     if ($this->_version != self::XLS_BIFF8{
  3151.                         throw new Exception('Not BIFF8. Can only read BIFF8 formulas');
  3152.                     }
  3153.                     $formula $this->_getFormulaFromStructure($formulaStructure)// get formula in human language
  3154.                     $this->_phpSheet->getCell($columnString ($row 1))->setValueExplicit('=' $formulaPHPExcel_Cell_DataType::TYPE_FORMULA);
  3155.  
  3156.                 catch (Exception $e{
  3157.                     $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$value$dataType);
  3158.                 }
  3159.             else {
  3160.                 if ($this->_version == self::XLS_BIFF8{
  3161.                     // do nothing at this point, formula id added later in the code
  3162.                 else {
  3163.                     $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$value$dataType);
  3164.                 }
  3165.             }
  3166.  
  3167.             // store the cached calculated value
  3168.             $this->_phpSheet->getCell($columnString ($row 1))->setCalculatedValue($value);
  3169.         }
  3170.     }
  3171.  
  3172.     /**
  3173.      * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
  3174.      * which usually contains relative references.
  3175.      * These will be used to construct the formula in each shared formula part after the sheet is read.
  3176.      */
  3177.     private function _readSharedFmla()
  3178.     {
  3179.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3180.         $recordData substr($this->_data$this->_pos + 4$length);
  3181.  
  3182.         // move stream pointer to next record
  3183.         $this->_pos += $length;
  3184.  
  3185.         // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
  3186.         $cellRange substr($recordData06);
  3187.         $cellRange $this->_readBIFF5CellRangeAddressFixed($cellRange)// note: even BIFF8 uses BIFF5 syntax
  3188.  
  3189.         // offset: 6, size: 1; not used
  3190.  
  3191.         // offset: 7, size: 1; number of existing FORMULA records for this shared formula
  3192.         $no ord($recordData{7});
  3193.  
  3194.         // offset: 8, size: var; Binary token array of the shared formula
  3195.         $formula substr($recordData8);
  3196.  
  3197.         // at this point we only store the shared formula for later use
  3198.         $this->_sharedFormulas[$this->_baseCell$formula;
  3199.  
  3200.     }
  3201.  
  3202.     /**
  3203.      * Read a STRING record from current stream position and advance the stream pointer to next record
  3204.      * This record is used for storing result from FORMULA record when it is a string, and
  3205.      * it occurs directly after the FORMULA record
  3206.      *
  3207.      * @return string The string contents as UTF-8
  3208.      */
  3209.     private function _readString()
  3210.     {
  3211.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3212.         $recordData substr($this->_data$this->_pos + 4$length);
  3213.  
  3214.         // move stream pointer to next record
  3215.         $this->_pos += $length;
  3216.  
  3217.         if ($this->_version == self::XLS_BIFF8{
  3218.             $string $this->_readUnicodeStringLong($recordData);
  3219.             $value $string['value'];
  3220.         else {
  3221.             $string $this->_readByteStringLong($recordData);
  3222.             $value $string['value'];
  3223.         }
  3224.  
  3225.         return $value;
  3226.     }
  3227.  
  3228.     /**
  3229.      * Read BOOLERR record
  3230.      * This record represents a Boolean value or error value
  3231.      * cell.
  3232.      *
  3233.      * --    "OpenOffice.org's Documentation of the Microsoft
  3234.      *         Excel File Format"
  3235.      */
  3236.     private function _readBoolErr()
  3237.     {
  3238.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3239.         $recordData substr($this->_data$this->_pos + 4$length);
  3240.  
  3241.         // move stream pointer to next record
  3242.         $this->_pos += $length;
  3243.  
  3244.         // offset: 0; size: 2; row index
  3245.         $row $this->_GetInt2d($recordData0);
  3246.  
  3247.         // offset: 2; size: 2; column index
  3248.         $column $this->_GetInt2d($recordData2);
  3249.         $columnString PHPExcel_Cell::stringFromColumnIndex($column);
  3250.  
  3251.         // Read cell?
  3252.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  3253.             // offset: 4; size: 2; index to XF record
  3254.             $xfIndex $this->_GetInt2d($recordData4);
  3255.  
  3256.             // offset: 6; size: 1; the boolean value or error value
  3257.             $boolErr ord($recordData{6});
  3258.  
  3259.             // offset: 7; size: 1; 0=boolean; 1=error
  3260.             $isError ord($recordData{7});
  3261.  
  3262.             switch ($isError{
  3263.             case 0// boolean
  3264.                 $value = (bool) $boolErr;
  3265.  
  3266.                 // add cell value
  3267.                 $this->_phpSheet->getCell($columnString ($row 1))->setValueExplicit($valuePHPExcel_Cell_DataType::TYPE_BOOL);
  3268.                 break;
  3269.  
  3270.             case 1// error type
  3271.                 $value $this->_mapErrorCode($boolErr);
  3272.  
  3273.                 // add cell value
  3274.                 $this->_phpSheet->getCell($columnString ($row 1))->setValueExplicit($valuePHPExcel_Cell_DataType::TYPE_ERROR);
  3275.                 break;
  3276.             }
  3277.  
  3278.             // add cell style
  3279.             if (!$this->_readDataOnly{
  3280.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3281.             }
  3282.         }
  3283.     }
  3284.  
  3285.     /**
  3286.      * Read MULBLANK record
  3287.      * This record represents a cell range of empty cells. All
  3288.      * cells are located in the same row
  3289.      *
  3290.      * --    "OpenOffice.org's Documentation of the Microsoft
  3291.      *         Excel File Format"
  3292.      */
  3293.     private function _readMulBlank()
  3294.     {
  3295.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3296.         $recordData substr($this->_data$this->_pos + 4$length);
  3297.  
  3298.         // move stream pointer to next record
  3299.         $this->_pos += $length;
  3300.  
  3301.         // offset: 0; size: 2; index to row
  3302.         $row $this->_GetInt2d($recordData0);
  3303.  
  3304.         // offset: 2; size: 2; index to first column
  3305.         $fc $this->_GetInt2d($recordData2);
  3306.  
  3307.         // offset: 4; size: 2 x nc; list of indexes to XF records
  3308.         // add style information
  3309.         if (!$this->_readDataOnly{
  3310.             for ($i 0$i $length 3++$i{
  3311.                 $columnString PHPExcel_Cell::stringFromColumnIndex($fc $i);
  3312.  
  3313.                 // Read cell?
  3314.                 if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  3315.                     $xfIndex $this->_GetInt2d($recordData$i);
  3316.                     $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3317.                 }
  3318.             }
  3319.         }
  3320.  
  3321.         // offset: 6; size 2; index to last column (not needed)
  3322.     }
  3323.  
  3324.     /**
  3325.      * Read LABEL record
  3326.      * This record represents a cell that contains a string. In
  3327.      * BIFF8 it is usually replaced by the LABELSST record.
  3328.      * Excel still uses this record, if it copies unformatted
  3329.      * text cells to the clipboard.
  3330.      *
  3331.      * --    "OpenOffice.org's Documentation of the Microsoft
  3332.      *         Excel File Format"
  3333.      */
  3334.     private function _readLabel()
  3335.     {
  3336.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3337.         $recordData substr($this->_data$this->_pos + 4$length);
  3338.  
  3339.         // move stream pointer to next record
  3340.         $this->_pos += $length;
  3341.  
  3342.         // offset: 0; size: 2; index to row
  3343.         $row $this->_GetInt2d($recordData0);
  3344.  
  3345.         // offset: 2; size: 2; index to column
  3346.         $column $this->_GetInt2d($recordData2);
  3347.         $columnString PHPExcel_Cell::stringFromColumnIndex($column);
  3348.  
  3349.         // Read cell?
  3350.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  3351.             // offset: 4; size: 2; XF index
  3352.             $xfIndex $this->_GetInt2d($recordData4);
  3353.  
  3354.             // add cell value
  3355.             // todo: what if string is very long? continue record
  3356.             if ($this->_version == self::XLS_BIFF8{
  3357.                 $string $this->_readUnicodeStringLong(substr($recordData6));
  3358.                 $value $string['value'];
  3359.             else {
  3360.                 $string $this->_readByteStringLong(substr($recordData6));
  3361.                 $value $string['value'];
  3362.             }
  3363.             $this->_phpSheet->setCellValueExplicit($columnString ($row 1)$valuePHPExcel_Cell_DataType::TYPE_STRING);
  3364.  
  3365.             // add cell style
  3366.             if (!$this->_readDataOnly{
  3367.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3368.             }
  3369.         }
  3370.     }
  3371.  
  3372.     /**
  3373.      * Read BLANK record
  3374.      */
  3375.     private function _readBlank()
  3376.     {
  3377.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3378.         $recordData substr($this->_data$this->_pos + 4$length);
  3379.  
  3380.         // move stream pointer to next record
  3381.         $this->_pos += $length;
  3382.  
  3383.         // offset: 0; size: 2; row index
  3384.         $row $this->_GetInt2d($recordData0);
  3385.  
  3386.         // offset: 2; size: 2; col index
  3387.         $col $this->_GetInt2d($recordData2);
  3388.         $columnString PHPExcel_Cell::stringFromColumnIndex($col);
  3389.  
  3390.         // Read cell?
  3391.         if !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString$row 1$this->_phpSheet->getTitle()) ) {
  3392.             // offset: 4; size: 2; XF index
  3393.             $xfIndex $this->_GetInt2d($recordData4);
  3394.  
  3395.             // add style information
  3396.             if (!$this->_readDataOnly{
  3397.                 $this->_phpSheet->getCell($columnString ($row 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3398.             }
  3399.         }
  3400.  
  3401.     }
  3402.  
  3403.     /**
  3404.      * Read MSODRAWING record
  3405.      */
  3406.     private function _readMsoDrawing()
  3407.     {
  3408.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3409.         $recordData substr($this->_data$this->_pos + 4$length);
  3410.  
  3411.         // move stream pointer to next record
  3412.         $this->_pos += $length;
  3413.  
  3414.         $this->_drawingData .= $recordData;
  3415.     }
  3416.  
  3417.     /**
  3418.      * Read OBJ record
  3419.      */
  3420.     private function _readObj()
  3421.     {
  3422.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3423.         $recordData substr($this->_data$this->_pos + 4$length);
  3424.  
  3425.         // move stream pointer to next record
  3426.         $this->_pos += $length;
  3427.  
  3428.         if ($this->_readDataOnly || $this->_version != self::XLS_BIFF8{
  3429.             return;
  3430.         }
  3431.  
  3432.         // recordData consists of an array of subrecords looking like this:
  3433.         //    ft: 2 bytes; id number
  3434.         //    cb: 2 bytes; size in bytes of following data
  3435.         //    data: var; subrecord data
  3436.  
  3437.         // for now, we are just interested in the second subrecord containing the object type
  3438.         $ot $this->_GetInt2d($recordData4);
  3439.  
  3440.         $this->_objs[array(
  3441.             'type' => $ot,
  3442.         );
  3443.     }
  3444.  
  3445.     /**
  3446.      * Read WINDOW2 record
  3447.      */
  3448.     private function _readWindow2()
  3449.     {
  3450.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3451.         $recordData substr($this->_data$this->_pos + 4$length);
  3452.  
  3453.         // move stream pointer to next record
  3454.         $this->_pos += $length;
  3455.  
  3456.         // offset: 0; size: 2; option flags
  3457.         $options $this->_GetInt2d($recordData0);
  3458.  
  3459.         // bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
  3460.         $showGridlines = (bool) ((0x0002 $options>> 1);
  3461.         $this->_phpSheet->setShowGridlines($showGridlines);
  3462.  
  3463.         // bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
  3464.         $this->_frozen = (bool) ((0x0008 $options>> 3);
  3465.  
  3466.         // bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
  3467.         $this->_phpSheet->setRightToLeft((bool)((0x0040 $options>> 6));
  3468.  
  3469.         // bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
  3470.         $isActive = (bool) ((0x0400 $options>> 10);
  3471.         if ($isActive{
  3472.             $this->_phpExcel->setActiveSheetIndex($this->_phpExcel->getIndex($this->_phpSheet));
  3473.         }
  3474.     }
  3475.  
  3476.     /**
  3477.      * Read SCL record
  3478.      */
  3479.     private function _readScl()
  3480.     {
  3481.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3482.         $recordData substr($this->_data$this->_pos + 4$length);
  3483.  
  3484.         // move stream pointer to next record
  3485.         $this->_pos += $length;
  3486.  
  3487.         // offset: 0; size: 2; numerator of the view magnification
  3488.         $numerator $this->_GetInt2d($recordData0);
  3489.  
  3490.         // offset: 2; size: 2; numerator of the view magnification
  3491.         $denumerator $this->_GetInt2d($recordData2);
  3492.  
  3493.         // set the zoom scale (in percent)
  3494.         $this->_phpSheet->getSheetView()->setZoomScale($numerator 100 $denumerator);
  3495.     }
  3496.  
  3497.     /**
  3498.      * Read PANE record
  3499.      */
  3500.     private function _readPane()
  3501.     {
  3502.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3503.         $recordData substr($this->_data$this->_pos + 4$length);
  3504.  
  3505.         // move stream pointer to next record
  3506.         $this->_pos += $length;
  3507.  
  3508.         if (!$this->_readDataOnly{
  3509.             // offset: 0; size: 2; position of vertical split
  3510.             $px $this->_GetInt2d($recordData0);
  3511.  
  3512.             // offset: 2; size: 2; position of horizontal split
  3513.             $py $this->_GetInt2d($recordData2);
  3514.  
  3515.             if ($this->_frozen{
  3516.                 // frozen panes
  3517.                 $this->_phpSheet->freezePane(PHPExcel_Cell::stringFromColumnIndex($px($py 1));
  3518.             else {
  3519.                 // unfrozen panes; split windows; not supported by PHPExcel core
  3520.             }
  3521.         }
  3522.     }
  3523.  
  3524.     /**
  3525.      * Read SELECTION record. There is one such record for each pane in the sheet.
  3526.      */
  3527.     private function _readSelection()
  3528.     {
  3529.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3530.         $recordData substr($this->_data$this->_pos + 4$length);
  3531.  
  3532.         // move stream pointer to next record
  3533.         $this->_pos += $length;
  3534.  
  3535.         if (!$this->_readDataOnly{
  3536.             // offset: 0; size: 1; pane identifier
  3537.             $paneId ord($recordData{0});
  3538.  
  3539.             // offset: 1; size: 2; index to row of the active cell
  3540.             $r $this->_GetInt2d($recordData1);
  3541.  
  3542.             // offset: 3; size: 2; index to column of the active cell
  3543.             $c $this->_GetInt2d($recordData3);
  3544.  
  3545.             // offset: 5; size: 2; index into the following cell range list to the
  3546.             //  entry that contains the active cell
  3547.             $index $this->_GetInt2d($recordData5);
  3548.  
  3549.             // offset: 7; size: var; cell range address list containing all selected cell ranges
  3550.             $data substr($recordData7);
  3551.             $cellRangeAddressList $this->_readBIFF5CellRangeAddressList($data)// note: also BIFF8 uses BIFF5 syntax
  3552.  
  3553.             $selectedCells $cellRangeAddressList['cellRangeAddresses'][0];
  3554.             
  3555.             // first row '1' + last row '16384' indicates that full column is selected (apparently also in BIFF8!)
  3556.             if (preg_match('/^([A-Z]+1\:[A-Z]+)16384$/'$selectedCells)) {
  3557.                 $selectedCells preg_replace('/^([A-Z]+1\:[A-Z]+)16384$/''${1}1048576'$selectedCells);
  3558.             }
  3559.  
  3560.             // first row '1' + last row '65536' indicates that full column is selected
  3561.             if (preg_match('/^([A-Z]+1\:[A-Z]+)65536$/'$selectedCells)) {
  3562.                 $selectedCells preg_replace('/^([A-Z]+1\:[A-Z]+)65536$/''${1}1048576'$selectedCells);
  3563.             }
  3564.  
  3565.             // first column 'A' + last column 'IV' indicates that full row is selected
  3566.             if (preg_match('/^(A[0-9]+\:)IV([0-9]+)$/'$selectedCells)) {
  3567.                 $selectedCells preg_replace('/^(A[0-9]+\:)IV([0-9]+)$/''${1}XFD${2}'$selectedCells);
  3568.             }
  3569.  
  3570.             $this->_phpSheet->setSelectedCells($selectedCells);
  3571.         }
  3572.     }
  3573.  
  3574.     /**
  3575.      * MERGEDCELLS
  3576.      *
  3577.      * This record contains the addresses of merged cell ranges
  3578.      * in the current sheet.
  3579.      *
  3580.      * --    "OpenOffice.org's Documentation of the Microsoft
  3581.      *         Excel File Format"
  3582.      */
  3583.     private function _readMergedCells()
  3584.     {
  3585.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3586.         $recordData substr($this->_data$this->_pos + 4$length);
  3587.  
  3588.         // move stream pointer to next record
  3589.         $this->_pos += $length;
  3590.  
  3591.         if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly{
  3592.             $cellRangeAddressList $this->_readBIFF8CellRangeAddressList($recordData);
  3593.             foreach ($cellRangeAddressList['cellRangeAddresses'as $cellRangeAddress{
  3594.                 $this->_phpSheet->mergeCells($cellRangeAddress);
  3595.             }
  3596.         }
  3597.     }
  3598.  
  3599.     /**
  3600.      * Read HYPERLINK record
  3601.      */
  3602.     private function _readHyperLink()
  3603.     {
  3604.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3605.         $recordData substr($this->_data$this->_pos + 4$length);
  3606.  
  3607.         // move stream pointer forward to next record
  3608.         $this->_pos += $length;
  3609.  
  3610.         if (!$this->_readDataOnly{
  3611.             // offset: 0; size: 8; cell range address of all cells containing this hyperlink
  3612.             try {
  3613.                 $cellRange $this->_readBIFF8CellRangeAddressFixed($recordData08);
  3614.             catch (Exception $e{
  3615.                 return;
  3616.             }
  3617.  
  3618.             // offset: 8, size: 16; GUID of StdLink
  3619.  
  3620.             // offset: 24, size: 4; unknown value
  3621.  
  3622.             // offset: 28, size: 4; option flags
  3623.  
  3624.                 // bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
  3625.                 $isFileLinkOrUrl (0x00000001 $this->_GetInt2d($recordData28)) >> 0;
  3626.  
  3627.                 // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
  3628.                 $isAbsPathOrUrl (0x00000001 $this->_GetInt2d($recordData28)) >> 1;
  3629.  
  3630.                 // bit: 2 (and 4); mask: 0x00000014; 0 = no description
  3631.                 $hasDesc (0x00000014 $this->_GetInt2d($recordData28)) >> 2;
  3632.  
  3633.                 // bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
  3634.                 $hasText (0x00000008 $this->_GetInt2d($recordData28)) >> 3;
  3635.  
  3636.                 // bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
  3637.                 $hasFrame (0x00000080 $this->_GetInt2d($recordData28)) >> 7;
  3638.  
  3639.                 // bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
  3640.                 $isUNC (0x00000100 $this->_GetInt2d($recordData28)) >> 8;
  3641.  
  3642.             // offset within record data
  3643.             $offset 32;
  3644.  
  3645.             if ($hasDesc{
  3646.                 // offset: 32; size: var; character count of description text
  3647.                 $dl $this->_GetInt4d($recordData32);
  3648.                 // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
  3649.                 $desc $this->_encodeUTF16(substr($recordData36($dl 1))false);
  3650.                 $offset += $dl;
  3651.             }
  3652.             if ($hasFrame{
  3653.                 $fl $this->_GetInt4d($recordData$offset);
  3654.                 $offset += $fl;
  3655.             }
  3656.  
  3657.             // detect type of hyperlink (there are 4 types)
  3658.             $hyperlinkType null;
  3659.  
  3660.             if ($isUNC{
  3661.                 $hyperlinkType 'UNC';
  3662.             else if (!$isFileLinkOrUrl{
  3663.                 $hyperlinkType 'workbook';
  3664.             else if (ord($recordData{$offset}== 0x03{
  3665.                 $hyperlinkType 'local';
  3666.             else if (ord($recordData{$offset}== 0xE0{
  3667.                 $hyperlinkType 'URL';
  3668.             }
  3669.  
  3670.             switch ($hyperlinkType{
  3671.             case 'URL':
  3672.                 // section 5.58.2: Hyperlink containing a URL
  3673.                 // e.g. http://example.org/index.php
  3674.  
  3675.                 // offset: var; size: 16; GUID of URL Moniker
  3676.                 $offset += 16;
  3677.                 // offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
  3678.                 $us $this->_GetInt4d($recordData$offset);
  3679.                 $offset += 4;
  3680.                 // offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
  3681.                 $url $this->_encodeUTF16(substr($recordData$offset$us 2)false);
  3682.                 $url .= $hasText '#' '';
  3683.                 $offset += $us;
  3684.                 break;
  3685.  
  3686.             case 'local':
  3687.                 // section 5.58.3: Hyperlink to local file
  3688.                 // examples:
  3689.                 //   mydoc.txt
  3690.                 //   ../../somedoc.xls#Sheet!A1
  3691.  
  3692.                 // offset: var; size: 16; GUI of File Moniker
  3693.                 $offset += 16;
  3694.  
  3695.                 // offset: var; size: 2; directory up-level count.
  3696.                 $upLevelCount $this->_GetInt2d($recordData$offset);
  3697.                 $offset += 2;
  3698.  
  3699.                 // offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
  3700.                 $sl $this->_GetInt4d($recordData$offset);
  3701.                 $offset += 4;
  3702.  
  3703.                 // offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
  3704.                 $shortenedFilePath substr($recordData$offset$sl);
  3705.                 $shortenedFilePath $this->_encodeUTF16($shortenedFilePathtrue);
  3706.                 $shortenedFilePath substr($shortenedFilePath0-1)// remove trailing zero
  3707.  
  3708.                 $offset += $sl;
  3709.  
  3710.                 // offset: var; size: 24; unknown sequence
  3711.                 $offset += 24;
  3712.  
  3713.                 // extended file path
  3714.                 // offset: var; size: 4; size of the following file link field including string lenth mark
  3715.                 $sz $this->_GetInt4d($recordData$offset);
  3716.                 $offset += 4;
  3717.                 
  3718.                 // only present if $sz > 0
  3719.                 if ($sz 0{
  3720.                     // offset: var; size: 4; size of the character array of the extended file path and name
  3721.                     $xl $this->_GetInt4d($recordData$offset);
  3722.                     $offset += 4;
  3723.                     
  3724.                     // offset: var; size 2; unknown
  3725.                     $offset += 2;
  3726.                     
  3727.                     // offset: var; size $xl; character array of the extended file path and name.
  3728.                     $extendedFilePath substr($recordData$offset$xl);
  3729.                     $extendedFilePath $this->_encodeUTF16($extendedFilePathfalse);
  3730.                     $offset += $xl;
  3731.                 }
  3732.  
  3733.                 // construct the path
  3734.                 $url str_repeat('..\\'$upLevelCount);
  3735.                 $url .= ($sz 0?
  3736.                     $extendedFilePath $shortenedFilePath// use extended path if available
  3737.                 $url .= $hasText '#' '';
  3738.  
  3739.                 break;
  3740.  
  3741.  
  3742.             case 'UNC':
  3743.                 // section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
  3744.                 // todo: implement
  3745.                 return;
  3746.  
  3747.             case 'workbook':
  3748.                 // section 5.58.5: Hyperlink to the Current Workbook
  3749.                 // e.g. Sheet2!B1:C2, stored in text mark field
  3750.                 $url 'sheet://';
  3751.                 break;
  3752.  
  3753.             default:
  3754.                 return;
  3755.  
  3756.             }
  3757.  
  3758.             if ($hasText{
  3759.                 // offset: var; size: 4; character count of text mark including trailing zero word
  3760.                 $tl $this->_GetInt4d($recordData$offset);
  3761.                 $offset += 4;
  3762.                 // offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
  3763.                 $text $this->_encodeUTF16(substr($recordData$offset($tl 1))false);
  3764.                 $url .= $text;
  3765.             }
  3766.  
  3767.             // apply the hyperlink to all the relevant cells
  3768.             foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cellRangeas $coordinate{
  3769.                 $this->_phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
  3770.             }
  3771.         }
  3772.     }
  3773.  
  3774.     /**
  3775.      * Read SHEETLAYOUT record. Stores sheet tab color information.
  3776.      */
  3777.     private function _readSheetLayout()
  3778.     {
  3779.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3780.         $recordData substr($this->_data$this->_pos + 4$length);
  3781.  
  3782.         // move stream pointer to next record
  3783.         $this->_pos += $length;
  3784.  
  3785.         // local pointer in record data
  3786.         $offset 0;
  3787.  
  3788.         if (!$this->_readDataOnly{
  3789.             // offset: 0; size: 2; repeated record identifier 0x0862
  3790.  
  3791.             // offset: 2; size: 10; not used
  3792.  
  3793.             // offset: 12; size: 4; size of record data
  3794.             // Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
  3795.             $sz $this->_GetInt4d($recordData12);
  3796.  
  3797.             switch ($sz{
  3798.                 case 0x14:
  3799.                     // offset: 16; size: 2; color index for sheet tab
  3800.                     $colorIndex $this->_GetInt2d($recordData16);
  3801.                     $color $this->_readColor($colorIndex);
  3802.                     $this->_phpSheet->getTabColor()->setRGB($color['rgb']);
  3803.                     break;
  3804.  
  3805.                 case 0x28:
  3806.                     // TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
  3807.                     return;
  3808.                     break;
  3809.             }
  3810.         }
  3811.     }
  3812.  
  3813.     /**
  3814.      * Read SHEETPROTECTION record
  3815.      */
  3816.     private function _readSheetProtection()
  3817.     {
  3818.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3819.         $recordData substr($this->_data$this->_pos + 4$length);
  3820.  
  3821.         // move stream pointer to next record
  3822.         $this->_pos += $length;
  3823.  
  3824.         if ($this->_readDataOnly{
  3825.             return;
  3826.         }
  3827.  
  3828.         // offset: 0; size: 2; repeated record header
  3829.  
  3830.         // offset: 2; size: 9; not used
  3831.  
  3832.         // offset: 11; size: 8; unknown data
  3833.  
  3834.         // offset: 19; size: 2; option flags
  3835.         $options $this->_GetInt2d($recordData19);
  3836.  
  3837.         // bit: 0; mask 0x0001; 1 = user may edit objects, 0 = users must not edit objects
  3838.         $bool (0x0001 $options>> 0;
  3839.         $this->_phpSheet->getProtection()->setObjects(!$bool);
  3840.  
  3841.         // bit: 1; mask 0x0002; edit scenarios
  3842.         $bool (0x0002 $options>> 1;
  3843.         $this->_phpSheet->getProtection()->setScenarios(!$bool);
  3844.  
  3845.         // bit: 2; mask 0x0004; format cells
  3846.         $bool (0x0004 $options>> 2;
  3847.         $this->_phpSheet->getProtection()->setFormatCells(!$bool);
  3848.  
  3849.         // bit: 3; mask 0x0008; format columns
  3850.         $bool (0x0008 $options>> 3;
  3851.         $this->_phpSheet->getProtection()->setFormatColumns(!$bool);
  3852.  
  3853.         // bit: 4; mask 0x0010; format rows
  3854.         $bool (0x0010 $options>> 4;
  3855.         $this->_phpSheet->getProtection()->setFormatRows(!$bool);
  3856.  
  3857.         // bit: 5; mask 0x0020; insert columns
  3858.         $bool (0x0020 $options>> 5;
  3859.         $this->_phpSheet->getProtection()->setInsertColumns(!$bool);
  3860.  
  3861.         // bit: 6; mask 0x0040; insert rows
  3862.         $bool (0x0040 $options>> 6;
  3863.         $this->_phpSheet->getProtection()->setInsertRows(!$bool);
  3864.  
  3865.         // bit: 7; mask 0x0080; insert hyperlinks
  3866.         $bool (0x0080 $options>> 7;
  3867.         $this->_phpSheet->getProtection()->setInsertHyperlinks(!$bool);
  3868.  
  3869.         // bit: 8; mask 0x0100; delete columns
  3870.         $bool (0x0100 $options>> 8;
  3871.         $this->_phpSheet->getProtection()->setDeleteColumns(!$bool);
  3872.  
  3873.         // bit: 9; mask 0x0200; delete rows
  3874.         $bool (0x0200 $options>> 9;
  3875.         $this->_phpSheet->getProtection()->setDeleteRows(!$bool);
  3876.  
  3877.         // bit: 10; mask 0x0400; select locked cells
  3878.         $bool (0x0400 $options>> 10;
  3879.         $this->_phpSheet->getProtection()->setSelectLockedCells(!$bool);
  3880.  
  3881.         // bit: 11; mask 0x0800; sort cell range
  3882.         $bool (0x0800 $options>> 11;
  3883.         $this->_phpSheet->getProtection()->setSort(!$bool);
  3884.  
  3885.         // bit: 12; mask 0x1000; auto filter
  3886.         $bool (0x1000 $options>> 12;
  3887.         $this->_phpSheet->getProtection()->setAutoFilter(!$bool);
  3888.  
  3889.         // bit: 13; mask 0x2000; pivot tables
  3890.         $bool (0x2000 $options>> 13;
  3891.         $this->_phpSheet->getProtection()->setPivotTables(!$bool);
  3892.  
  3893.         // bit: 14; mask 0x4000; select unlocked cells
  3894.         $bool (0x4000 $options>> 14;
  3895.         $this->_phpSheet->getProtection()->setSelectUnlockedCells(!$bool);
  3896.  
  3897.         // offset: 21; size: 2; not used
  3898.     }
  3899.     
  3900.     /**
  3901.      * Read RANGEPROTECTION record
  3902.      * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
  3903.      * where it is referred to as FEAT record
  3904.      */
  3905.     private function _readRangeProtection()
  3906.     {
  3907.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3908.         $recordData substr($this->_data$this->_pos + 4$length);
  3909.  
  3910.         // move stream pointer to next record
  3911.         $this->_pos += $length;
  3912.  
  3913.         // local pointer in record data
  3914.         $offset 0;
  3915.  
  3916.         if (!$this->_readDataOnly{
  3917.             $offset += 12;
  3918.  
  3919.             // offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
  3920.             $isf $this->_GetInt2d($recordData12);
  3921.             if ($isf != 2{
  3922.                 // we only read FEAT records of type 2
  3923.                 return;
  3924.             }
  3925.             $offset += 2;
  3926.  
  3927.             $offset += 5;
  3928.  
  3929.             // offset: 19; size: 2; count of ref ranges this feature is on
  3930.             $cref $this->_GetInt2d($recordData19);
  3931.             $offset += 2;
  3932.  
  3933.             $offset += 6;
  3934.  
  3935.             // offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
  3936.             $cellRanges array();
  3937.             for ($i 0$i $cref++$i{
  3938.                 try {
  3939.                     $cellRange $this->_readBIFF8CellRangeAddressFixed(substr($recordData27 $i8));
  3940.                 catch (Exception $e{
  3941.                     return;
  3942.                 }
  3943.                 $cellRanges[$cellRange;
  3944.                 $offset += 8;
  3945.             }
  3946.  
  3947.             // offset: var; size: var; variable length of feature specific data
  3948.             $rgbFeat substr($recordData$offset);
  3949.             $offset += 4;
  3950.  
  3951.             // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
  3952.             $wPassword $this->_GetInt4d($recordData$offset);
  3953.             $offset += 4;
  3954.  
  3955.             // Apply range protection to sheet
  3956.             if ($cellRanges{
  3957.                 $this->_phpSheet->protectCells(implode(' '$cellRanges)strtoupper(dechex($wPassword))true);
  3958.             }
  3959.         }
  3960.     }
  3961.  
  3962.     /**
  3963.      * Read IMDATA record
  3964.      */
  3965.     private function _readImData()
  3966.     {
  3967.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  3968.  
  3969.         // get spliced record data
  3970.         $splicedRecordData $this->_getSplicedRecordData();
  3971.         $recordData $splicedRecordData['recordData'];
  3972.  
  3973.         // UNDER CONSTRUCTION
  3974.  
  3975.         // offset: 0; size: 2; image format
  3976.         $cf $this->_GetInt2d($recordData0);
  3977.  
  3978.         // offset: 2; size: 2; environment from which the file was written
  3979.         $env $this->_GetInt2d($recordData2);
  3980.  
  3981.         // offset: 4; size: 4; length of the image data
  3982.         $lcb $this->_GetInt4d($recordData4);
  3983.  
  3984.         // offset: 8; size: var; image data
  3985.         $iData substr($recordData8);
  3986.  
  3987.         switch ($cf{
  3988.         case 0x09// Windows bitmap format
  3989.             // BITMAPCOREINFO
  3990.             // 1. BITMAPCOREHEADER
  3991.             // offset: 0; size: 4; bcSize, Specifies the number of bytes required by the structure
  3992.             $bcSize $this->_GetInt4d($iData0);
  3993.             var_dump($bcSize);
  3994.  
  3995.             // offset: 4; size: 2; bcWidth, specifies the width of the bitmap, in pixels
  3996.             $bcWidth $this->_GetInt2d($iData4);
  3997.             var_dump($bcWidth);
  3998.  
  3999.             // offset: 6; size: 2; bcHeight, specifies the height of the bitmap, in pixels.
  4000.             $bcHeight $this->_GetInt2d($iData6);
  4001.             var_dump($bcHeight);
  4002.             $ih imagecreatetruecolor($bcWidth$bcHeight);
  4003.  
  4004.             // offset: 8; size: 2; bcPlanes, specifies the number of planes for the target device. This value must be 1
  4005.  
  4006.             // offset: 10; size: 2; bcBitCount specifies the number of bits-per-pixel. This value must be 1, 4, 8, or 24
  4007.             $bcBitCount $this->_GetInt2d($iData10);
  4008.             var_dump($bcBitCount);
  4009.  
  4010.             $rgbString substr($iData12);
  4011.             $rgbTriples array();
  4012.             while (strlen($rgbString0{
  4013.                 $rgbTriples[unpack('Cb/Cg/Cr'$rgbString);
  4014.                 $rgbString substr($rgbString3);
  4015.             }
  4016.             $x 0;
  4017.             $y 0;
  4018.             foreach ($rgbTriples as $i => $rgbTriple{
  4019.                 $color imagecolorallocate($ih$rgbTriple['r']$rgbTriple['g']$rgbTriple['b']);
  4020.                 imagesetpixel($ih$x$bcHeight $y$color);
  4021.                 $x ($x 1$bcWidth;
  4022.                 $y $y floor(($x 1$bcWidth);
  4023.             }
  4024.             //imagepng($ih, 'image.png');
  4025.  
  4026.             $drawing new PHPExcel_Worksheet_Drawing();
  4027.             $drawing->setPath($filename);
  4028.             $drawing->setWorksheet($this->_phpSheet);
  4029.  
  4030.             break;
  4031.  
  4032.         case 0x02// Windows metafile or Macintosh PICT format
  4033.         case 0x0e// native format
  4034.         default;
  4035.             break;
  4036.  
  4037.         }
  4038.  
  4039.         // _getSplicedRecordData() takes care of moving current position in data stream
  4040.     }
  4041.  
  4042.     /**
  4043.      * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
  4044.      * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
  4045.      * In this case, we must treat the CONTINUE record as a MSODRAWING record
  4046.      */
  4047.     private function _readContinue()
  4048.     {
  4049.         $length $this->_GetInt2d($this->_data$this->_pos + 2);
  4050.         $recordData substr($this->_data$this->_pos + 4$length);
  4051.  
  4052.         // move stream pointer to next record
  4053.         $this->_pos += $length;
  4054.  
  4055.         // check if we are reading drawing data
  4056.         // this is in case a free CONTINUE record occurs in other circumstances we are unaware of
  4057.         if ($this->_drawingData == ''{
  4058.             return;
  4059.         }
  4060.  
  4061.         // check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
  4062.         if (strlen($recordData4{
  4063.             return;
  4064.         }
  4065.  
  4066.         // dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
  4067.         // look inside CONTINUE record to see if it looks like a part of an Escher stream
  4068.         // we know that Escher stream may be split at least at
  4069.         //        0xF003 MsofbtSpgrContainer
  4070.         //        0xF004 MsofbtSpContainer
  4071.         //        0xF00D MsofbtClientTextbox
  4072.         $validSplitPoints array(0xF0030xF0040xF00D)// add identifiers if we find more
  4073.  
  4074.         $splitPoint $this->_GetInt2d($recordData2);
  4075.         if (in_array($splitPoint$validSplitPoints)) {
  4076.             $this->_drawingData .= $recordData;
  4077.         }
  4078.     }
  4079.  
  4080.  
  4081.     /**
  4082.      * Reads a record from current position in data stream and continues reading data as long as CONTINUE
  4083.      * records are found. Splices the record data pieces and returns the combined string as if record data
  4084.      * is in one piece.
  4085.      * Moves to next current position in data stream to start of next record different from a CONtINUE record
  4086.      *
  4087.      * @return array 
  4088.      */
  4089.     private function _getSplicedRecordData()
  4090.     {
  4091.         $data '';
  4092.         $spliceOffsets array();
  4093.  
  4094.         $i 0;
  4095.         $spliceOffsets[00;
  4096.  
  4097.         do {
  4098.             ++$i;
  4099.  
  4100.             // offset: 0; size: 2; identifier
  4101.             $identifier $this->_GetInt2d($this->_data$this->_pos);
  4102.             // offset: 2; size: 2; length
  4103.             $length $this->_GetInt2d($this->_data$this->_pos + 2);
  4104.             $data .= substr($this->_data$this->_pos + 4$length);
  4105.  
  4106.             $spliceOffsets[$i$spliceOffsets[$i 1$length;
  4107.  
  4108.             $this->_pos += $length;
  4109.             $nextIdentifier $this->_GetInt2d($this->_data$this->_pos);
  4110.         }
  4111.         while ($nextIdentifier == self::XLS_Type_CONTINUE);
  4112.  
  4113.         $splicedData array(
  4114.             'recordData' => $data,
  4115.             'spliceOffsets' => $spliceOffsets,
  4116.         );
  4117.  
  4118.         return $splicedData;
  4119.  
  4120.     }
  4121.  
  4122.     /**
  4123.      * Convert formula structure into human readable Excel formula like 'A3+A5*5'
  4124.      *
  4125.      * @param string $formulaStructure The complete binary data for the formula
  4126.      * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4127.      * @return string Human readable formula
  4128.      */
  4129.     private function _getFormulaFromStructure($formulaStructure$baseCell 'A1')
  4130.     {
  4131.         // offset: 0; size: 2; size of the following formula data
  4132.         $sz $this->_GetInt2d($formulaStructure0);
  4133.  
  4134.         // offset: 2; size: sz
  4135.         $formulaData substr($formulaStructure2$sz);
  4136.  
  4137.         // for debug: dump the formula data
  4138.         //echo '<xmp>';
  4139.         //echo 'size: ' . $sz . "\n";
  4140.         //echo 'the entire formula data: ';
  4141.         //Debug::dump($formulaData);
  4142.         //echo "\n----\n";
  4143.  
  4144.         // offset: 2 + sz; size: variable (optional)
  4145.         if (strlen($formulaStructure$sz{
  4146.             $additionalData substr($formulaStructure$sz);
  4147.  
  4148.             // for debug: dump the additional data
  4149.             //echo 'the entire additional data: ';
  4150.             //Debug::dump($additionalData);
  4151.             //echo "\n----\n";
  4152.  
  4153.         else {
  4154.             $additionalData '';
  4155.         }
  4156.  
  4157.         return $this->_getFormulaFromData($formulaData$additionalData$baseCell);
  4158.     }
  4159.  
  4160.     /**
  4161.      * Take formula data and additional data for formula and return human readable formula
  4162.      *
  4163.      * @param string $formulaData The binary data for the formula itself
  4164.      * @param string $additionalData Additional binary data going with the formula
  4165.      * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4166.      * @return string Human readable formula
  4167.      */
  4168.     private function _getFormulaFromData($formulaData,  $additionalData ''$baseCell 'A1')
  4169.     {
  4170.         // start parsing the formula data
  4171.         $tokens array();
  4172.  
  4173.         while (strlen($formulaDataand $token $this->_getNextToken($formulaData$baseCell)) {
  4174.             $tokens[$token;
  4175.             $formulaData substr($formulaData$token['size']);
  4176.  
  4177.             // for debug: dump the token
  4178.             //var_dump($token);
  4179.         }
  4180.  
  4181.         $formulaString $this->_createFormulaFromTokens($tokens$additionalData);
  4182.  
  4183.         return $formulaString;
  4184.     }
  4185.  
  4186.     /**
  4187.      * Take array of tokens together with additional data for formula and return human readable formula
  4188.      *
  4189.      * @param array $tokens 
  4190.      * @param array $additionalData Additional binary data going with the formula
  4191.      * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4192.      * @return string Human readable formula
  4193.      */
  4194.     private function _createFormulaFromTokens($tokens$additionalData)
  4195.     {
  4196.         // empty formula?
  4197.         if (count($tokens== 0{
  4198.             return '';
  4199.         }
  4200.  
  4201.         $formulaStrings array();
  4202.         foreach ($tokens as $token{
  4203.             // initialize spaces
  4204.             $space0 = isset($space0$space0 ''// spaces before next token, not tParen
  4205.             $space1 = isset($space1$space1 ''// carriage returns before next token, not tParen
  4206.             $space2 = isset($space2$space2 ''// spaces before opening parenthesis
  4207.             $space3 = isset($space3$space3 ''// carriage returns before opening parenthesis
  4208.             $space4 = isset($space4$space4 ''// spaces before closing parenthesis
  4209.             $space5 = isset($space5$space5 ''// carriage returns before closing parenthesis
  4210.  
  4211.             switch ($token['name']{
  4212.             case 'tAdd'// addition
  4213.             case 'tConcat'// addition
  4214.             case 'tDiv'// division
  4215.             case 'tEQ'// equaltiy
  4216.             case 'tGE'// greater than or equal
  4217.             case 'tGT'// greater than
  4218.             case 'tIsect'// intersection
  4219.             case 'tLE'// less than or equal
  4220.             case 'tList'// less than or equal
  4221.             case 'tLT'// less than
  4222.             case 'tMul'// multiplication
  4223.             case 'tNE'// multiplication
  4224.             case 'tPower'// power
  4225.             case 'tRange'// range
  4226.             case 'tSub'// subtraction
  4227.                 $op2 array_pop($formulaStrings);
  4228.                 $op1 array_pop($formulaStrings);
  4229.                 $formulaStrings["$op1$space1$space0{$token['data']}$op2";
  4230.                 unset($space0$space1);
  4231.                 break;
  4232.             case 'tUplus'// unary plus
  4233.             case 'tUminus'// unary minus
  4234.                 $op array_pop($formulaStrings);
  4235.                 $formulaStrings["$space1$space0{$token['data']}$op";
  4236.                 unset($space0$space1);
  4237.                 break;
  4238.             case 'tPercent'// percent sign
  4239.                 $op array_pop($formulaStrings);
  4240.                 $formulaStrings["$op$space1$space0{$token['data']}";
  4241.                 unset($space0$space1);
  4242.                 break;
  4243.             case 'tAttrVolatile'// indicates volatile function
  4244.             case 'tAttrIf':
  4245.             case 'tAttrSkip':
  4246.             case 'tAttrChoose':
  4247.                 // token is only important for Excel formula evaluator
  4248.                 // do nothing
  4249.                 break;
  4250.             case 'tAttrSpace'// space / carriage return
  4251.                 // space will be used when next token arrives, do not alter formulaString stack
  4252.                 switch ($token['data']['spacetype']{
  4253.                 case 'type0':
  4254.                     $space0 str_repeat(' '$token['data']['spacecount']);
  4255.                     break;
  4256.                 case 'type1':
  4257.                     $space1 str_repeat("\n"$token['data']['spacecount']);
  4258.                     break;
  4259.                 case 'type2':
  4260.                     $space2 str_repeat(' '$token['data']['spacecount']);
  4261.                     break;
  4262.                 case 'type3':
  4263.                     $space3 str_repeat("\n"$token['data']['spacecount']);
  4264.                     break;
  4265.                 case 'type4':
  4266.                     $space4 str_repeat(' '$token['data']['spacecount']);
  4267.                     break;
  4268.                 case 'type5':
  4269.                     $space5 str_repeat("\n"$token['data']['spacecount']);
  4270.                     break;
  4271.                 }
  4272.                 break;
  4273.             case 'tAttrSum'// SUM function with one parameter
  4274.                 $op array_pop($formulaStrings);
  4275.                 $formulaStrings["{$space1}{$space0}SUM($op)";
  4276.                 unset($space0$space1);
  4277.                 break;
  4278.             case 'tFunc'// function with fixed number of arguments
  4279.             case 'tFuncV'// function with variable number of arguments
  4280.                 $ops array()// array of operators
  4281.                 for ($i 0$i $token['data']['args']++$i{
  4282.                     $ops[array_pop($formulaStrings);
  4283.                 }
  4284.                 $ops array_reverse($ops);
  4285.                 $formulaStrings["$space1$space0{$token['data']['function']}(implode(','$ops")";
  4286.                 unset($space0$space1);
  4287.                 break;
  4288.             case 'tParen'// parenthesis
  4289.                 $expression array_pop($formulaStrings);
  4290.                 $formulaStrings["$space3$space2($expression$space5$space4)";
  4291.                 unset($space2$space3$space4$space5);
  4292.                 break;
  4293.             case 'tArray'// array constant
  4294.                 $constantArray $this->_readBIFF8ConstantArray($additionalData);
  4295.                 $formulaStrings[$space1 $space0 $constantArray['value'];
  4296.                 $additionalData substr($additionalData$constantArray['size'])// bite of chunk of additional data
  4297.                 unset($space0$space1);
  4298.                 break;
  4299.             case 'tMemArea':
  4300.                 // bite off chunk of additional data
  4301.                 $cellRangeAddressList $this->_readBIFF8CellRangeAddressList($additionalData);
  4302.                 $additionalData substr($additionalData$cellRangeAddressList['size']);
  4303.                 $formulaStrings["$space1$space0{$token['data']}";
  4304.                 unset($space0$space1);
  4305.                 break;
  4306.             case 'tArea'// cell range address
  4307.             case 'tBool'// boolean
  4308.             case 'tErr'// error code
  4309.             case 'tInt'// integer
  4310.             case 'tMemErr':
  4311.             case 'tMemFunc':
  4312.             case 'tMissArg':
  4313.             case 'tName':
  4314.             case 'tNum'// number
  4315.             case 'tRef'// single cell reference
  4316.             case 'tRef3d'// 3d cell reference
  4317.             case 'tArea3d'// 3d cell range reference
  4318.             case 'tRefN':
  4319.             case 'tAreaN':
  4320.             case 'tStr'// string
  4321.                 $formulaStrings["$space1$space0{$token['data']}";
  4322.                 unset($space0$space1);
  4323.                 break;
  4324.             }
  4325.         }
  4326.         $formulaString $formulaStrings[0];
  4327.  
  4328.         // for debug: dump the human readable formula
  4329.         //echo '----' . "\n";
  4330.         //echo 'Formula: ' . $formulaString;
  4331.  
  4332.         return $formulaString;
  4333.     }
  4334.  
  4335.     /**
  4336.      * Fetch next token from binary formula data
  4337.      *
  4338.      * @param string Formula data
  4339.      * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4340.      * @return array 
  4341.      * @throws Exception
  4342.      */
  4343.     private function _getNextToken($formulaData$baseCell 'A1')
  4344.     {
  4345.         // offset: 0; size: 1; token id
  4346.         $id ord($formulaData[0])// token id
  4347.         $name false// initialize token name
  4348.  
  4349.         switch ($id{
  4350.         case 0x03$name 'tAdd';        $size 1;    $data '+';    break;
  4351.         case 0x04$name 'tSub';        $size 1;    $data '-';    break;
  4352.         case 0x05$name 'tMul';        $size 1;    $data '*';    break;
  4353.         case 0x06$name 'tDiv';        $size 1;    $data '/';    break;
  4354.         case 0x07$name 'tPower';    $size 1;    $data '^';    break;
  4355.         case 0x08$name 'tConcat';    $size 1;    $data '&';    break;
  4356.         case 0x09$name 'tLT';        $size 1;    $data '<';    break;
  4357.         case 0x0A$name 'tLE';        $size 1;    $data '<=';    break;
  4358.         case 0x0B$name 'tEQ';        $size 1;    $data '=';    break;
  4359.         case 0x0C$name 'tGE';        $size 1;    $data '>=';    break;
  4360.         case 0x0D$name 'tGT';        $size 1;    $data '>';    break;
  4361.         case 0x0E$name 'tNE';        $size 1;    $data '<>';    break;
  4362.         case 0x0F$name 'tIsect';    $size 1;    $data ' ';    break;
  4363.         case 0x10$name 'tList';        $size 1;    $data ',';    break;
  4364.         case 0x11$name 'tRange';    $size 1;    $data ':';    break;
  4365.         case 0x12$name 'tUplus';    $size 1;    $data '+';    break;
  4366.         case 0x13$name 'tUminus';    $size 1;    $data '-';    break;
  4367.         case 0x14$name 'tPercent';    $size 1;    $data '%';    break;
  4368.         case 0x15// parenthesis
  4369.             $name  'tParen';
  4370.             $size  1;
  4371.             $data null;
  4372.             break;
  4373.         case 0x16// missing argument
  4374.             $name 'tMissArg';
  4375.             $size 1;
  4376.             $data '';
  4377.             break;
  4378.         case 0x17// string
  4379.             $name 'tStr';
  4380.             // offset: 1; size: var; Unicode string, 8-bit string length
  4381.             $string $this->_readUnicodeStringShort(substr($formulaData1));
  4382.             $size $string['size'];
  4383.             $data $this->_UTF8toExcelDoubleQuoted($string['value']);
  4384.             break;
  4385.         case 0x19// Special attribute
  4386.             // offset: 1; size: 1; attribute type flags:
  4387.             switch (ord($formulaData[1])) {
  4388.             case 0x01:
  4389.                 $name 'tAttrVolatile';
  4390.                 $size 4;
  4391.                 $data null;
  4392.                 break;
  4393.             case 0x02:
  4394.                 $name 'tAttrIf';
  4395.                 $size 4;
  4396.                 $data null;
  4397.                 break;
  4398.             case 0x04:
  4399.                 $name 'tAttrChoose';
  4400.                 // offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
  4401.                 $nc $this->_GetInt2d($formulaData2);
  4402.                 // offset: 4; size: 2 * $nc
  4403.                 // offset: 4 + 2 * $nc; size: 2
  4404.                 $size $nc 6;
  4405.                 $data null;
  4406.                 break;
  4407.             case 0x08:
  4408.                 $name 'tAttrSkip';
  4409.                 $size 4;
  4410.                 $data null;
  4411.                 break;
  4412.             case 0x10:
  4413.                 $name 'tAttrSum';
  4414.                 $size 4;
  4415.                 $data null;
  4416.                 break;
  4417.             case 0x40:
  4418.             case 0x41:
  4419.                 $name 'tAttrSpace';
  4420.                 $size 4;
  4421.                 // offset: 2; size: 2; space type and position
  4422.                 switch (ord($formulaData[2])) {
  4423.                 case 0x00:
  4424.                     $spacetype 'type0';
  4425.                     break;
  4426.                 case 0x01:
  4427.                     $spacetype 'type1';
  4428.                     break;
  4429.                 case 0x02:
  4430.                     $spacetype 'type2';
  4431.                     break;
  4432.                 case 0x03:
  4433.                     $spacetype 'type3';
  4434.                     break;
  4435.                 case 0x04:
  4436.                     $spacetype 'type4';
  4437.                     break;
  4438.                 case 0x05:
  4439.                     $spacetype 'type5';
  4440.                     break;
  4441.                 default:
  4442.                     throw new Exception('Unrecognized space type in tAttrSpace token');
  4443.                     break;
  4444.                 }
  4445.                 // offset: 3; size: 1; number of inserted spaces/carriage returns
  4446.                 $spacecount ord($formulaData[3]);
  4447.  
  4448.                 $data array('spacetype' => $spacetype'spacecount' => $spacecount);
  4449.                 break;
  4450.             default:
  4451.                 throw new Exception('Unrecognized attribute flag in tAttr token');
  4452.                 break;
  4453.             }
  4454.             break;
  4455.         case 0x1C// error code
  4456.             // offset: 1; size: 1; error code
  4457.             $name 'tErr';
  4458.             $size 2;
  4459.             $data $this->_mapErrorCode(ord($formulaData[1]));
  4460.             break;
  4461.         case 0x1D// boolean
  4462.             // offset: 1; size: 1; 0 = false, 1 = true;
  4463.             $name 'tBool';
  4464.             $size 2;
  4465.             $data ord($formulaData[1]'TRUE' 'FALSE';
  4466.             break;
  4467.         case 0x1E// integer
  4468.             // offset: 1; size: 2; unsigned 16-bit integer
  4469.             $name 'tInt';
  4470.             $size 3;
  4471.             $data $this->_GetInt2d($formulaData1);
  4472.             break;
  4473.         case 0x1F// number
  4474.             // offset: 1; size: 8;
  4475.             $name 'tNum';
  4476.             $size 9;
  4477.             $data $this->_extractNumber(substr($formulaData1));
  4478.             $data str_replace(',''.'(string)$data)// in case non-English locale
  4479.             break;
  4480.         case 0x40// array constant
  4481.         case 0x60// array constant
  4482.             // offset: 1; size: 7; not used
  4483.             $name 'tArray';
  4484.             $size 8;
  4485.             $data null;
  4486.             break;
  4487.         case 0x41// function with fixed number of arguments
  4488.             $name 'tFunc';
  4489.             $size 3;
  4490.             // offset: 1; size: 2; index to built-in sheet function
  4491.             switch ($this->_GetInt2d($formulaData1)) {
  4492.             case   2$function 'ISNA';             $args 1;     break;
  4493.             case   3$function 'ISERROR';         $args 1;     break;
  4494.             case  10$function 'NA';             $args 0;     break;
  4495.             case  15$function 'SIN';             $args 1;     break;
  4496.             case  16$function 'COS';             $args 1;     break;
  4497.             case  17$function 'TAN';             $args 1;     break;
  4498.             case  18$function 'ATAN';             $args 1;     break;
  4499.             case  19$function 'PI';             $args 0;     break;
  4500.             case  20$function 'SQRT';             $args 1;     break;
  4501.             case  21$function 'EXP';             $args 1;     break;
  4502.             case  22$function 'LN';             $args 1;     break;
  4503.             case  23$function 'LOG10';             $args 1;     break;
  4504.             case  24$function 'ABS';             $args 1;     break;
  4505.             case  25$function 'INT';             $args 1;     break;
  4506.             case  26$function 'SIGN';             $args 1;     break;
  4507.             case  27$function 'ROUND';             $args 2;     break;
  4508.             case  30$function 'REPT';             $args 2;     break;
  4509.             case  31$function 'MID';             $args 3;     break;
  4510.             case  32$function 'LEN';             $args 1;     break;
  4511.             case  33$function 'VALUE';             $args 1;     break;
  4512.             case  34$function 'TRUE';             $args 0;     break;
  4513.             case  35$function 'FALSE';             $args 0;     break;
  4514.             case  38$function 'NOT';             $args 1;     break;
  4515.             case  39$function 'MOD';             $args 2;    break;
  4516.             case  40$function 'DCOUNT';         $args 3;    break;
  4517.             case  41$function 'DSUM';             $args 3;    break;
  4518.             case  42$function 'DAVERAGE';         $args 3;    break;
  4519.             case  43$function 'DMIN';             $args 3;    break;
  4520.             case  44$function 'DMAX';             $args 3;    break;
  4521.             case  45$function 'DSTDEV';         $args 3;    break;
  4522.             case  48$function 'TEXT';             $args 2;    break;
  4523.             case  61$function 'MIRR';             $args 3;    break;
  4524.             case  63$function 'RAND';             $args 0;    break;
  4525.             case  65$function 'DATE';             $args 3;    break;
  4526.             case  66$function 'TIME';             $args 3;    break;
  4527.             case  67$function 'DAY';             $args 1;    break;
  4528.             case  68$function 'MONTH';             $args 1;    break;
  4529.             case  69$function 'YEAR';             $args 1;    break;
  4530.             case  71$function 'HOUR';             $args 1;    break;
  4531.             case  72$function 'MINUTE';         $args 1;    break;
  4532.             case  73$function 'SECOND';         $args 1;    break;
  4533.             case  74$function 'NOW';             $args 0;    break;
  4534.             case  75$function 'AREAS';             $args 1;    break;
  4535.             case  76$function 'ROWS';             $args 1;    break;
  4536.             case  77$function 'COLUMNS';         $args 1;    break;
  4537.             case  83$function 'TRANSPOSE';         $args 1;    break;
  4538.             case  86$function 'TYPE';             $args 1;    break;
  4539.             case  97$function 'ATAN2';             $args 2;    break;
  4540.             case  98$function 'ASIN';             $args 1;    break;
  4541.             case  99$function 'ACOS';             $args 1;    break;
  4542.             case 105$function 'ISREF';             $args 1;    break;
  4543.             case 111$function 'CHAR';             $args 1;    break;
  4544.             case 112$function 'LOWER';             $args 1;    break;
  4545.             case 113$function 'UPPER';             $args 1;    break;
  4546.             case 114$function 'PROPER';         $args 1;    break;
  4547.             case 117$function 'EXACT';             $args 2;    break;
  4548.             case 118$function 'TRIM';             $args 1;    break;
  4549.             case 119$function 'REPLACE';         $args 4;    break;
  4550.             case 121$function 'CODE';             $args 1;    break;
  4551.             case 126$function 'ISERR';             $args 1;    break;
  4552.             case 127$function 'ISTEXT';         $args 1;    break;
  4553.             case 128$function 'ISNUMBER';         $args 1;    break;
  4554.             case 129$function 'ISBLANK';         $args 1;    break;
  4555.             case 130$function 'T';                 $args 1;    break;
  4556.             case 131$function 'N';                 $args 1;    break;
  4557.             case 140$function 'DATEVALUE';         $args 1;    break;
  4558.             case 141$function 'TIMEVALUE';         $args 1;    break;
  4559.             case 142$function 'SLN';             $args 3;    break;
  4560.             case 143$function 'SYD';             $args 4;    break;
  4561.             case 162$function 'CLEAN';             $args 1;    break;
  4562.             case 163$function 'MDETERM';         $args 1;    break;
  4563.             case 164$function 'MINVERSE';         $args 1;    break;
  4564.             case 165$function 'MMULT';             $args 2;    break;
  4565.             case 184$function 'FACT';             $args 1;    break;
  4566.             case 189$function 'DPRODUCT';         $args 3;    break;
  4567.             case 190$function 'ISNONTEXT';         $args 1;    break;
  4568.             case 195$function 'DSTDEVP';         $args 3;    break;
  4569.             case 196$function 'DVARP';             $args 3;    break;
  4570.             case 198$function 'ISLOGICAL';         $args 1;    break;
  4571.             case 199$function 'DCOUNTA';         $args 3;    break;
  4572.             case 207$function 'REPLACEB';         $args 4;    break;
  4573.             case 210$function 'MIDB';             $args 3;    break;
  4574.             case 211$function 'LENB';             $args 1;    break;
  4575.             case 212$function 'ROUNDUP';         $args 2;    break;
  4576.             case 213$function 'ROUNDDOWN';         $args 2;    break;
  4577.             case 214$function 'ASC';             $args 1;    break;
  4578.             case 215$function 'DBCS';             $args 1;    break;
  4579.             case 221$function 'TODAY';             $args 0;    break;
  4580.             case 229$function 'SINH';             $args 1;    break;
  4581.             case 230$function 'COSH';             $args 1;    break;
  4582.             case 231$function 'TANH';             $args 1;    break;
  4583.             case 232$function 'ASINH';             $args 1;    break;
  4584.             case 233$function 'ACOSH';             $args 1;    break;
  4585.             case 234$function 'ATANH';             $args 1;    break;
  4586.             case 235$function 'DGET';             $args 3;    break;
  4587.             case 244$function 'INFO';             $args 1;    break;
  4588.             case 252$function 'FREQUENCY';         $args 2;    break;
  4589.             case 261$function 'ERROR.TYPE';     $args 1;    break;
  4590.             case 271$function 'GAMMALN';         $args 1;    break;
  4591.             case 273$function 'BINOMDIST';         $args 4;    break;
  4592.             case 274$function 'CHIDIST';         $args 2;    break;
  4593.             case 275$function 'CHIINV';         $args 2;    break;
  4594.             case 276$function 'COMBIN';         $args 2;    break;
  4595.             case 277$function 'CONFIDENCE';     $args 3;    break;
  4596.             case 278$function 'CRITBINOM';         $args 3;    break;
  4597.             case 279$function 'EVEN';             $args 1;    break;
  4598.             case 280$function 'EXPONDIST';         $args 3;    break;
  4599.             case 281$function 'FDIST';             $args 3;    break;
  4600.             case 282$function 'FINV';             $args 3;    break;
  4601.             case 283$function 'FISHER';         $args 1;    break;
  4602.             case 284$function 'FISHERINV';         $args 1;    break;
  4603.             case 285$function 'FLOOR';             $args 2;    break;
  4604.             case 286$function 'GAMMADIST';         $args 4;    break;
  4605.             case 287$function 'GAMMAINV';         $args 3;    break;
  4606.             case 288$function 'CEILING';         $args 2;    break;
  4607.             case 289$function 'HYPGEOMDIST';    $args 4;    break;
  4608.             case 290$function 'LOGNORMDIST';    $args 3;    break;
  4609.             case 291$function 'LOGINV';            $args 3;    break;
  4610.             case 292$function 'NEGBINOMDIST';    $args 3;    break;
  4611.             case 293$function 'NORMDIST';        $args 4;    break;
  4612.             case 294$function 'NORMSDIST';        $args 1;    break;
  4613.             case 295$function 'NORMINV';        $args 3;    break;
  4614.             case 296$function 'NORMSINV';        $args 1;    break;
  4615.             case 297$function 'STANDARDIZE';    $args 3;    break;
  4616.             case 298$function 'ODD';            $args 1;    break;
  4617.             case 299$function 'PERMUT';            $args 2;    break;
  4618.             case 300$function 'POISSON';        $args 3;    break;
  4619.             case 301$function 'TDIST';            $args 3;    break;
  4620.             case 302$function 'WEIBULL';        $args 4;    break;
  4621.             case 303$function 'SUMXMY2';        $args 2;    break;
  4622.             case 304$function 'SUMX2MY2';        $args 2;    break;
  4623.             case 305$function 'SUMX2PY2';        $args 2;    break;
  4624.             case 306$function 'CHITEST';        $args 2;    break;
  4625.             case 307$function 'CORREL';            $args 2;    break;
  4626.             case 308$function 'COVAR';            $args 2;    break;
  4627.             case 309$function 'FORECAST';        $args 3;    break;
  4628.             case 310$function 'FTEST';            $args 2;    break;
  4629.             case 311$function 'INTERCEPT';        $args 2;    break;
  4630.             case 312$function 'PEARSON';        $args 2;    break;
  4631.             case 313$function 'RSQ';            $args 2;    break;
  4632.             case 314$function 'STEYX';            $args 2;    break;
  4633.             case 315$function 'SLOPE';            $args 2;    break;
  4634.             case 316$function 'TTEST';            $args 4;    break;
  4635.             case 325$function 'LARGE';            $args 2;    break;
  4636.             case 326$function 'SMALL';            $args 2;    break;
  4637.             case 327$function 'QUARTILE';        $args 2;    break;
  4638.             case 328$function 'PERCENTILE';        $args 2;    break;
  4639.             case 331$function 'TRIMMEAN';        $args 2;    break;
  4640.             case 332$function 'TINV';            $args 2;    break;
  4641.             case 337$function 'POWER';            $args 2;    break;
  4642.             case 342$function 'RADIANS';        $args 1;    break;
  4643.             case 343$function 'DEGREES';        $args 1;    break;
  4644.             case 346$function 'COUNTIF';        $args 2;    break;
  4645.             case 347$function 'COUNTBLANK';        $args 1;    break;
  4646.             case 350$function 'ISPMT';            $args 4;    break;
  4647.             case 351$function 'DATEDIF';        $args 3;    break;
  4648.             case 352$function 'DATESTRING';        $args 1;    break;
  4649.             case 353$function 'NUMBERSTRING';    $args 2;    break;
  4650.             case 360$function 'PHONETIC';        $args 1;    break;
  4651.             default:
  4652.                 throw new Exception('Unrecognized function in formula');
  4653.                 break;
  4654.             }
  4655.             $data array('function' => $function'args' => $args);
  4656.             break;
  4657.         case 0x22// function with variable number of arguments
  4658.         case 0x42// function with variable number of arguments
  4659.         case 0x62// function with variable number of arguments
  4660.             $name 'tFuncV';
  4661.             $size 4;
  4662.             // offset: 1; size: 1; number of arguments
  4663.             $args ord($formulaData[1]);
  4664.             // offset: 2: size: 2; index to built-in sheet function
  4665.             switch ($this->_GetInt2d($formulaData2)) {
  4666.             case   0$function 'COUNT';            break;
  4667.             case   1$function 'IF';                break;
  4668.             case   4$function 'SUM';            break;
  4669.             case   5$function 'AVERAGE';        break;
  4670.             case   6$function 'MIN';            break;
  4671.             case   7$function 'MAX';            break;
  4672.             case   8$function 'ROW';            break;
  4673.             case   9$function 'COLUMN';            break;
  4674.             case  11$function 'NPV';            break;
  4675.             case  12$function 'STDEV';            break;
  4676.             case  13$function 'DOLLAR';            break;
  4677.             case  14$function 'FIXED';            break;
  4678.             case  28$function 'LOOKUP';            break;
  4679.             case  29$function 'INDEX';            break;
  4680.             case  36$function 'AND';            break;
  4681.             case  37$function 'OR';                break;
  4682.             case  46$function 'VAR';            break;
  4683.             case  49$function 'LINEST';            break;
  4684.             case  50$function 'TREND';            break;
  4685.             case  51$function 'LOGEST';            break;
  4686.             case  52$function 'GROWTH';            break;
  4687.             case  56$function 'PV';                break;
  4688.             case  57$function 'FV';                break;
  4689.             case  58$function 'NPER';            break;
  4690.             case  59$function 'PMT';            break;
  4691.             case  60$function 'RATE';            break;
  4692.             case  62$function 'IRR';            break;
  4693.             case  64$function 'MATCH';            break;
  4694.             case  70$function 'WEEKDAY';        break;
  4695.             case  78$function 'OFFSET';            break;
  4696.             case  82$function 'SEARCH';            break;
  4697.             case 100$function 'CHOOSE';            break;
  4698.             case 101$function 'HLOOKUP';        break;
  4699.             case 102$function 'VLOOKUP';        break;
  4700.             case 109$function 'LOG';            break;
  4701.             case 115$function 'LEFT';            break;
  4702.             case 116$function 'RIGHT';            break;
  4703.             case 120$function 'SUBSTITUTE';        break;
  4704.             case 124$function 'FIND';            break;
  4705.             case 125$function 'CELL';            break;
  4706.             case 144$function 'DDB';            break;
  4707.             case 148$function 'INDIRECT';        break;
  4708.             case 167$function 'IPMT';            break;
  4709.             case 168$function 'PPMT';            break;
  4710.             case 169$function 'COUNTA';            break;
  4711.             case 183$function 'PRODUCT';        break;
  4712.             case 193$function 'STDEVP';            break;
  4713.             case 194$function 'VARP';            break;
  4714.             case 197$function 'TRUNC';            break;
  4715.             case 204$function 'USDOLLAR';        break;
  4716.             case 205$function 'FINDB';            break;
  4717.             case 206$function 'SEARCHB';        break;
  4718.             case 208$function 'LEFTB';            break;
  4719.             case 209$function 'RIGHTB';            break;
  4720.             case 216$function 'RANK';            break;
  4721.             case 219$function 'ADDRESS';        break;
  4722.             case 220$function 'DAYS360';        break;
  4723.             case 222$function 'VDB';            break;
  4724.             case 227$function 'MEDIAN';            break;
  4725.             case 228$function 'SUMPRODUCT';        break;
  4726.             case 247$function 'DB';                break;
  4727.             case 269$function 'AVEDEV';            break;
  4728.             case 270$function 'BETADIST';        break;
  4729.             case 272$function 'BETAINV';        break;
  4730.             case 317$function 'PROB';            break;
  4731.             case 318$function 'DEVSQ';            break;
  4732.             case 319$function 'GEOMEAN';        break;
  4733.             case 320$function 'HARMEAN';        break;
  4734.             case 321$function 'SUMSQ';            break;
  4735.             case 322$function 'KURT';            break;
  4736.             case 323$function 'SKEW';            break;
  4737.             case 324$function 'ZTEST';            break;
  4738.             case 329$function 'PERCENTRANK';    break;
  4739.             case 330$function 'MODE';            break;
  4740.             case 336$function 'CONCATENATE';    break;
  4741.             case 344$function 'SUBTOTAL';        break;
  4742.             case 345$function 'SUMIF';            break;
  4743.             case 354$function 'ROMAN';            break;
  4744.             case 358$function 'GETPIVOTDATA';    break;
  4745.             case 359$function 'HYPERLINK';        break;
  4746.             case 361$function 'AVERAGEA';        break;
  4747.             case 362$function 'MAXA';            break;
  4748.             case 363$function 'MINA';            break;
  4749.             case 364$function 'STDEVPA';        break;
  4750.             case 365$function 'VARPA';            break;
  4751.             case 366$function 'STDEVA';            break;
  4752.             case 367$function 'VARA';            break;
  4753.             default:
  4754.                 throw new Exception('Unrecognized function in formula');
  4755.                 break;
  4756.             }
  4757.             $data array('function' => $function'args' => $args);
  4758.             break;
  4759.         case 0x23// index to defined name
  4760.         case 0x43:
  4761.             $name 'tName';
  4762.             $size 5;
  4763.             // offset: 1; size: 2; one-based index to definedname record
  4764.             $definedNameIndex $this->_GetInt2d($formulaData11;
  4765.             // offset: 2; size: 2; not used
  4766.             $data $this->_definedname[$definedNameIndex]['name'];
  4767.             break;
  4768.         case 0x24// single cell reference e.g. A5
  4769.         case 0x44:
  4770.         case 0x64:
  4771.             $name 'tRef';
  4772.             $size 5;
  4773.             $data $this->_readBIFF8CellAddress(substr($formulaData14));
  4774.             break;
  4775.         case 0x25// cell range reference to cells in the same sheet
  4776.         case 0x45:
  4777.         case 0x65:
  4778.             $name 'tArea';
  4779.             $size 9;
  4780.             $data $this->_readBIFF8CellRangeAddress(substr($formulaData18));
  4781.             break;
  4782.         case 0x26:
  4783.         case 0x46:
  4784.             $name 'tMemArea';
  4785.             // offset: 1; size: 4; not used
  4786.             // offset: 5; size: 2; size of the following subexpression
  4787.             $subSize $this->_GetInt2d($formulaData5);
  4788.             $size $subSize;
  4789.             $data $this->_getFormulaFromData(substr($formulaData7$subSize));
  4790.             break;
  4791.         case 0x47:
  4792.             $name 'tMemErr';
  4793.             // offset: 1; size: 4; not used
  4794.             // offset: 5; size: 2; size of the following subexpression
  4795.             $subSize $this->_GetInt2d($formulaData5);
  4796.             $size $subSize;
  4797.             $data $this->_getFormulaFromData(substr($formulaData7$subSize));
  4798.             break;
  4799.         case 0x29:
  4800.         case 0x49:
  4801.             $name 'tMemFunc';
  4802.             // offset: 1; size: 2; size of the following subexpression
  4803.             $subSize $this->_GetInt2d($formulaData1);
  4804.             $size $subSize;
  4805.             $data $this->_getFormulaFromData(substr($formulaData3$subSize));
  4806.             break;
  4807.             
  4808.         case 0x2C// Relative reference, used in shared formulas and some other places
  4809.         case 0x4C:
  4810.         case 0x6C:
  4811.             $name 'tRefN';
  4812.             $size 5;
  4813.             $data $this->_readBIFF8CellAddressB(substr($formulaData14)$baseCell);
  4814.             break;
  4815.         
  4816.         case 0x2D:
  4817.         case 0x4D:
  4818.         case 0x6D:
  4819.             $name 'tAreaN';
  4820.             $size 9;
  4821.             $data $this->_readBIFF8CellRangeAddressB(substr($formulaData18)$baseCell);
  4822.             break;
  4823.  
  4824.         case 0x3A// 3d reference to cell
  4825.         case 0x5A:
  4826.             $name 'tRef3d';
  4827.             $size 7;
  4828.             // offset: 1; size: 2; index to REF entry
  4829.             $sheetRange $this->_readSheetRangeByRefIndex($this->_GetInt2d($formulaData1));
  4830.             // offset: 3; size: 4; cell address
  4831.             $cellAddress $this->_readBIFF8CellAddress(substr($formulaData34));
  4832.  
  4833.             $data "$sheetRange!$cellAddress";
  4834.  
  4835.             break;
  4836.         case 0x3B// 3d reference to cell range
  4837.         case 0x5B:
  4838.             $name 'tArea3d';
  4839.             $size 11;
  4840.             // offset: 1; size: 2; index to REF entry
  4841.             $sheetRange $this->_readSheetRangeByRefIndex($this->_GetInt2d($formulaData1));
  4842.             // offset: 3; size: 8; cell address
  4843.             $cellRangeAddress $this->_readBIFF8CellRangeAddress(substr($formulaData38));
  4844.  
  4845.             $data "$sheetRange!$cellRangeAddress";
  4846.  
  4847.             break;
  4848.         // case 0x39: // don't know how to deal with
  4849.         default:
  4850.             throw new Exception('Unrecognized token ' sprintf('%02X'$id' in formula');
  4851.             break;
  4852.         }
  4853.  
  4854.         return array(
  4855.             'id' => $id,
  4856.             'name' => $name,
  4857.             'size' => $size,
  4858.             'data' => $data,
  4859.         );
  4860.     }
  4861.  
  4862.     /**
  4863.      * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
  4864.      * section 3.3.4
  4865.      *
  4866.      * @param string $cellAddressStructure 
  4867.      * @return string 
  4868.      */
  4869.     private function _readBIFF8CellAddress($cellAddressStructure)
  4870.     {
  4871.         // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  4872.             $row $this->_GetInt2d($cellAddressStructure01;
  4873.  
  4874.         // offset: 2; size: 2; index to column or column offset + relative flags
  4875.  
  4876.             // bit: 7-0; mask 0x00FF; column index
  4877.             $column PHPExcel_Cell::stringFromColumnIndex(0x00FF $this->_GetInt2d($cellAddressStructure2));
  4878.  
  4879.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4880.             if (!(0x4000 $this->_GetInt2d($cellAddressStructure2))) {
  4881.                 $column '$' $column;
  4882.             }
  4883.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4884.             if (!(0x8000 $this->_GetInt2d($cellAddressStructure2))) {
  4885.                 $row '$' $row;
  4886.             }
  4887.  
  4888.         return $column $row;
  4889.     }
  4890.  
  4891.     /**
  4892.      * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  4893.      * to indicate offsets from a base cell
  4894.      * section 3.3.4
  4895.      *
  4896.      * @param string $cellAddressStructure 
  4897.      * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4898.      * @return string 
  4899.      */
  4900.     private function _readBIFF8CellAddressB($cellAddressStructure$baseCell 'A1')
  4901.     {
  4902.         list($baseCol$baseRowPHPExcel_Cell::coordinateFromString($baseCell);
  4903.         $baseCol PHPExcel_Cell::columnIndexFromString($baseCol1;
  4904.         
  4905.         // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  4906.             $rowIndex $this->_GetInt2d($cellAddressStructure0);
  4907.             $row $this->_GetInt2d($cellAddressStructure01;
  4908.  
  4909.         // offset: 2; size: 2; index to column or column offset + relative flags
  4910.  
  4911.             // bit: 7-0; mask 0x00FF; column index
  4912.             $colIndex 0x00FF $this->_GetInt2d($cellAddressStructure2);
  4913.  
  4914.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4915.             if (!(0x4000 $this->_GetInt2d($cellAddressStructure2))) {
  4916.                 $column PHPExcel_Cell::stringFromColumnIndex($colIndex);
  4917.                 $column '$' $column;
  4918.             else {
  4919.                 $colIndex ($colIndex <= 127$colIndex $colIndex 256;
  4920.                 $column PHPExcel_Cell::stringFromColumnIndex($baseCol $colIndex);
  4921.             }
  4922.  
  4923.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4924.             if (!(0x8000 $this->_GetInt2d($cellAddressStructure2))) {
  4925.                 $row '$' $row;
  4926.             else {
  4927.                 $rowIndex ($rowIndex <= 32767$rowIndex $rowIndex 65536;
  4928.                 $row $baseRow $rowIndex;
  4929.             }
  4930.  
  4931.         return $column $row;
  4932.     }
  4933.  
  4934.     /**
  4935.      * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
  4936.      * always fixed range
  4937.      * section 2.5.14
  4938.      *
  4939.      * @param string $subData 
  4940.      * @return string 
  4941.      * @throws Exception
  4942.      */
  4943.     private function _readBIFF5CellRangeAddressFixed($subData)
  4944.     {
  4945.         // offset: 0; size: 2; index to first row
  4946.         $fr $this->_GetInt2d($subData01;
  4947.  
  4948.         // offset: 2; size: 2; index to last row
  4949.         $lr $this->_GetInt2d($subData21;
  4950.  
  4951.         // offset: 4; size: 1; index to first column
  4952.         $fc ord($subData{4});
  4953.  
  4954.         // offset: 5; size: 1; index to last column
  4955.         $lc ord($subData{5});
  4956.  
  4957.         // check values
  4958.         if ($fr $lr || $fc $lc{
  4959.             throw new Exception('Not a cell range address');
  4960.         }
  4961.  
  4962.         // column index to letter
  4963.         $fc PHPExcel_Cell::stringFromColumnIndex($fc);
  4964.         $lc PHPExcel_Cell::stringFromColumnIndex($lc);
  4965.  
  4966.         if ($fr == $lr and $fc == $lc{
  4967.             return "$fc$fr";
  4968.         }
  4969.         return "$fc$fr:$lc$lr";
  4970.     }
  4971.  
  4972.     /**
  4973.      * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
  4974.      * always fixed range
  4975.      * section 2.5.14
  4976.      *
  4977.      * @param string $subData 
  4978.      * @return string 
  4979.      * @throws Exception
  4980.      */
  4981.     private function _readBIFF8CellRangeAddressFixed($subData)
  4982.     {
  4983.         // offset: 0; size: 2; index to first row
  4984.         $fr $this->_GetInt2d($subData01;
  4985.  
  4986.         // offset: 2; size: 2; index to last row
  4987.         $lr $this->_GetInt2d($subData21;
  4988.  
  4989.         // offset: 4; size: 2; index to first column
  4990.         $fc $this->_GetInt2d($subData4);
  4991.  
  4992.         // offset: 6; size: 2; index to last column
  4993.         $lc $this->_GetInt2d($subData6);
  4994.  
  4995.         // check values
  4996.         if ($fr $lr || $fc $lc{
  4997.             throw new Exception('Not a cell range address');
  4998.         }
  4999.  
  5000.         // column index to letter
  5001.         $fc PHPExcel_Cell::stringFromColumnIndex($fc);
  5002.         $lc PHPExcel_Cell::stringFromColumnIndex($lc);
  5003.  
  5004.         if ($fr == $lr and $fc == $lc{
  5005.             return "$fc$fr";
  5006.         }
  5007.         return "$fc$fr:$lc$lr";
  5008.     }
  5009.  
  5010.     /**
  5011.      * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
  5012.      * there are flags indicating whether column/row index is relative
  5013.      * section 3.3.4
  5014.      *
  5015.      * @param string $subData 
  5016.      * @return string 
  5017.      */
  5018.     private function _readBIFF8CellRangeAddress($subData)
  5019.     {
  5020.         // todo: if cell range is just a single cell, should this funciton
  5021.         // not just return e.g. 'A1' and not 'A1:A1' ?
  5022.  
  5023.         // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
  5024.             $fr $this->_GetInt2d($subData01;
  5025.  
  5026.         // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
  5027.             $lr $this->_GetInt2d($subData21;
  5028.  
  5029.         // offset: 4; size: 2; index to first column or column offset + relative flags
  5030.  
  5031.             // bit: 7-0; mask 0x00FF; column index
  5032.             $fc PHPExcel_Cell::stringFromColumnIndex(0x00FF $this->_GetInt2d($subData4));
  5033.  
  5034.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5035.             if (!(0x4000 $this->_GetInt2d($subData4))) {
  5036.                 $fc '$' $fc;
  5037.             }
  5038.  
  5039.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5040.             if (!(0x8000 $this->_GetInt2d($subData4))) {
  5041.                 $fr '$' $fr;
  5042.             }
  5043.  
  5044.         // offset: 6; size: 2; index to last column or column offset + relative flags
  5045.  
  5046.             // bit: 7-0; mask 0x00FF; column index
  5047.             $lc PHPExcel_Cell::stringFromColumnIndex(0x00FF $this->_GetInt2d($subData6));
  5048.  
  5049.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5050.             if (!(0x4000 $this->_GetInt2d($subData6))) {
  5051.                 $lc '$' $lc;
  5052.             }
  5053.  
  5054.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5055.             if (!(0x8000 $this->_GetInt2d($subData6))) {
  5056.                 $lr '$' $lr;
  5057.             }
  5058.  
  5059.         return "$fc$fr:$lc$lr";
  5060.     }
  5061.  
  5062.     /**
  5063.      * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  5064.      * to indicate offsets from a base cell
  5065.      * section 3.3.4
  5066.      *
  5067.      * @param string $subData 
  5068.      * @param string $baseCell Base cell
  5069.      * @return string Cell range address
  5070.      */
  5071.     private function _readBIFF8CellRangeAddressB($subData$baseCell 'A1')
  5072.     {
  5073.         list($baseCol$baseRowPHPExcel_Cell::coordinateFromString($baseCell);
  5074.         $baseCol PHPExcel_Cell::columnIndexFromString($baseCol1;
  5075.  
  5076.         // TODO: if cell range is just a single cell, should this funciton
  5077.         // not just return e.g. 'A1' and not 'A1:A1' ?
  5078.  
  5079.         // offset: 0; size: 2; first row
  5080.         $frIndex $this->_GetInt2d($subData0)// adjust below
  5081.  
  5082.         // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
  5083.         $lrIndex $this->_GetInt2d($subData2)// adjust below
  5084.  
  5085.         // offset: 4; size: 2; first column with relative/absolute flags
  5086.  
  5087.             // bit: 7-0; mask 0x00FF; column index
  5088.             $fcIndex 0x00FF $this->_GetInt2d($subData4);
  5089.  
  5090.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5091.             if (!(0x4000 $this->_GetInt2d($subData4))) {
  5092.                 // absolute column index
  5093.                 $fc PHPExcel_Cell::stringFromColumnIndex($fcIndex);
  5094.                 $fc '$' $fc;
  5095.             else {
  5096.                 // column offset
  5097.                 $fcIndex ($fcIndex <= 127$fcIndex $fcIndex 256;
  5098.                 $fc PHPExcel_Cell::stringFromColumnIndex($baseCol $fcIndex);
  5099.             }
  5100.  
  5101.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5102.             if (!(0x8000 $this->_GetInt2d($subData4))) {
  5103.                 // absolute row index
  5104.                 $fr $frIndex 1;
  5105.                 $fr '$' $fr;
  5106.             else {
  5107.                 // row offset
  5108.                 $frIndex ($frIndex <= 32767$frIndex $frIndex 65536;
  5109.                 $fr $baseRow $frIndex;
  5110.             }
  5111.  
  5112.         // offset: 6; size: 2; last column with relative/absolute flags
  5113.  
  5114.             // bit: 7-0; mask 0x00FF; column index
  5115.             $lcIndex 0x00FF $this->_GetInt2d($subData6);
  5116.             $lcIndex ($lcIndex <= 127$lcIndex $lcIndex 256;
  5117.             $lc PHPExcel_Cell::stringFromColumnIndex($baseCol $lcIndex);
  5118.  
  5119.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5120.             if (!(0x4000 $this->_GetInt2d($subData6))) {
  5121.                 // absolute column index
  5122.                 $lc PHPExcel_Cell::stringFromColumnIndex($lcIndex);
  5123.                 $lc '$' $lc;
  5124.             else {
  5125.                 // column offset
  5126.                 $lcIndex ($lcIndex <= 127$lcIndex $lcIndex 256;
  5127.                 $lc PHPExcel_Cell::stringFromColumnIndex($baseCol $lcIndex);
  5128.             }
  5129.  
  5130.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5131.             if (!(0x8000 $this->_GetInt2d($subData6))) {
  5132.                 // absolute row index
  5133.                 $lr $lrIndex 1;
  5134.                 $lr '$' $lr;
  5135.             else {
  5136.                 // row offset
  5137.                 $lrIndex ($lrIndex <= 32767$lrIndex $lrIndex 65536;
  5138.                 $lr $baseRow $lrIndex;
  5139.             }
  5140.  
  5141.         return "$fc$fr:$lc$lr";
  5142.     }
  5143.  
  5144.     /**
  5145.      * Read BIFF8 cell range address list
  5146.      * section 2.5.15
  5147.      *
  5148.      * @param string $subData 
  5149.      * @return array 
  5150.      */
  5151.     private function _readBIFF8CellRangeAddressList($subData)
  5152.     {
  5153.         $cellRangeAddresses array();
  5154.  
  5155.         // offset: 0; size: 2; number of the following cell range addresses
  5156.         $nm $this->_GetInt2d($subData0);
  5157.  
  5158.         $offset 2;
  5159.         // offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
  5160.         for ($i 0$i $nm++$i{
  5161.             $cellRangeAddresses[$this->_readBIFF8CellRangeAddressFixed(substr($subData$offset8));
  5162.             $offset += 8;
  5163.         }
  5164.  
  5165.         return array(
  5166.             'size' => $nm,
  5167.             'cellRangeAddresses' => $cellRangeAddresses,
  5168.         );
  5169.     }
  5170.  
  5171.     /**
  5172.      * Read BIFF5 cell range address list
  5173.      * section 2.5.15
  5174.      *
  5175.      * @param string $subData 
  5176.      * @return array 
  5177.      */
  5178.     private function _readBIFF5CellRangeAddressList($subData)
  5179.     {
  5180.         $cellRangeAddresses array();
  5181.  
  5182.         // offset: 0; size: 2; number of the following cell range addresses
  5183.         $nm $this->_GetInt2d($subData0);
  5184.  
  5185.         $offset 2;
  5186.         // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
  5187.         for ($i 0$i $nm++$i{
  5188.             $cellRangeAddresses[$this->_readBIFF5CellRangeAddressFixed(substr($subData$offset6));
  5189.             $offset += 6;
  5190.         }
  5191.  
  5192.         return array(
  5193.             'size' => $nm,
  5194.             'cellRangeAddresses' => $cellRangeAddresses,
  5195.         );
  5196.     }
  5197.  
  5198.     /**
  5199.      * Get a sheet range like Sheet1:Sheet3 from REF index
  5200.      * Note: If there is only one sheet in the range, one gets e.g Sheet1
  5201.      * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
  5202.      * in which case an exception is thrown
  5203.      *
  5204.      * @param int $index 
  5205.      * @return string|false
  5206.      * @throws Exception
  5207.      */
  5208.     private function _readSheetRangeByRefIndex($index)
  5209.     {
  5210.         if (isset($this->_ref[$index])) {
  5211.  
  5212.             $type $this->_externalBooks[$this->_ref[$index]['externalBookIndex']]['type'];
  5213.             
  5214.             switch ($type{
  5215.                 case 'internal':
  5216.                     // check if we have a deleted 3d reference
  5217.                     if ($this->_ref[$index]['firstSheetIndex'== 0xFFFF or $this->_ref[$index]['lastSheetIndex'== 0xFFFF{
  5218.                         throw new Exception('Deleted sheet reference');
  5219.                     }
  5220.  
  5221.                     // we have normal sheet range (collapsed or uncollapsed)
  5222.                     $firstSheetName $this->_sheets[$this->_ref[$index]['firstSheetIndex']]['name'];
  5223.                     $lastSheetName $this->_sheets[$this->_ref[$index]['lastSheetIndex']]['name'];
  5224.  
  5225.                     if ($firstSheetName == $lastSheetName{
  5226.                         // collapsed sheet range
  5227.                         $sheetRange $firstSheetName;
  5228.                     else {
  5229.                         $sheetRange "$firstSheetName:$lastSheetName";
  5230.                     }
  5231.  
  5232.                     // escape the single-quotes
  5233.                     $sheetRange str_replace("'""''"$sheetRange);
  5234.  
  5235.                     // if there are special characters, we need to enclose the range in single-quotes
  5236.                     // todo: check if we have identified the whole set of special characters
  5237.                     // it seems that the following characters are not accepted for sheet names
  5238.                     // and we may assume that they are not present: []*/:\?
  5239.                     if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/"$sheetRange)) {
  5240.                         $sheetRange "'$sheetRange'";
  5241.                     }
  5242.  
  5243.                     return $sheetRange;
  5244.                     break;
  5245.  
  5246.                 default:
  5247.                     // TODO: external sheet support
  5248.                     throw new Exception('Excel5 reader only supports internal sheets in fomulas');
  5249.                     break;
  5250.             }
  5251.         }
  5252.         return false;
  5253.     }
  5254.  
  5255.     /**
  5256.      * read BIFF8 constant value array from array data
  5257.      * returns e.g. array('value' => '{1,2;3,4}', 'size' => 40}
  5258.      * section 2.5.8
  5259.      *
  5260.      * @param string $arrayData 
  5261.      * @return array 
  5262.      */
  5263.     private function _readBIFF8ConstantArray($arrayData)
  5264.     {
  5265.         // offset: 0; size: 1; number of columns decreased by 1
  5266.         $nc ord($arrayData[0]);
  5267.  
  5268.         // offset: 1; size: 2; number of rows decreased by 1
  5269.         $nr $this->_GetInt2d($arrayData1);
  5270.         $size 3// initialize
  5271.         $arrayData substr($arrayData3);
  5272.  
  5273.         // offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
  5274.         $matrixChunks array();
  5275.         for ($r 1$r <= $nr 1++$r{
  5276.             $items array();
  5277.             for ($c 1$c <= $nc 1++$c{
  5278.                 $constant $this->_readBIFF8Constant($arrayData);
  5279.                 $items[$constant['value'];
  5280.                 $arrayData substr($arrayData$constant['size']);
  5281.                 $size += $constant['size'];
  5282.             }
  5283.             $matrixChunks[implode(','$items)// looks like e.g. '1,"hello"'
  5284.         }
  5285.         $matrix '{' implode(';'$matrixChunks'}';
  5286.  
  5287.         return array(
  5288.             'value' => $matrix,
  5289.             'size' => $size,
  5290.         );
  5291.     }
  5292.  
  5293.     /**
  5294.      * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
  5295.      * section 2.5.7
  5296.      * returns e.g. array('value' => '5', 'size' => 9)
  5297.      *
  5298.      * @param string $valueData 
  5299.      * @return array 
  5300.      */
  5301.     private function _readBIFF8Constant($valueData)
  5302.     {
  5303.         // offset: 0; size: 1; identifier for type of constant
  5304.         $identifier ord($valueData[0]);
  5305.  
  5306.         switch ($identifier{
  5307.         case 0x00// empty constant (what is this?)
  5308.             $value '';
  5309.             $size 9;
  5310.             break;
  5311.         case 0x01// number
  5312.             // offset: 1; size: 8; IEEE 754 floating-point value
  5313.             $value $this->_extractNumber(substr($valueData18));
  5314.             $size 9;
  5315.             break;
  5316.         case 0x02// string value
  5317.             // offset: 1; size: var; Unicode string, 16-bit string length
  5318.             $string $this->_readUnicodeStringLong(substr($valueData1));
  5319.             $value '"' $string['value''"';
  5320.             $size $string['size'];
  5321.             break;
  5322.         case 0x04// boolean
  5323.             // offset: 1; size: 1; 0 = FALSE, 1 = TRUE
  5324.             if (ord($valueData[1])) {
  5325.                 $value 'TRUE';
  5326.             else {
  5327.                 $value 'FALSE';
  5328.             }
  5329.             $size 9;
  5330.             break;
  5331.         case 0x10// error code
  5332.             // offset: 1; size: 1; error code
  5333.             $value $this->_mapErrorCode(ord($valueData[1]));
  5334.             $size 9;
  5335.             break;
  5336.         }
  5337.         return array(
  5338.             'value' => $value,
  5339.             'size' => $size,
  5340.         );
  5341.     }
  5342.  
  5343.     /**
  5344.      * Extract RGB color
  5345.      * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4
  5346.      *
  5347.      * @param string $rgb Encoded RGB value (4 bytes)
  5348.      * @return array 
  5349.      */
  5350.     private function _readRGB($rgb)
  5351.     {
  5352.         // offset: 0; size 1; Red component
  5353.         $r ord($rgb{0});
  5354.  
  5355.         // offset: 1; size: 1; Green component
  5356.         $g ord($rgb{1});
  5357.  
  5358.         // offset: 2; size: 1; Blue component
  5359.         $b ord($rgb{2});
  5360.  
  5361.         // HEX notation, e.g. 'FF00FC'
  5362.         $rgb sprintf('%02X'$rsprintf('%02X'$gsprintf('%02X'$b);
  5363.  
  5364.         return array('rgb' => $rgb);
  5365.     }
  5366.  
  5367.     /**
  5368.      * Read byte string (8-bit string length)
  5369.      * OpenOffice documentation: 2.5.2
  5370.      *
  5371.      * @param string $subData 
  5372.      * @return array 
  5373.      */
  5374.     private function _readByteStringShort($subData)
  5375.     {
  5376.         // offset: 0; size: 1; length of the string (character count)
  5377.         $ln ord($subData[0]);
  5378.  
  5379.         // offset: 1: size: var; character array (8-bit characters)
  5380.         $value $this->_decodeCodepage(substr($subData1$ln));
  5381.  
  5382.         return array(
  5383.             'value' => $value,
  5384.             'size' => $ln// size in bytes of data structure
  5385.         );
  5386.     }
  5387.  
  5388.     /**
  5389.      * Read byte string (16-bit string length)
  5390.      * OpenOffice documentation: 2.5.2
  5391.      *
  5392.      * @param string $subData 
  5393.      * @return array 
  5394.      */
  5395.     private function _readByteStringLong($subData)
  5396.     {
  5397.         // offset: 0; size: 2; length of the string (character count)
  5398.         $ln $this->_GetInt2d($subData0);
  5399.  
  5400.         // offset: 2: size: var; character array (8-bit characters)
  5401.         $value $this->_decodeCodepage(substr($subData2));
  5402.  
  5403.         //return $string;
  5404.         return array(
  5405.             'value' => $value,
  5406.             'size' => $ln// size in bytes of data structure
  5407.         );
  5408.     }
  5409.  
  5410.     /**
  5411.      * Extracts an Excel Unicode short string (8-bit string length)
  5412.      * OpenOffice documentation: 2.5.3
  5413.      * function will automatically find out where the Unicode string ends.
  5414.      *
  5415.      * @param string $subData 
  5416.      * @return array 
  5417.      */
  5418.     private function _readUnicodeStringShort($subData)
  5419.     {
  5420.         $value '';
  5421.  
  5422.         // offset: 0: size: 1; length of the string (character count)
  5423.         $characterCount ord($subData[0]);
  5424.  
  5425.         $string $this->_readUnicodeString(substr($subData1)$characterCount);
  5426.  
  5427.         // add 1 for the string length
  5428.         $string['size'+= 1;
  5429.  
  5430.         return $string;
  5431.     }
  5432.  
  5433.     /**
  5434.      * Extracts an Excel Unicode long string (16-bit string length)
  5435.      * OpenOffice documentation: 2.5.3
  5436.      * this function is under construction, needs to support rich text, and Asian phonetic settings
  5437.      *
  5438.      * @param string $subData 
  5439.      * @return array 
  5440.      */
  5441.     private function _readUnicodeStringLong($subData)
  5442.     {
  5443.         $value '';
  5444.  
  5445.         // offset: 0: size: 2; length of the string (character count)
  5446.         $characterCount $this->_GetInt2d($subData0);
  5447.  
  5448.         $string $this->_readUnicodeString(substr($subData2)$characterCount);
  5449.  
  5450.         // add 2 for the string length
  5451.         $string['size'+= 2;
  5452.  
  5453.         return $string;
  5454.     }
  5455.  
  5456.     /**
  5457.      * Read Unicode string with no string length field, but with known character count
  5458.      * this function is under construction, needs to support rich text, and Asian phonetic settings
  5459.      * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
  5460.      *
  5461.      * @param string $subData 
  5462.      * @param int $characterCount 
  5463.      * @return array 
  5464.      */
  5465.     private function _readUnicodeString($subData$characterCount)
  5466.     {
  5467.         $value '';
  5468.  
  5469.         // offset: 0: size: 1; option flags
  5470.  
  5471.             // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  5472.             $isCompressed !((0x01 ord($subData[0])) >> 0);
  5473.  
  5474.             // bit: 2; mask: 0x04; Asian phonetic settings
  5475.             $hasAsian (0x04ord($subData[0]>> 2;
  5476.  
  5477.             // bit: 3; mask: 0x08; Rich-Text settings
  5478.             $hasRichText (0x08ord($subData[0]>> 3;
  5479.  
  5480.         // offset: 1: size: var; character array
  5481.         // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  5482.         // needs to be fixed
  5483.         $value $this->_encodeUTF16(substr($subData1$isCompressed $characterCount $characterCount)$isCompressed);
  5484.  
  5485.         return array(
  5486.             'value' => $value,
  5487.             'size' => $isCompressed $characterCount $characterCount// the size in bytes including the option flags
  5488.         );
  5489.     }
  5490.  
  5491.     /**
  5492.      * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
  5493.      * Example:  hello"world  -->  "hello""world"
  5494.      *
  5495.      * @param string $value UTF-8 encoded string
  5496.      * @return string 
  5497.      */
  5498.     private function _UTF8toExcelDoubleQuoted($value)
  5499.     {
  5500.         return '"' str_replace('"''""'$value'"';
  5501.     }
  5502.  
  5503.     /**
  5504.      * Reads first 8 bytes of a string and return IEEE 754 float
  5505.      *
  5506.      * @param string $data Binary string that is at least 8 bytes long
  5507.      * @return float 
  5508.      */
  5509.     private function _extractNumber($data)
  5510.     {
  5511.         $rknumhigh $this->_GetInt4d($data4);
  5512.         $rknumlow $this->_GetInt4d($data0);
  5513.         $sign ($rknumhigh 0x80000000>> 31;
  5514.         $exp ($rknumhigh 0x7ff00000>> 20;
  5515.         $mantissa (0x100000 ($rknumhigh 0x000fffff));
  5516.         $mantissalow1 ($rknumlow 0x80000000>> 31;
  5517.         $mantissalow2 ($rknumlow 0x7fffffff);
  5518.         $value $mantissa pow(20 ($exp 1023)));
  5519.  
  5520.         if ($mantissalow1 != 0{
  5521.             $value += pow ((21 ($exp 1023)));
  5522.         }
  5523.  
  5524.         $value += $mantissalow2 pow ((52 ($exp 1023)));
  5525.         if ($sign{
  5526.             $value = -$value;
  5527.         }
  5528.  
  5529.         return $value;
  5530.     }
  5531.  
  5532.     private function _GetIEEE754($rknum)
  5533.     {
  5534.         if (($rknum 0x02!= 0{
  5535.             $value $rknum >> 2;
  5536.         }
  5537.         else {
  5538.             // changes by mmp, info on IEEE754 encoding from
  5539.             // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  5540.             // The RK format calls for using only the most significant 30 bits
  5541.             // of the 64 bit floating point value. The other 34 bits are assumed
  5542.             // to be 0 so we use the upper 30 bits of $rknum as follows...
  5543.             $sign ($rknum 0x80000000>> 31;
  5544.             $exp ($rknum 0x7ff00000>> 20;
  5545.             $mantissa (0x100000 ($rknum 0x000ffffc));
  5546.             $value $mantissa pow(20($exp 1023)));
  5547.             if ($sign{
  5548.                 $value = -$value;
  5549.             }
  5550.             //end of changes by mmp
  5551.         }
  5552.         if (($rknum 0x01!= 0{
  5553.             $value /= 100;
  5554.         }
  5555.         return $value;
  5556.     }
  5557.  
  5558.     /**
  5559.      * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
  5560.      *
  5561.      * @param string $string 
  5562.      * @param bool $compressed 
  5563.      * @return string 
  5564.      */
  5565.     private function _encodeUTF16($string$compressed '')
  5566.     {
  5567.         if ($compressed{
  5568.             $string $this->_uncompressByteString($string);
  5569.          }
  5570.  
  5571.         $result PHPExcel_Shared_String::ConvertEncoding($string'UTF-8''UTF-16LE');
  5572.  
  5573.         return $result;
  5574.     }
  5575.  
  5576.     /**
  5577.      * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
  5578.      *
  5579.      * @param string $string 
  5580.      * @return string 
  5581.      */
  5582.     private function _uncompressByteString($string)
  5583.     {
  5584.         $uncompressedString '';
  5585.         for ($i 0$i strlen($string)++$i{
  5586.             $uncompressedString .= $string[$i"\0";
  5587.         }
  5588.  
  5589.         return $uncompressedString;
  5590.     }
  5591.  
  5592.     /**
  5593.      * Convert string to UTF-8. Only used for BIFF5.
  5594.      *
  5595.      * @param string $string 
  5596.      * @return string 
  5597.      */
  5598.     private function _decodeCodepage($string)
  5599.     {
  5600.         $result PHPExcel_Shared_String::ConvertEncoding($string'UTF-8'$this->_codepage);
  5601.         return $result;
  5602.     }
  5603.  
  5604.     /**
  5605.      * Read 16-bit unsigned integer
  5606.      *
  5607.      * @param string $data 
  5608.      * @param int $pos 
  5609.      * @return int 
  5610.      */
  5611.     private function _GetInt2d($data$pos)
  5612.     {
  5613.         return ord($data[$pos](ord($data[$pos 1]<< 8);
  5614.     }
  5615.  
  5616.     /**
  5617.      * Read 32-bit signed integer
  5618.      *
  5619.      * @param string $data 
  5620.      * @param int $pos 
  5621.      * @return int 
  5622.      */
  5623.     private function _GetInt4d($data$pos)
  5624.     {
  5625.         //return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) |
  5626.         //    (ord($data[$pos + 2]) << 16) | (ord($data[$pos + 3]) << 24);
  5627.  
  5628.         // FIX: represent numbers correctly on 64-bit system
  5629.         // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  5630.         $_or_24 ord($data[$pos 3]);
  5631.         if ($_or_24 >= 128{
  5632.             // negative number
  5633.             $_ord_24 = -abs((256 $_or_24<< 24);
  5634.         else {
  5635.             $_ord_24 ($_or_24 127<< 24;
  5636.         }
  5637.         return ord($data[$pos](ord($data[$pos 1]<< 8(ord($data[$pos 2]<< 16$_ord_24;
  5638.     }
  5639.  
  5640.     /**
  5641.      * Read color
  5642.      *
  5643.      * @param int $color Indexed color
  5644.      * @return array RGB color value, example: array('rgb' => 'FF0000')
  5645.      */
  5646.     private function _readColor($color)
  5647.     {
  5648.         if ($color <= 0x07 || $color >= 0x40{
  5649.             // special built-in color
  5650.             $color $this->_mapBuiltInColor($color);
  5651.         else if (isset($this->_palette&& isset($this->_palette[$color 8])) {
  5652.             // palette color, color index 0x08 maps to pallete index 0
  5653.             $color $this->_palette[$color 8];
  5654.         else {
  5655.             // default color table
  5656.             if ($this->_version == self::XLS_BIFF8{
  5657.                 $color $this->_mapColor($color);
  5658.             else {
  5659.                 // BIFF5
  5660.                 $color $this->_mapColorBIFF5($color);
  5661.             }
  5662.         }
  5663.  
  5664.         return $color;
  5665.     }
  5666.  
  5667.  
  5668.     /**
  5669.      * Map border style
  5670.      * OpenOffice documentation: 2.5.11
  5671.      *
  5672.      * @param int $index 
  5673.      * @return string 
  5674.      */
  5675.     private function _mapBorderStyle($index)
  5676.     {
  5677.         switch ($index{
  5678.             case 0x00return PHPExcel_Style_Border::BORDER_NONE;
  5679.             case 0x01return PHPExcel_Style_Border::BORDER_THIN;
  5680.             case 0x02return PHPExcel_Style_Border::BORDER_MEDIUM;
  5681.             case 0x03return PHPExcel_Style_Border::BORDER_DASHED;
  5682.             case 0x04return PHPExcel_Style_Border::BORDER_DOTTED;
  5683.             case 0x05return PHPExcel_Style_Border::BORDER_THICK;
  5684.             case 0x06return PHPExcel_Style_Border::BORDER_DOUBLE;
  5685.             case 0x07return PHPExcel_Style_Border::BORDER_HAIR;
  5686.             case 0x08return PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
  5687.             case 0x09return PHPExcel_Style_Border::BORDER_DASHDOT;
  5688.             case 0x0Areturn PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
  5689.             case 0x0Breturn PHPExcel_Style_Border::BORDER_DASHDOTDOT;
  5690.             case 0x0Creturn PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  5691.             case 0x0Dreturn PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
  5692.             default:   return PHPExcel_Style_Border::BORDER_NONE;
  5693.         }
  5694.     }
  5695.  
  5696.     /**
  5697.      * Get fill pattern from index
  5698.      * OpenOffice documentation: 2.5.12
  5699.      *
  5700.      * @param int $index 
  5701.      * @return string 
  5702.      */
  5703.     private function _mapFillPattern($index)
  5704.     {
  5705.         switch ($index{
  5706.             case 0x00return PHPExcel_Style_Fill::FILL_NONE;
  5707.             case 0x01return PHPExcel_Style_Fill::FILL_SOLID;
  5708.             case 0x02return PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
  5709.             case 0x03return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
  5710.             case 0x04return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
  5711.             case 0x05return PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
  5712.             case 0x06return PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
  5713.             case 0x07return PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
  5714.             case 0x08return PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
  5715.             case 0x09return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
  5716.             case 0x0Areturn PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
  5717.             case 0x0Breturn PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
  5718.             case 0x0Creturn PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
  5719.             case 0x0Dreturn PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
  5720.             case 0x0Ereturn PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
  5721.             case 0x0Freturn PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
  5722.             case 0x10return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
  5723.             case 0x11return PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
  5724.             case 0x12return PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
  5725.             default:   return PHPExcel_Style_Fill::FILL_NONE;
  5726.         }
  5727.     }
  5728.  
  5729.     /**
  5730.      * Map error code, e.g. '#N/A'
  5731.      *
  5732.      * @param int $subData 
  5733.      * @return string 
  5734.      */
  5735.     private function _mapErrorCode($subData)
  5736.     {
  5737.         switch ($subData{
  5738.             case 0x00return '#NULL!';        break;
  5739.             case 0x07return '#DIV/0!';    break;
  5740.             case 0x0Freturn '#VALUE!';    break;
  5741.             case 0x17return '#REF!';        break;
  5742.             case 0x1Dreturn '#NAME?';        break;
  5743.             case 0x24return '#NUM!';        break;
  5744.             case 0x2Areturn '#N/A';        break;
  5745.             defaultreturn false;
  5746.         }
  5747.     }
  5748.  
  5749.     /**
  5750.      * Map built-in color to RGB value
  5751.      *
  5752.      * @param int $color Indexed color
  5753.      * @return array 
  5754.      */
  5755.     private function _mapBuiltInColor($color)
  5756.     {
  5757.         switch ($color{
  5758.             case 0x00return array('rgb' => '000000');
  5759.             case 0x01return array('rgb' => 'FFFFFF');
  5760.             case 0x02return array('rgb' => 'FF0000');
  5761.             case 0x03return array('rgb' => '00FF00');
  5762.             case 0x04return array('rgb' => '0000FF');
  5763.             case 0x05return array('rgb' => 'FFFF00');
  5764.             case 0x06return array('rgb' => 'FF00FF');
  5765.             case 0x07return array('rgb' => '00FFFF');
  5766.             case 0x40return array('rgb' => '000000')// system window text color
  5767.             case 0x41return array('rgb' => 'FFFFFF')// system window background color
  5768.             default:   return array('rgb' => '000000');
  5769.         }
  5770.     }
  5771.  
  5772.     /**
  5773.      * Map color array from BIFF5 built-in color index
  5774.      *
  5775.      * @param int $subData 
  5776.      * @return array 
  5777.      */
  5778.     private function _mapColorBIFF5($subData)
  5779.     {
  5780.         switch ($subData{
  5781.             case 0x08return array('rgb' => '000000');
  5782.             case 0x09return array('rgb' => 'FFFFFF');
  5783.             case 0x0Areturn array('rgb' => 'FF0000');
  5784.             case 0x0Breturn array('rgb' => '00FF00');
  5785.             case 0x0Creturn array('rgb' => '0000FF');
  5786.             case 0x0Dreturn array('rgb' => 'FFFF00');
  5787.             case 0x0Ereturn array('rgb' => 'FF00FF');
  5788.             case 0x0Freturn array('rgb' => '00FFFF');
  5789.             case 0x10return array('rgb' => '800000');
  5790.             case 0x11return array('rgb' => '008000');
  5791.             case 0x12return array('rgb' => '000080');
  5792.             case 0x13return array('rgb' => '808000');
  5793.             case 0x14return array('rgb' => '800080');
  5794.             case 0x15return array('rgb' => '008080');
  5795.             case 0x16return array('rgb' => 'C0C0C0');
  5796.             case 0x17return array('rgb' => '808080');
  5797.             case 0x18return array('rgb' => '8080FF');
  5798.             case 0x19return array('rgb' => '802060');
  5799.             case 0x1Areturn array('rgb' => 'FFFFC0');
  5800.             case 0x1Breturn array('rgb' => 'A0E0F0');
  5801.             case 0x1Creturn array('rgb' => '600080');
  5802.             case 0x1Dreturn array('rgb' => 'FF8080');
  5803.             case 0x1Ereturn array('rgb' => '0080C0');
  5804.             case 0x1Freturn array('rgb' => 'C0C0FF');
  5805.             case 0x20return array('rgb' => '000080');
  5806.             case 0x21return array('rgb' => 'FF00FF');
  5807.             case 0x22return array('rgb' => 'FFFF00');
  5808.             case 0x23return array('rgb' => '00FFFF');
  5809.             case 0x24return array('rgb' => '800080');
  5810.             case 0x25return array('rgb' => '800000');
  5811.             case 0x26return array('rgb' => '008080');
  5812.             case 0x27return array('rgb' => '0000FF');
  5813.             case 0x28return array('rgb' => '00CFFF');
  5814.             case 0x29return array('rgb' => '69FFFF');
  5815.             case 0x2Areturn array('rgb' => 'E0FFE0');
  5816.             case 0x2Breturn array('rgb' => 'FFFF80');
  5817.             case 0x2Creturn array('rgb' => 'A6CAF0');
  5818.             case 0x2Dreturn array('rgb' => 'DD9CB3');
  5819.             case 0x2Ereturn array('rgb' => 'B38FEE');
  5820.             case 0x2Freturn array('rgb' => 'E3E3E3');
  5821.             case 0x30return array('rgb' => '2A6FF9');
  5822.             case 0x31return array('rgb' => '3FB8CD');
  5823.             case 0x32return array('rgb' => '488436');
  5824.             case 0x33return array('rgb' => '958C41');
  5825.             case 0x34return array('rgb' => '8E5E42');
  5826.             case 0x35return array('rgb' => 'A0627A');
  5827.             case 0x36return array('rgb' => '624FAC');
  5828.             case 0x37return array('rgb' => '969696');
  5829.             case 0x38return array('rgb' => '1D2FBE');
  5830.             case 0x39return array('rgb' => '286676');
  5831.             case 0x3Areturn array('rgb' => '004500');
  5832.             case 0x3Breturn array('rgb' => '453E01');
  5833.             case 0x3Creturn array('rgb' => '6A2813');
  5834.             case 0x3Dreturn array('rgb' => '85396A');
  5835.             case 0x3Ereturn array('rgb' => '4A3285');
  5836.             case 0x3Freturn array('rgb' => '424242');
  5837.             default:   return array('rgb' => '000000');
  5838.         }
  5839.     }
  5840.  
  5841.     /**
  5842.      * Map color array from BIFF8 built-in color index
  5843.      *
  5844.      * @param int $subData 
  5845.      * @return array 
  5846.      */
  5847.     private function _mapColor($subData)
  5848.     {
  5849.         switch ($subData{
  5850.             case 0x08return array('rgb' => '000000');
  5851.             case 0x09return array('rgb' => 'FFFFFF');
  5852.             case 0x0Areturn array('rgb' => 'FF0000');
  5853.             case 0x0Breturn array('rgb' => '00FF00');
  5854.             case 0x0Creturn array('rgb' => '0000FF');
  5855.             case 0x0Dreturn array('rgb' => 'FFFF00');
  5856.             case 0x0Ereturn array('rgb' => 'FF00FF');
  5857.             case 0x0Freturn array('rgb' => '00FFFF');
  5858.             case 0x10return array('rgb' => '800000');
  5859.             case 0x11return array('rgb' => '008000');
  5860.             case 0x12return array('rgb' => '000080');
  5861.             case 0x13return array('rgb' => '808000');
  5862.             case 0x14return array('rgb' => '800080');
  5863.             case 0x15return array('rgb' => '008080');
  5864.             case 0x16return array('rgb' => 'C0C0C0');
  5865.             case 0x17return array('rgb' => '808080');
  5866.             case 0x18return array('rgb' => '9999FF');
  5867.             case 0x19return array('rgb' => '993366');
  5868.             case 0x1Areturn array('rgb' => 'FFFFCC');
  5869.             case 0x1Breturn array('rgb' => 'CCFFFF');
  5870.             case 0x1Creturn array('rgb' => '660066');
  5871.             case 0x1Dreturn array('rgb' => 'FF8080');
  5872.             case 0x1Ereturn array('rgb' => '0066CC');
  5873.             case 0x1Freturn array('rgb' => 'CCCCFF');
  5874.             case 0x20return array('rgb' => '000080');
  5875.             case 0x21return array('rgb' => 'FF00FF');
  5876.             case 0x22return array('rgb' => 'FFFF00');
  5877.             case 0x23return array('rgb' => '00FFFF');
  5878.             case 0x24return array('rgb' => '800080');
  5879.             case 0x25return array('rgb' => '800000');
  5880.             case 0x26return array('rgb' => '008080');
  5881.             case 0x27return array('rgb' => '0000FF');
  5882.             case 0x28return array('rgb' => '00CCFF');
  5883.             case 0x29return array('rgb' => 'CCFFFF');
  5884.             case 0x2Areturn array('rgb' => 'CCFFCC');
  5885.             case 0x2Breturn array('rgb' => 'FFFF99');
  5886.             case 0x2Creturn array('rgb' => '99CCFF');
  5887.             case 0x2Dreturn array('rgb' => 'FF99CC');
  5888.             case 0x2Ereturn array('rgb' => 'CC99FF');
  5889.             case 0x2Freturn array('rgb' => 'FFCC99');
  5890.             case 0x30return array('rgb' => '3366FF');
  5891.             case 0x31return array('rgb' => '33CCCC');
  5892.             case 0x32return array('rgb' => '99CC00');
  5893.             case 0x33return array('rgb' => 'FFCC00');
  5894.             case 0x34return array('rgb' => 'FF9900');
  5895.             case 0x35return array('rgb' => 'FF6600');
  5896.             case 0x36return array('rgb' => '666699');
  5897.             case 0x37return array('rgb' => '969696');
  5898.             case 0x38return array('rgb' => '003366');
  5899.             case 0x39return array('rgb' => '339966');
  5900.             case 0x3Areturn array('rgb' => '003300');
  5901.             case 0x3Breturn array('rgb' => '333300');
  5902.             case 0x3Creturn array('rgb' => '993300');
  5903.             case 0x3Dreturn array('rgb' => '993366');
  5904.             case 0x3Ereturn array('rgb' => '333399');
  5905.             case 0x3Freturn array('rgb' => '333333');
  5906.             default:   return array('rgb' => '000000');
  5907.         }
  5908.     }
  5909.  
  5910. }

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