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

Documentation generated on Wed, 22 Apr 2009 08:57:54 +0200 by phpDocumentor 1.4.1