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

Source for file Excel5.php

Documentation is available at Excel5.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of tshhe GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Reader
  23.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  26.  */
  27.  
  28. // 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_Shared_OLERead */
  67. require_once 'PHPExcel/Shared/OLERead.php';
  68.  
  69.  
  70. /**
  71.  * PHPExcel_Reader_Excel5
  72.  *
  73.  * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
  74.  *
  75.  * @category   PHPExcel
  76.  * @package    PHPExcel_Reader
  77.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  78.  */
  79. class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
  80. {
  81.     // ParseXL definitions
  82.     const XLS_BIFF8 = 0x600;
  83.     const XLS_BIFF7 = 0x500;
  84.     const XLS_WorkbookGlobals = 0x5;
  85.     const XLS_Worksheet = 0x10;
  86.     
  87.     const XLS_Type_BOF = 0x809;
  88.     const XLS_Type_EOF = 0x0a;
  89.     const XLS_Type_BOUNDSHEET = 0x85;
  90.     const XLS_Type_DIMENSION = 0x200;
  91.     const XLS_Type_ROW = 0x208;
  92.     const XLS_Type_DBCELL = 0xd7;
  93.     const XLS_Type_FILEPASS = 0x2f;
  94.     const XLS_Type_NOTE = 0x1c;
  95.     const XLS_Type_TXO = 0x1b6;
  96.     const XLS_Type_RK = 0x7e;
  97.     const XLS_Type_RK2 = 0x27e;
  98.     const XLS_Type_MULRK = 0xbd;
  99.     const XLS_Type_MULBLANK = 0xbe;
  100.     const XLS_Type_INDEX = 0x20b;
  101.     const XLS_Type_SST = 0xfc;
  102.     const XLS_Type_EXTSST = 0xff;
  103.     const XLS_Type_CONTINUE = 0x3c;
  104.     const XLS_Type_LABEL = 0x204;
  105.     const XLS_Type_LABELSST = 0xfd;
  106.     const XLS_Type_NUMBER = 0x203;
  107.     const XLS_Type_EXTSHEET = 0x17;
  108.     const XLS_Type_NAME = 0x18;
  109.     const XLS_Type_ARRAY = 0x221;
  110.     const XLS_Type_STRING = 0x207;
  111.     const XLS_Type_FORMULA = 0x406;
  112.     const XLS_Type_FORMULA2 = 0x6;
  113.     const XLS_Type_FORMAT = 0x41e;
  114.     const XLS_Type_XF = 0xe0;
  115.     const XLS_Type_BOOLERR = 0x205;
  116.     const XLS_Type_UNKNOWN = 0xffff;
  117.     const XLS_Type_NINETEENFOUR = 0x22;
  118.     const XLS_Type_MERGEDCELLS = 0xe5;
  119.     const XLS_Type_CODEPAGE = 0x42;
  120.     const XLS_Type_PROTECT                = 0x0012;
  121.     const XLS_Type_PASSWORD                = 0x0013;
  122.     const XLS_Type_COLINFO                = 0x007d;
  123.     const XLS_Type_FONT                    = 0x0031;
  124.     const XLS_Type_EXTERNALBOOK            = 0x01ae;
  125.     const XLS_Type_BLANK                = 0x0201;
  126.     const XLS_Type_SHEETPR                = 0x0081;
  127.     const XLS_Type_DEFAULTROWHEIGHT     = 0x0225;
  128.     const XLS_Type_DEFCOLWIDTH             = 0x0055;
  129.     
  130.     const XLS_utcOffsetDays = 25569;
  131.     const XLS_utcOffsetDays1904 = 24107;
  132.     const XLS_SecInADay = 86400;
  133.     
  134.     const XLS_DEF_NUM_FORMAT = "%s";
  135.  
  136.     /**
  137.      * Read data only?
  138.      *
  139.      * @var boolean 
  140.      */
  141.     private $_readDataOnly = false;
  142.     
  143.     private $_boundsheets = array();
  144.     
  145.     // shared numberFormats
  146.     // $_formatRecords[] will eventually be replaced by $_numberFormat[]
  147.     private $_formatRecords = array();
  148.     private $_numberFormat = array();
  149.     
  150.     /**
  151.      * Shared strings
  152.      *
  153.      * @var array 
  154.      */
  155.     private $_sst = array();
  156.     
  157.     /**
  158.      * Shared styles
  159.      *
  160.      * @var array 
  161.      */
  162.     private $_xf = array();
  163.     
  164.     /**
  165.      * Shared fonts
  166.      *
  167.      * @var array 
  168.      */
  169.     private $_font = array();
  170.     
  171.     // REF structures
  172.     private $_ref = array();
  173.     
  174.     private $_sheets = array();
  175.     // dvc: added list of names and their sheet associated indexes
  176.     private $_namedcells = array();
  177.     private $_data;
  178.     private $_pos;
  179.     private $_ole;
  180.     private $_defaultEncoding;
  181.     private $_codepage;
  182.     private $_defaultFormat = self::XLS_DEF_NUM_FORMAT;
  183.     private $_columnsFormat = array();
  184.     private $_rowoffset = 1;
  185.     private $_coloffset = 1;
  186.     // dvc: added for external sheets references
  187.     private $_extshref = array();
  188.  
  189.     private $_dateFormats = array (
  190.         // dvc: fixed known date formats
  191.         0xe => 'd/m/Y',
  192.         0xf => 'd-M-y',
  193.         0x10 => 'd-M',
  194.         0x11 => 'M-y',
  195.         0x12 => 'h:i A',
  196.         0x13 => 'h:i:s A',
  197.         0x14 => 'H:i',
  198.         0x15 => 'H:i:s',
  199.         0x16 => 'd/m/Y H:i',
  200.         0x2d => 'i:s',
  201.         0x2e => 'H:i:s',
  202.         0x2f => 'i:s'
  203.     );
  204.  
  205.     // dvc: separated percent formats
  206.     private $_percentFormats = array(
  207.         0x9 => '%1.0f%%',
  208.         0xa => '%1.2f%%'
  209.     );
  210.  
  211.     // dvc: removed exponentials to format as default strings.
  212.     private $_numberFormats = array(
  213.         0x1 => '%1.0f',
  214.         0x2 => '%1.2f',
  215.         0x3 => '%1.0f',
  216.         0x4 => '%1.2f',
  217.         0x5 => '%1.0f',
  218.         0x6 => '$%1.0f',
  219.         0x7 => '$%1.2f',
  220.         0x8 => '$%1.2f',
  221.         0x25 => '%1.0f',
  222.         0x26 => '%1.0f',
  223.         0x27 => '%1.2f',
  224.         0x28 => '%1.2f',
  225.         0x29 => '%1.0f',
  226.         0x2a => '$%1.0f',
  227.         0x2b => '%1.2f',
  228.         0x2c => '$%1.2f'
  229.     );
  230.  
  231.     /**
  232.      * Read data only?
  233.      *
  234.      * @return boolean 
  235.      */
  236.     public function getReadDataOnly() {
  237.         return $this->_readDataOnly;
  238.     }
  239.     
  240.     /**
  241.      * Set read data only
  242.      *
  243.      * @param boolean $pValue 
  244.      */
  245.     public function setReadDataOnly($pValue = false) {
  246.         $this->_readDataOnly = $pValue;
  247.     }
  248.     
  249.     /**
  250.      * Loads PHPExcel from file
  251.      *
  252.      * @param     string         $pFilename 
  253.      * @throws     Exception
  254.      */
  255.     public function load($pFilename)
  256.     {
  257.         // Check if file exists
  258.         if (!file_exists($pFilename)) {
  259.             throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  260.         }
  261.  
  262.         // Initialisations
  263.         $excel = new PHPExcel;
  264.         $excel->removeSheetByIndex(0);
  265.  
  266.         // Use ParseXL for the hard work.
  267.         $this->_ole = new PHPExcel_Shared_OLERead();
  268.  
  269.         $this->_rowoffset = $this->_coloffset = 0;
  270.         $this->_defaultEncoding = 'ISO-8859-1';
  271.         $this->_encoderFunction = function_exists('mb_convert_encoding') ?
  272.             'mb_convert_encoding' : 'iconv';
  273.  
  274.         // get excel data
  275.         $res = $this->_ole->read($pFilename);
  276.  
  277.         // oops, something goes wrong (Darko Miljanovic)
  278.         if($res === false) { // check error code
  279.             if($this->_ole->error == 1) { // bad file
  280.                 throw new Exception('The filename ' . $pFilename . ' is not readable');
  281.             } elseif($this->_ole->error == 2) {
  282.                 throw new Exception('The filename ' . $pFilename . ' is not recognised as an Excel file');
  283.             }
  284.             // check other error codes here (eg bad fileformat, etc...)
  285.         }
  286.  
  287.         $this->_data = $this->_ole->getWorkBook();
  288.         $this->_pos = 0;
  289.         
  290.         /**
  291.          * PARSE WORKBOOK
  292.          *
  293.          **/
  294.         $pos = 0;
  295.         $code = ord($this->_data[$pos]) | ord($this->_data[$pos + 1]) << 8;
  296.         $length = ord($this->_data[$pos + 2]) | ord($this->_data[$pos + 3]) << 8;
  297.         $version = ord($this->_data[$pos + 4]) | ord($this->_data[$pos + 5]) << 8;
  298.         $substreamType = ord($this->_data[$pos + 6]) | ord($this->_data[$pos + 7]) << 8;
  299.  
  300.  
  301.         if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
  302.             return false;
  303.         }
  304.         if ($substreamType != self::XLS_WorkbookGlobals){
  305.             return false;
  306.         }
  307.         $pos += $length + 4;
  308.         $code = ord($this->_data[$pos]) | ord($this->_data[$pos + 1]) << 8;
  309.         $length = ord($this->_data[$pos + 2]) | ord($this->_data[$pos + 3]) << 8;
  310.         $recordData = substr($this->_data, $pos + 4, $length);
  311.         
  312.         while ($code != self::XLS_Type_EOF){
  313.             switch ($code) {
  314.                 case self::XLS_Type_SST:
  315.                     /**
  316.                      * SST - Shared String Table
  317.                      *
  318.                      * This record contains a list of all strings used anywhere
  319.                      * in the workbook. Each string occurs only once. The
  320.                      * workbook uses indexes into the list to reference the
  321.                      * strings.
  322.                      *
  323.                      * --    "OpenOffice.org's Documentation of the Microsoft
  324.                      *         Excel File Format"
  325.                      **/
  326.                     // offset: 0; size: 4; total number of strings
  327.                     // offset: 4; size: 4; number of unique strings
  328.                     $spos = $pos + 4;
  329.                     $limitpos = $spos + $length;
  330.                     $uniqueStrings = $this->_GetInt4d($this->_data, $spos + 4);
  331.                     $spos += 8;
  332.                     // loop through the Unicode strings (16-bit length)
  333.                     for ($i = 0; $i < $uniqueStrings; $i++) {
  334.                         if ($spos == $limitpos) {
  335.                             // then we have reached end of SST record data
  336.                             $opcode = ord($this->_data[$spos]) | ord($this->_data[$spos + 1])<<8;
  337.                             $conlength = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3])<<8;
  338.                             if ($opcode != self::XLS_Type_CONTINUE) {
  339.                                 // broken file, something is wrong
  340.                                 return -1;
  341.                             }
  342.                             $spos += 4;
  343.                             $limitpos = $spos + $conlength;
  344.                         }
  345.                         // Read in the number of characters in the Unicode string
  346.                         $numChars = ord($this->_data[$spos]) | (ord($this->_data[$spos + 1]) << 8);
  347.                         $spos += 2;
  348.                         // option flags
  349.                         $optionFlags = ord($this->_data[$spos]);
  350.                         $spos++;
  351.                         // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  352.                         $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  353.                         // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  354.                         $extendedString = ( ($optionFlags & 0x04) != 0); // Asian phonetic
  355.                         // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  356.                         $richString = ( ($optionFlags & 0x08) != 0);
  357.                         if ($richString) { // Read in the crun
  358.                             // number of Rich-Text formatting runs
  359.                             $formattingRuns = ord($this->_data[$spos]) | (ord($this->_data[$spos + 1]) << 8);
  360.                             $spos += 2;
  361.                         }
  362.                         if ($extendedString) {
  363.                             // size of Asian phonetic setting
  364.                             $extendedRunLength = $this->_GetInt4d($this->_data, $spos);
  365.                             $spos += 4;
  366.                         }
  367.                         // read in the characters
  368.                         $len = ($asciiEncoding) ? $numChars : $numChars * 2;
  369.                         if ($spos + $len < $limitpos) {
  370.                             $retstr = substr($this->_data, $spos, $len);
  371.                             $spos += $len;
  372.                         } else {
  373.                             // found countinue record
  374.                             $retstr = substr($this->_data, $spos, $limitpos - $spos);
  375.                             $bytesRead = $limitpos - $spos;
  376.                             // remaining characters in Unicode string
  377.                             $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  378.                             $spos = $limitpos;
  379.                             // keep reading the characters
  380.                             while ($charsLeft > 0) {
  381.                                 // record data 
  382.                                 $opcode = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  383.                                 
  384.                                 // length of continue record data
  385.                                 $conlength = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  386.                                 if ($opcode != self::XLS_Type_CONTINUE) {
  387.                                     // broken file, something is wrong
  388.                                     return -1;
  389.                                 }
  390.                                 $spos += 4;
  391.                                 $limitpos = $spos + $conlength;
  392.                                 
  393.                                 // option flags are repeated when Unicode string is split by a continue record
  394.                                 // OpenOffice.org documentation 5.21
  395.                                 $option = ord($this->_data[$spos]);
  396.                                 $spos += 1;
  397.                                 
  398.                                 if ($asciiEncoding && ($option == 0)) {
  399.                                     // 1st fragment compressed
  400.                                     // this fragment compressed
  401.                                     $len = min($charsLeft, $limitpos - $spos);
  402.                                     $retstr .= substr($this->_data, $spos, $len);
  403.                                     $charsLeft -= $len;
  404.                                     $asciiEncoding = true;
  405.  
  406.                                 } elseif (!$asciiEncoding && ($option != 0)) {
  407.                                     // 1st fragment uncompressed
  408.                                     // this fragment uncompressed
  409.                                     $len = min($charsLeft * 2, $limitpos - $spos);
  410.                                     $retstr .= substr($this->_data, $spos, $len);
  411.                                     $charsLeft -= $len/2;
  412.                                     $asciiEncoding = false;
  413.  
  414.                                 } elseif (!$asciiEncoding && ($option == 0)) {
  415.                                     // 1st fragment uncompressed
  416.                                     // this fragment compressed
  417.                                     $len = min($charsLeft, $limitpos - $spos);
  418.                                     for ($j = 0; $j < $len; $j++) {
  419.                                         $retstr .= $this->_data[$spos + $j].chr(0);
  420.                                     }
  421.                                     $charsLeft -= $len;
  422.                                     $asciiEncoding = false;
  423.                                 } else {
  424.                                     // 1st fragment compressed
  425.                                     // this fragment uncompressed
  426.                                     $newstr = '';
  427.                                     for ($j = 0; $j < strlen($retstr); $j++) {
  428.                                         $newstr = $retstr[$j].chr(0);
  429.                                     }
  430.                                     $retstr = $newstr;
  431.                                     $len = min($charsLeft * 2, $limitpos - $spos);
  432.                                     $retstr .= substr($this->_data, $spos, $len);
  433.                                     $charsLeft -= $len/2;
  434.                                     $asciiEncoding = false;
  435.                                 }
  436.                                 $spos += $len;
  437.                             }
  438.                         }
  439.                         //$retstr = ($asciiEncoding) ?
  440.                         //    $retstr : $this->_encodeUTF16($retstr);
  441.                         // convert string according codepage and BIFF version
  442.  
  443.                         if($version == self::XLS_BIFF8) {
  444.                             $retstr = $this->_encodeUTF16($retstr, $asciiEncoding);
  445.  
  446.                         } else {
  447.                             // SST only occurs in BIFF8, so why this part? 
  448.                             $retstr = $this->_decodeCodepage($retstr);
  449.                         }
  450.  
  451.                         $fmtRuns = array();
  452.                         if ($richString) {
  453.                             // list of formatting runs
  454.                             for ($j = 0; $j < $formattingRuns; $j++) {
  455.                                 // first formatted character; zero-based
  456.                                 $charPos = $this->_getInt2d($this->_data, $spos + $j * 4);
  457.                                 // index to font record
  458.                                 $fontIndex = $this->_getInt2d($this->_data, $spos + 2 + $j * 4);
  459.                                 $fmtRuns[] = array(
  460.                                     'charPos' => $charPos,
  461.                                     'fontIndex' => $fontIndex,
  462.                                 );
  463.                             }
  464.                             $spos += 4 * $formattingRuns;
  465.                         }
  466.                         if ($extendedString) {
  467.                             // For Asian phonetic settings, we skip the extended string data
  468.                             $spos += $extendedRunLength;
  469.                         }
  470.                         $this->_sst[] = array(
  471.                             'value' => $retstr,
  472.                             'fmtRuns' => $fmtRuns,
  473.                         );
  474.                     }
  475.                     break;
  476.  
  477.                 case self::XLS_Type_FILEPASS:
  478.                     /**
  479.                      * SHEETPROTECTION
  480.                      *
  481.                      * This record is part of the File Protection Block. It
  482.                      * contains information about the read/write password of the
  483.                      * file. All record contents following this record will be
  484.                      * encrypted.
  485.                      *
  486.                      * --    "OpenOffice.org's Documentation of the Microsoft
  487.                      *         Excel File Format"
  488.                      */
  489.                     return false;
  490.                     break;
  491.  
  492.                 case self::XLS_Type_EXTERNALBOOK:
  493.                     break;
  494.                 
  495.                 case self::XLS_Type_EXTSHEET:
  496.                     // external sheet references provided for named cells
  497.                     if ($version == self::XLS_BIFF8) {
  498.                         $xpos = $pos + 4;
  499.                         $xcnt = ord($this->_data[$xpos]) | ord($this->_data[$xpos + 1]) << 8;
  500.                         for ($x = 0; $x < $xcnt; $x++) {
  501.                             $this->_extshref[$x] = ord($this->_data[$xpos + 4 + 6*$x]) |
  502.                                 ord($this->_data[$xpos + 5 + 6*$x]) << 8;
  503.                         }
  504.                     }
  505.                     
  506.                     // this if statement is going to replace the above one later
  507.                     if ($version == self::XLS_BIFF8) {
  508.                         // offset: 0; size: 2; number of following ref structures
  509.                         $nm = $this->_GetInt2d($recordData, 0);
  510.                         for ($i = 0; $i < $nm; $i++) {
  511.                             $this->_ref[] = array(
  512.                                 // offset: 2 + 6 * $i; index to EXTERNALBOOK record
  513.                                 'externalBookIndex' => $this->_getInt2d($recordData, 2 + 6 * $i),
  514.                                 // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
  515.                                 'firstSheetIndex' => $this->_getInt2d($recordData, 4 + 6 * $i),
  516.                                 // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
  517.                                 'lastSheetIndex' => $this->_getInt2d($recordData, 6 + 6 * $i),
  518.                             );
  519.                         }
  520.                     }
  521.                     break;
  522.  
  523.                 case self::XLS_Type_NAME:
  524.                     /**
  525.                      * DEFINEDNAME
  526.                      *
  527.                      * This record is part of a Link Table. It contains the name
  528.                      * and the token array of an internal defined name. Token
  529.                      * arrays of defined names contain tokens with aberrant
  530.                      * token classes.
  531.                      *
  532.                      * --    "OpenOffice.org's Documentation of the Microsoft
  533.                      *         Excel File Format"
  534.                      */
  535.                     // retrieves named cells
  536.                     $npos = $pos + 4;
  537.                     $opts = ord($this->_data[$npos]) | ord($this->_data[$npos + 1]) << 8;
  538.                     $nlen = ord($this->_data[$npos + 3]);
  539.                     $flen = ord($this->_data[$npos + 4]) | ord($this->_data[$npos + 5]) << 8;
  540.                     $fpos = $npos + 14 + 1 + $nlen;
  541.                     $nstr = substr($this->_data, $npos + 15, $nlen);
  542.                     $ftoken = ord($this->_data[$fpos]);
  543.                     if ($ftoken == 58 && $opts == 0 && $flen == 7) {
  544.                         $xref = ord($this->_data[$fpos + 1]) | ord($this->_data[$fpos + 2]) << 8;
  545.                         $frow = ord($this->_data[$fpos + 3]) | ord($this->_data[$fpos + 4]) << 8;
  546.                         $fcol = ord($this->_data[$fpos + 5]);
  547.                         if (array_key_exists($xref,$this->_extshref)) {
  548.                             $fsheet = $this->_extshref[$xref];
  549.                         } else {
  550.                             $fsheet = '';
  551.                         }
  552.                         $this->_namedcells[$nstr] = array(
  553.                             'sheet' => $fsheet,
  554.                             'row' => $frow,
  555.                             'column' => $fcol
  556.                         );
  557.                     }
  558.                     break;
  559.  
  560.                 case self::XLS_Type_FORMAT:
  561.                     /**
  562.                      * FORMAT
  563.                      *
  564.                      * This record contains information about a number format.
  565.                      * All FORMAT records occur together in a sequential list.
  566.                      *
  567.                      * In BIFF2-BIFF4 other records referencing a FORMAT record
  568.                      * contain a zero-based index into this list. From BIFF5 on
  569.                      * the FORMAT record contains the index itself that will be
  570.                      * used by other records.
  571.                      *
  572.                      * --    "OpenOffice.org's Documentation of the Microsoft
  573.                      *         Excel File Format"
  574.                      */
  575.                     //$indexCode = ord($this->_data[$pos + 4]) | ord($this->_data[$pos + 5]) << 8;
  576.                     $indexCode = $this->_GetInt2d($recordData, 0);
  577.                     
  578.                     /*
  579.                     if ($version == self::XLS_BIFF8) {
  580.                     */
  581.                         $formatString = $this->_readUnicodeStringLong(substr($recordData, 2));
  582.                     /*
  583.                     } else {
  584.                         $numchars = ord($this->_data[$pos + 6]);
  585.                         $formatString = substr($this->_data, $pos + 7, $numchars*2);
  586.                     }
  587.                     */
  588.                     $this->_formatRecords[$indexCode] = $formatString;
  589.                     
  590.                     // now also stored in array _format[]
  591.                     // _formatRecords[] will be removed from code later
  592.                     $this->_numberFormat[$indexCode] = $formatString;
  593.  
  594.                     break;
  595.  
  596.                 case self::XLS_Type_FONT:
  597.                     /**
  598.                      * FONT
  599.                      */
  600.                     $this->_font[] = $this->_readFont($recordData);
  601.                     
  602.                     break;
  603.                     
  604.                 case self::XLS_Type_XF:
  605.                     /**
  606.                      * XF - Extended Format
  607.                      *
  608.                      * This record contains formatting information for cells,
  609.                      * rows, columns or styles.
  610.                      *
  611.                      * --    "OpenOffice.org's Documentation of the Microsoft
  612.                      *         Excel File Format"
  613.                      */
  614.                     $indexCode = ord($this->_data[$pos + 6]) | ord($this->_data[$pos + 7]) << 8;
  615.                     if (array_key_exists($indexCode, $this->_dateFormats)) {
  616.                         $this->_formatRecords['xfrecords'][] = array(
  617.                             'type' => 'date',
  618.                             'format' => $this->_dateFormats[$indexCode],
  619.                             'code' => $indexCode
  620.                         );
  621.                     } elseif (array_key_exists($indexCode, $this->_percentFormats)) {
  622.                         $this->_formatRecords['xfrecords'][] = array(
  623.                             'type' => 'percent',
  624.                             'format' => $this->_percentFormats[$indexCode],
  625.                             'code' => $indexCode
  626.                         );
  627.                     } elseif (array_key_exists($indexCode, $this->_numberFormats)) {
  628.                         $this->_formatRecords['xfrecords'][] = array(
  629.                             'type' => 'number',
  630.                             'format' => $this->_numberFormats[$indexCode],
  631.                             'code' => $indexCode
  632.                         );
  633.                     } else {
  634.                         if ($indexCode > 0 && isset($this->_formatRecords[$indexCode])) {
  635.                             // custom formats...
  636.                             $formatstr = $this->_formatRecords[$indexCode];
  637.                             if ($formatstr) {
  638.                                 // dvc: reg exp changed to custom date/time format chars
  639.                                 if (preg_match("/^[hmsdy]/i", $formatstr)) {
  640.                                     // custom datetime format
  641.                                     // dvc: convert Excel formats to PHP date formats
  642.                                     // first remove escapes related to non-format characters
  643.                                     $formatstr = str_replace('\\', '', $formatstr);
  644.                                     // 4-digit year
  645.                                     $formatstr = str_replace('yyyy', 'Y', $formatstr);
  646.                                     // 2-digit year
  647.                                     $formatstr = str_replace('yy', 'y', $formatstr);
  648.                                     // first letter of month - no php equivalent
  649.                                     $formatstr = str_replace('mmmmm', 'M', $formatstr);
  650.                                     // full month name
  651.                                     $formatstr = str_replace('mmmm', 'F', $formatstr);
  652.                                     // short month name
  653.                                     $formatstr = str_replace('mmm', 'M', $formatstr);
  654.                                     // mm is minutes if time or month w/leading zero
  655.                                     $formatstr = str_replace(':mm', ':i', $formatstr);
  656.                                     // tmp place holder
  657.                                     $formatstr = str_replace('mm', 'x', $formatstr);
  658.                                     // month no leading zero
  659.                                     $formatstr = str_replace('m', 'n', $formatstr);
  660.                                     // month leading zero
  661.                                     $formatstr = str_replace('x', 'm', $formatstr);
  662.                                     // 12-hour suffix
  663.                                     $formatstr = str_replace('AM/PM', 'A', $formatstr);
  664.                                     // tmp place holder
  665.                                     $formatstr = str_replace('dd', 'x', $formatstr);
  666.                                     // days no leading zero
  667.                                     $formatstr = str_replace('d', 'j', $formatstr);
  668.                                     // days leading zero
  669.                                     $formatstr = str_replace('x', 'd', $formatstr);
  670.                                     // seconds
  671.                                     $formatstr = str_replace('ss', 's', $formatstr);
  672.                                     // fractional seconds - no php equivalent
  673.                                     $formatstr = str_replace('.S', '', $formatstr);
  674.                                     if (! strpos($formatstr,'A')) { // 24-hour format
  675.                                         $formatstr = str_replace('h', 'H', $formatstr);
  676.                                         }
  677.                                     // user defined flag symbol????
  678.                                     $formatstr = str_replace(';@', '', $formatstr);
  679.                                     $this->_formatRecords['xfrecords'][] = array(
  680.                                         'type' => 'date',
  681.                                         'format' => $formatstr,
  682.                                         'code' => $indexCode
  683.                                     );
  684.                                 }
  685.                                 // dvc: new code for custom percent formats
  686.                                 else if (preg_match('/%$/', $formatstr)) { // % number format
  687.                                     if (preg_match('/\.[#0]+/i',$formatstr,$m)) {
  688.                                         $s = substr($m[0],0,1).(strlen($m[0])-1);
  689.                                         $formatstr = str_replace($m[0],$s,$formatstr);
  690.                                     }
  691.                                     if (preg_match('/^[#0]+/',$formatstr,$m)) {
  692.                                         $formatstr = str_replace($m[0],strlen($m[0]),$formatstr);
  693.                                     }
  694.                                     $formatstr = '%' . str_replace('%',"f%%",$formatstr);
  695.                                     $this->_formatRecords['xfrecords'][] = array(
  696.                                         'type' => 'percent',
  697.                                         'format' => $formatstr,
  698.                                         'code' => $indexCode
  699.                                     );
  700.                                 }
  701.                                 // dvc: code for other unknown formats
  702.                                 else {
  703.                                     // dvc: changed to add format to unknown for debug
  704.                                     $this->_formatRecords['xfrecords'][] = array(
  705.                                         'type' => 'other',
  706.                                         'format' => $this->_defaultFormat,
  707.                                         'code' => $indexCode
  708.                                     );
  709.                                 }
  710.                             }
  711.  
  712.                         } else {
  713.                             // dvc: changed to add format to unknown for debug
  714.                             if (isset($this->_formatRecords[$indexCode])) {
  715.                                 $formatstr = $this->_formatRecords[$indexCode];
  716.                                 $type = 'undefined';
  717.                             } else {
  718.                                 $formatstr = $this->_defaultFormat;
  719.                                 $type = 'default';
  720.                             }
  721.                             $this->_formatRecords['xfrecords'][] = array(
  722.                                 'type' => $type,
  723.                                 'format' => $formatstr,
  724.                                 'code' => $indexCode
  725.                             );
  726.                         }
  727.                     }
  728.                     
  729.                     // store styles in xf array
  730.                     $this->_xf[] = $this->_readBIFF8Style($recordData);
  731.                     
  732.                     break;
  733.  
  734.                 case self::XLS_Type_NINETEENFOUR:
  735.                     /**
  736.                      * DATEMODE
  737.                      *
  738.                      * This record specifies the base date for displaying date
  739.                      * values. All dates are stored as count of days past this
  740.                      * base date. In BIFF2-BIFF4 this record is part of the
  741.                      * Calculation Settings Block. In BIFF5-BIFF8 it is
  742.                      * stored in the Workbook Globals Substream.
  743.                      *
  744.                      * --    "OpenOffice.org's Documentation of the Microsoft
  745.                      *         Excel File Format"
  746.                      */
  747.                     $this->_nineteenFour = (ord($this->_data[$pos + 4]) == 1);
  748.                     
  749.                     /*
  750.                     if (ord($this->_data[$pos + 4]) == 1) {
  751.                         PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  752.                     } else {
  753.                         PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  754.                     }
  755.                     */
  756.                     break;
  757.  
  758.                 case self::XLS_Type_BOUNDSHEET:
  759.                     /**
  760.                      * SHEET
  761.                      *
  762.                      * This record is  located in the  Workbook Globals
  763.                      * Substream  and represents a sheet inside the workbook.
  764.                      * One SHEET record is written for each sheet. It stores the
  765.                      * sheet name and a stream offset to the BOF record of the
  766.                      * respective Sheet Substream within the Workbook Stream.
  767.                      *
  768.                      * --    "OpenOffice.org's Documentation of the Microsoft
  769.                      *         Excel File Format"
  770.                      */
  771.                     $rec_offset = $this->_GetInt4d($this->_data, $pos + 4);
  772.                     $rec_typeFlag = ord($this->_data[$pos + 8]);
  773.                     $rec_visibilityFlag = ord($this->_data[$pos + 9]);
  774.                     $rec_length = ord($this->_data[$pos + 10]);
  775.  
  776.  
  777.  
  778.                     if ($version == self::XLS_BIFF8) {
  779.                         $compressedUTF16 = ((ord($this->_data[$pos + 11]) & 0x01) == 0);
  780.                         $rec_length = ($compressedUTF16) ? $rec_length : $rec_length*2;
  781.                         $rec_name = $this->_encodeUTF16(substr($this->_data, $pos + 12, $rec_length), $compressedUTF16);
  782.                     } elseif ($version == self::XLS_BIFF7) {
  783.                         $rec_name        = substr($this->_data, $pos + 11, $rec_length);
  784.                     }
  785.                     $this->_boundsheets[] = array(
  786.                         'name' => $rec_name,
  787.                         'offset' => $rec_offset
  788.                     );
  789.                     break;
  790.  
  791.                 case self::XLS_Type_CODEPAGE:
  792.                     /**
  793.                      * CODEPAGE
  794.                      *
  795.                      * This record stores the text encoding used to write byte
  796.                      * strings, stored as MS Windows code page identifier.
  797.                      *
  798.                      * --    "OpenOffice.org's Documentation of the Microsoft
  799.                      *         Excel File Format"
  800.                      */
  801.                     $codepage = $this->_GetInt2d($this->_data, $pos + 4);
  802.                     switch($codepage) {
  803.                         case 367: // ASCII
  804.                             $this->_codepage ="ASCII";
  805.                             break;
  806.                         case 437: //OEM US
  807.                             $this->_codepage ="CP437";
  808.                             break;
  809.                         case 720: //OEM Arabic
  810.                             // currently not supported by libiconv
  811.                             $this->_codepage = "";
  812.                             break;
  813.                         case 737: //OEM Greek
  814.                             $this->_codepage ="CP737";
  815.                             break;
  816.                         case 775: //OEM Baltic
  817.                             $this->_codepage ="CP775";
  818.                             break;
  819.                         case 850: //OEM Latin I
  820.                             $this->_codepage ="CP850";
  821.                             break;
  822.                         case 852: //OEM Latin II (Central European)
  823.                             $this->_codepage ="CP852";
  824.                             break;
  825.                         case 855: //OEM Cyrillic
  826.                             $this->_codepage ="CP855";
  827.                             break;
  828.                         case 857: //OEM Turkish
  829.                             $this->_codepage ="CP857";
  830.                             break;
  831.                         case 858: //OEM Multilingual Latin I with Euro
  832.                             $this->_codepage ="CP858";
  833.                             break;
  834.                         case 860: //OEM Portugese
  835.                             $this->_codepage ="CP860";
  836.                             break;
  837.                         case 861: //OEM Icelandic
  838.                             $this->_codepage ="CP861";
  839.                             break;
  840.                         case 862: //OEM Hebrew
  841.                             $this->_codepage ="CP862";
  842.                             break;
  843.                         case 863: //OEM Canadian (French)
  844.                             $this->_codepage ="CP863";
  845.                             break;
  846.                         case 864: //OEM Arabic
  847.                             $this->_codepage ="CP864";
  848.                             break;
  849.                         case 865: //OEM Nordic
  850.                             $this->_codepage ="CP865";
  851.                             break;
  852.                         case 866: //OEM Cyrillic (Russian)
  853.                             $this->_codepage ="CP866";
  854.                             break;
  855.                         case 869: //OEM Greek (Modern)
  856.                             $this->_codepage ="CP869";
  857.                             break;
  858.                         case 874: //ANSI Thai
  859.                             $this->_codepage ="CP874";
  860.                             break;
  861.                         case 932: //ANSI Japanese Shift-JIS
  862.                             $this->_codepage ="CP932";
  863.                             break;
  864.                         case 936: //ANSI Chinese Simplified GBK
  865.                             $this->_codepage ="CP936";
  866.                             break;
  867.                         case 949: //ANSI Korean (Wansung)
  868.                             $this->_codepage ="CP949";
  869.                             break;
  870.                         case 950: //ANSI Chinese Traditional BIG5
  871.                             $this->_codepage ="CP950";
  872.                             break;
  873.                         case 1200: //UTF-16 (BIFF8)
  874.                             $this->_codepage ="UTF-16LE";
  875.                             break;
  876.                         case 1250:// ANSI Latin II (Central European)
  877.                             $this->_codepage ="CP1250";
  878.                             break;
  879.                         case 1251: //ANSI Cyrillic
  880.                             $this->_codepage ="CP1251";
  881.                             break;
  882.                         case 1252: //ANSI Latin I (BIFF4-BIFF7)
  883.                             $this->_codepage ="CP1252";
  884.                             break;
  885.                         case 1253: //ANSI Greek
  886.                             $this->_codepage ="CP1253";
  887.                             break;
  888.                         case 1254: //ANSI Turkish
  889.                             $this->_codepage ="CP1254";
  890.                             break;
  891.                         case 1255: //ANSI Hebrew
  892.                             $this->_codepage ="CP1255";
  893.                             break;
  894.                         case 1256: //ANSI Arabic
  895.                             $this->_codepage ="CP1256";
  896.                             break;
  897.                         case 1257: //ANSI Baltic
  898.                             $this->_codepage ="CP1257";
  899.                             break;
  900.                         case 1258: //ANSI Vietnamese
  901.                             $this->_codepage ="CP1258";
  902.                             break;
  903.                         case 1361: //ANSI Korean (Johab)
  904.                             $this->_codepage ="CP1361";
  905.                             break;
  906.                         case 10000: //Apple Roman
  907.                             // currently not supported by libiconv
  908.                             $this->_codepage = "";
  909.                             break;
  910.                         case 32768: //Apple Roman
  911.                             // currently not supported by libiconv
  912.                             $this->_codepage = "";
  913.                             break;
  914.                         case 32769: //ANSI Latin I (BIFF2-BIFF3)
  915.                             // currently not supported by libiconv
  916.                             $this->_codepage = "";
  917.                             break;
  918.                     }
  919.                     break;
  920.                     
  921.             }
  922.             $pos += $length + 4;
  923.             $code = ord($this->_data[$pos]) | ord($this->_data[$pos + 1]) << 8;
  924.             $length = ord($this->_data[$pos + 2]) | ord($this->_data[$pos + 3]) << 8;
  925.             $recordData = substr($this->_data, $pos + 4, $length);
  926.         }
  927.         /**
  928.          *
  929.          * PARSE THE INDIVIDUAL SHEETS
  930.          *
  931.          **/
  932.         foreach ($this->_boundsheets as $key => $val){
  933.             // add sheet to PHPExcel object
  934.             $sheet = $excel->createSheet();
  935.             $sheet->setTitle((string) $val['name']);
  936.             
  937.             $this->_sn = $key;
  938.             $spos = $val['offset'];
  939.             $cont = true;
  940.             // read BOF
  941.             $code = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  942.             $length = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  943.             $version = ord($this->_data[$spos + 4]) | ord($this->_data[$spos + 5]) << 8;
  944.             $substreamType = ord($this->_data[$spos + 6]) | ord($this->_data[$spos + 7]) << 8;
  945.  
  946.             if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
  947.                 return -1;
  948.             }
  949.             if ($substreamType != self::XLS_Worksheet) {
  950.                 return -2;
  951.             }
  952.  
  953.             $spos += $length + 4;
  954.             while($cont) {
  955.                 $lowcode = ord($this->_data[$spos]);
  956.                 if ($lowcode == self::XLS_Type_EOF) {
  957.                     break;
  958.                 }
  959.  
  960.                 $code = $lowcode | ord($this->_data[$spos + 1]) << 8;
  961.                 $length = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  962.                 $recordData = substr($this->_data, $spos + 4, $length);
  963.                 
  964.                 $spos += 4;
  965.                 $this->_sheets[$this->_sn]['maxrow'] = $this->_rowoffset - 1;
  966.                 $this->_sheets[$this->_sn]['maxcol'] = $this->_coloffset - 1;
  967.                 unset($this->_rectype);
  968.                 unset($this->_formula);
  969.                 unset($this->_formula_result);
  970.                 $this->_multiplier = 1; // need for format with %
  971.  
  972.                 switch ($code) {
  973.                     case self::XLS_Type_DIMENSION:
  974.                         /**
  975.                          * DIMENSION
  976.                          *
  977.                          * This record contains the range address of the used area
  978.                          * in the current sheet.
  979.                          *
  980.                          * --    "OpenOffice.org's Documentation of the Microsoft
  981.                          *         Excel File Format"
  982.                          */
  983.                         if (!isset($this->_numRows)) {
  984.                             if (($length == 10) ||    ($version == self::XLS_BIFF7)){
  985.                                 $this->_sheets[$this->_sn]['numRows'] = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  986.                                 $this->_sheets[$this->_sn]['numCols'] = ord($this->_data[$spos + 6]) | ord($this->_data[$spos + 7]) << 8;
  987.                             } else {
  988.                                 $this->_sheets[$this->_sn]['numRows'] = ord($this->_data[$spos + 4]) | ord($this->_data[$spos + 5]) << 8;
  989.                                 $this->_sheets[$this->_sn]['numCols'] = ord($this->_data[$spos + 10]) | ord($this->_data[$spos + 11]) << 8;
  990.                             }
  991.                         }
  992.                         break;
  993.  
  994.                     case self::XLS_Type_MERGEDCELLS:
  995.                         /**
  996.                          * MERGEDCELLS
  997.                          *
  998.                          * This record contains the addresses of merged cell ranges
  999.                          * in the current sheet.
  1000.                          *
  1001.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1002.                          *         Excel File Format"
  1003.                          */
  1004.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1005.                             $cellRanges = $this->_GetInt2d($this->_data, $spos);
  1006.                             
  1007.                             for ($i = 0; $i < $cellRanges; $i++) {
  1008.                                 $fr = $this->_GetInt2d($this->_data, $spos + 8 * $i + 2); // first row
  1009.                                 $lr = $this->_GetInt2d($this->_data, $spos + 8 * $i + 4); // last row
  1010.                                 $fc = $this->_GetInt2d($this->_data, $spos + 8 * $i + 6); // first column
  1011.                                 $lc = $this->_GetInt2d($this->_data, $spos + 8 * $i + 8); // last column
  1012.                                 
  1013.                                 // this part no longer needed, instead apply cell merge on PHPExcel worksheet object
  1014.                                 /*
  1015.                                 if ($lr - $fr > 0) {
  1016.                                     $this->_sheets[$this->_sn]['cellsInfo'][$fr + 1][$fc + 1]['rowspan'] = $lr - $fr + 1;
  1017.                                 }
  1018.                                 if ($lc - $fc > 0) {
  1019.                                     $this->_sheets[$this->_sn]['cellsInfo'][$fr + 1][$fc + 1]['colspan'] = $lc - $fc + 1;
  1020.                                 }
  1021.                                 */
  1022.                                 $sheet->mergeCellsByColumnAndRow($fc, $fr + 1, $lc, $lr + 1);
  1023.                             }
  1024.                         }
  1025.                         break;
  1026.  
  1027.                     case self::XLS_Type_RK:
  1028.                     case self::XLS_Type_RK2:
  1029.                         /**
  1030.                          * RK
  1031.                          *
  1032.                          * This record represents a cell that contains an RK value
  1033.                          * (encoded integer or floating-point value). If a
  1034.                          * floating-point value cannot be encoded to an RK value,
  1035.                          * a NUMBER record will be written. This record replaces the
  1036.                          * record INTEGER written in BIFF2.
  1037.                          *
  1038.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1039.                          *         Excel File Format"
  1040.                          */
  1041.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1042.                         $column = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1043.                         $rknum = $this->_GetInt4d($this->_data, $spos + 6);
  1044.                         $numValue = $this->_GetIEEE754($rknum);
  1045.  
  1046.                         /*
  1047.                         if ($this->_isDate($spos)) {
  1048.                             list($string, $raw) = $this->_createDate($numValue);
  1049.                         } else {
  1050.                             $raw = $numValue;
  1051.                             if (isset($this->_columnsFormat[$column + 1])){
  1052.                                 $this->_curformat = $this->_columnsFormat[$column + 1];
  1053.                             }
  1054.                             $string = sprintf($this->_curformat,$numValue*$this->_multiplier);
  1055.                         }
  1056.                         */
  1057.                         
  1058.                         // offset 4; size: 2; index to XF record
  1059.                         $xfindex = $this->_getInt2d($recordData, 4);
  1060.                         // add BIFF8 style information
  1061.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1062.                             $sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1063.                             
  1064.                             if (PHPExcel_Shared_Date::isDateTimeFormatCode($this->_xf[$xfindex]['numberformat']['code'])) {
  1065.                                 $numValue = (int) PHPExcel_Shared_Date::ExcelToPHP($numValue);
  1066.                             }
  1067.                             
  1068.                         }
  1069.                         
  1070.                         //$this->_addcell($row, $column, $string, $raw);
  1071.                         //$sheet->setCellValueByColumnAndRow($column, $row + 1, $string);
  1072.                         
  1073.                         $sheet->setCellValueByColumnAndRow($column, $row + 1, $numValue);
  1074.                         
  1075.                         break;
  1076.  
  1077.                     case self::XLS_Type_LABELSST:
  1078.                         /**
  1079.                          * LABELSST
  1080.                          *
  1081.                          * This record represents a cell that contains a string. It
  1082.                          * replaces the LABEL record and RSTRING record used in
  1083.                          * BIFF2-BIFF5.
  1084.                          *
  1085.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1086.                          *         Excel File Format"
  1087.                          */
  1088.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1089.                         $column = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1090.                         $xfindex = ord($this->_data[$spos + 4]) | ord($this->_data[$spos + 5]) << 8;
  1091.                         $index = $this->_GetInt4d($this->_data, $spos + 6);
  1092.                         //$this->_addcell($row, $column, $this->_sst[$index]);
  1093.                         
  1094.                         if ($fmtRuns = $this->_sst[$index]['fmtRuns']) {
  1095.                             // then we have rich text
  1096.                             $richText = new PHPExcel_RichText($sheet->getCellByColumnAndRow($column, $row + 1));
  1097.                             $charPos = 0;
  1098.                             for ($i = 0; $i <= count($this->_sst[$index]['fmtRuns']); $i++) {
  1099.                                 if (isset($fmtRuns[$i])) {
  1100.                                     $text = mb_substr($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos, 'UTF-8');
  1101.                                     $charPos = $fmtRuns[$i]['charPos'];
  1102.                                 } else {
  1103.                                     $text = mb_substr($this->_sst[$index]['value'], $charPos, mb_strlen($this->_sst[$index]['value']), 'UTF-8');
  1104.                                 }
  1105.                                 
  1106.                                 if (mb_strlen($text) > 0) {
  1107.                                     $textRun = $richText->createTextRun($text);
  1108.                                     if (isset($fmtRuns[$i - 1])) {
  1109.                                         if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
  1110.                                             $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
  1111.                                         } else {
  1112.                                             // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1113.                                             // check the OpenOffice documentation of the FONT record
  1114.                                             $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
  1115.                                         }
  1116.                                         $textRun->getFont()->applyFromArray($this->_font[$fontIndex]);
  1117.                                     }
  1118.                                 }
  1119.                             }
  1120.                         } else {
  1121.                             $sheet->setCellValueByColumnAndRow($column, $row + 1, $this->_sst[$index]['value']);
  1122.                         }
  1123.                         
  1124.                         // add BIFF8 style information
  1125.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1126.                             $sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1127.                         }
  1128.                         break;
  1129.  
  1130.                     case self::XLS_Type_MULRK:
  1131.                         /**
  1132.                          * MULRK - Multiple RK
  1133.                          *
  1134.                          * This record represents a cell range containing RK value
  1135.                          * cells. All cells are located in the same row.
  1136.                          *
  1137.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1138.                          *         Excel File Format"
  1139.                          */
  1140.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1141.                         $colFirst = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1142.                         $colLast = ord($this->_data[$spos + $length - 2]) | ord($this->_data[$spos + $length - 1]) << 8;
  1143.                         $columns = $colLast - $colFirst + 1;
  1144.                         $tmppos = $spos + 4;
  1145.  
  1146.                         for ($i = 0; $i < $columns; $i++) {
  1147.                             // offset: 0; size: 2; index to XF record
  1148.                             $xfindex = $this->_getInt2d($recordData, 4 + 6 * $i);
  1149.                             
  1150.                             // offset: 2; size: 4; RK value
  1151.                             $numValue = $this->_GetIEEE754($this->_GetInt4d($this->_data, $tmppos + 2));
  1152.                             /*
  1153.                             if ($this->_isDate($tmppos-4)) {
  1154.                                 list($string, $raw) = $this->_createDate($numValue);
  1155.                             } else {
  1156.                                 $raw = $numValue;
  1157.                                 if (isset($this->_columnsFormat[$colFirst + $i + 1])){
  1158.                                     $this->_curformat = $this->_columnsFormat[$colFirst+ $i + 1];
  1159.                                 }
  1160.                                 $string = sprintf($this->_curformat, $numValue *
  1161.                                     $this->_multiplier);
  1162.                             }
  1163.                             */
  1164.                             //$this->_addcell($row, $colFirst + $i, $string, $raw);
  1165.                             if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1166.                                 $sheet->getStyleByColumnAndRow($colFirst + $i, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1167.                                 if (PHPExcel_Shared_Date::isDateTimeFormatCode($this->_xf[$xfindex]['numberformat']['code'])) {
  1168.                                     $numValue = (int) PHPExcel_Shared_Date::ExcelToPHP($numValue);
  1169.                                 }
  1170.                             }
  1171.                             //$sheet->setCellValueByColumnAndRow($colFirst + $i, $row + 1, $string);
  1172.                             $sheet->setCellValueByColumnAndRow($colFirst + $i, $row + 1, $numValue);
  1173.                             $tmppos += 6;
  1174.                         }
  1175.                         break;
  1176.  
  1177.                     case self::XLS_Type_NUMBER:
  1178.                         /**
  1179.                          * NUMBER
  1180.                          *
  1181.                          * This record represents a cell that contains a
  1182.                          * floating-point value.
  1183.                          *
  1184.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1185.                          *         Excel File Format"
  1186.                          */
  1187.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1188.                         $column = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1189.                         // offset 4; size: 2; index to XF record
  1190.                         $xfindex = $this->_GetInt2d($recordData, 4);
  1191.  
  1192.                         $numValue = $this->_createNumber($spos);
  1193.                         /*
  1194.                         if ($this->_isDate($spos)) {
  1195.                             $numValue = $this->_createNumber($spos);
  1196.                             list($string, $raw) = $this->_createDate($numValue);
  1197.                         } else {
  1198.                             if (isset($this->_columnsFormat[$column + 1])) {
  1199.                                 $this->_curformat = $this->_columnsFormat[$column + 1];
  1200.                             }
  1201.                             $raw = $this->_createNumber($spos);
  1202.                             $string = sprintf($this->_curformat, $raw * $this->_multiplier);
  1203.                         }
  1204.                         */
  1205.                         // add BIFF8 style information
  1206.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1207.                             $sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1208.                             if (PHPExcel_Shared_Date::isDateTimeFormatCode($this->_xf[$xfindex]['numberformat']['code'])) {
  1209.                                 $numValue = (int) PHPExcel_Shared_Date::ExcelToPHP($numValue);
  1210.                             }
  1211.                         }
  1212.                         //$this->_addcell($row, $column, $string, $raw);
  1213.                         //$sheet->setCellValueByColumnAndRow($column, $row + 1, $string);
  1214.                         $sheet->setCellValueByColumnAndRow($column, $row + 1, $numValue);
  1215.                         
  1216.                         break;
  1217.  
  1218.                     case self::XLS_Type_FORMULA:
  1219.                     case self::XLS_Type_FORMULA2:
  1220.                         /**
  1221.                          * FORMULA
  1222.                          *
  1223.                          * This record contains the token array and the result of a
  1224.                          * formula cell.
  1225.                          *
  1226.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1227.                          *         Excel File Format"
  1228.                          */
  1229.                         // offset: 0; size: 2; row index
  1230.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1231.                         // offset: 2; size: 2; col index
  1232.                         $column = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1233.                         // offset: 4; size: 2; XF index
  1234.                         $xfindex = ord($this->_data[$spos + 4]) | ord($this->_data[$spos + 5]) << 8;
  1235.  
  1236.                         // offset: 6; size: 8; result of the formula
  1237.                         if ((ord($this->_data[$spos + 6]) == 0) &&
  1238.                         (ord($this->_data[$spos + 12]) == 255) &&
  1239.                         (ord($this->_data[$spos + 13]) == 255)) {
  1240.                             //String formula. Result follows in appended STRING record
  1241.                             $this->_formula_result = 'string';
  1242.                             $soff = $spos + $length;
  1243.                             $scode = ord($this->_data[$soff]) | ord($this->_data[$soff + 1])<<8;
  1244.                             $sopt = ord($this->_data[$soff + 6]);
  1245.                             // only reads byte strings...
  1246.                             if ($scode == self::XLS_Type_STRING && $sopt == '0') {
  1247.                                 $slen = ord($this->_data[$soff + 4]) | ord($this->_data[$soff + 5]) << 8;
  1248.                                 $string = substr($this->_data, $soff + 7, ord($this->_data[$soff + 4]) | ord($this->_data[$soff + 5]) << 8);
  1249.                             } else {
  1250.                                 $string = 'NOT FOUND';
  1251.                             }
  1252.                             $raw = $string;
  1253.  
  1254.                         } elseif ((ord($this->_data[$spos + 6]) == 1) &&
  1255.                         (ord($this->_data[$spos + 12]) == 255) &&
  1256.                         (ord($this->_data[$spos + 13]) == 255)) {
  1257.                             //Boolean formula. Result is in +2; 0=false,1=true
  1258.                             $this->_formula_result = 'boolean';
  1259.                             $raw = ord($this->_data[$spos + 8]);
  1260.                             if ($raw) {
  1261.                                 $string = "TRUE";
  1262.                             } else {
  1263.                                 $string = "FALSE";
  1264.                             }
  1265.  
  1266.                         } elseif ((ord($this->_data[$spos + 6]) == 2) &&
  1267.                         (ord($this->_data[$spos + 12]) == 255) &&
  1268.                         (ord($this->_data[$spos + 13]) == 255)) {
  1269.                             //Error formula. Error code is in +2
  1270.                             $this->_formula_result = 'error';
  1271.                             $raw = ord($this->_data[$spos + 8]);
  1272.                             $string = 'ERROR:'.$raw;
  1273.  
  1274.                         } elseif ((ord($this->_data[$spos + 6]) == 3) &&
  1275.                         (ord($this->_data[$spos + 12]) == 255) &&
  1276.                         (ord($this->_data[$spos + 13]) == 255)) {
  1277.                             //Formula result is a null string
  1278.                             $this->_formula_result = 'null';
  1279.                             $raw = '';
  1280.                             $string = '';
  1281.  
  1282.                         } else {
  1283.                             // forumla result is a number, first 14 bytes like _NUMBER record
  1284.                             $string = $this->_createNumber($spos);
  1285.                             /*
  1286.                             $this->_formula_result = 'number';
  1287.                             if ($this->_isDate($spos)) {
  1288.                                 $numValue = $this->_createNumber($spos);
  1289.                                 list($string, $raw) = $this->_createDate($numValue);
  1290.                             } else {
  1291.                                 if (isset($this->_columnsFormat[$column + 1])){
  1292.                                     $this->_curformat = $this->_columnsFormat[$column + 1];
  1293.                                 }
  1294.                                 $raw = $this->_createNumber($spos);
  1295.                                 $string = sprintf($this->_curformat, $raw * $this->_multiplier);
  1296.                             }
  1297.                             */
  1298.                         }
  1299.                         // save the raw formula tokens for end user interpretation
  1300.                         // Excel stores as a token record
  1301.                         $this->_rectype = 'formula';
  1302.                         // read formula record tokens ...
  1303.                         $tokenlength = ord($this->_data[$spos + 20]) | ord($this->_data[$spos + 21]) << 8;
  1304.                         for ($i = 0; $i < $tokenlength; $i++) {
  1305.                             $this->_formula[$i] = ord($this->_data[$spos + 22 + $i]);
  1306.                         }
  1307.                         // add BIFF8 style information
  1308.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1309.                             $sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1310.                             if (PHPExcel_Shared_Date::isDateTimeFormatCode($this->_xf[$xfindex]['numberformat']['code'])) {
  1311.                                 $string = (int) PHPExcel_Shared_Date::ExcelToPHP($string);
  1312.                             }
  1313.                         }
  1314.                         //$this->_addcell($row, $column, $string, $raw);
  1315.                         $sheet->setCellValueByColumnAndRow($column, $row + 1, $string);
  1316.                         
  1317.                         // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
  1318.                         // offset: 16: size: 4; not used
  1319.                         // offset: 20: size: variable; formula structure
  1320.                         // WORK IN PROGRESS: TRUE FORMULA SUPPORT
  1321.                         //   resolve BIFF8 formula tokens into human readable formula
  1322.                         //   so it can be added as formula
  1323.                         // $formulaStructure = substr($recordData, 20);
  1324.                         // $formulaString = $this->_getFormulaStringFromStructure($formulaStructure); // get human language
  1325.                         break;
  1326.  
  1327.                     case self::XLS_Type_BOOLERR:
  1328.                         /**
  1329.                          * BOOLERR
  1330.                          *
  1331.                          * This record represents a Boolean value or error value
  1332.                          * cell.
  1333.                          *
  1334.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1335.                          *         Excel File Format"
  1336.                          */
  1337.                         // offset: 0; size: 2; row index
  1338.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1339.                         // offset: 2; size: 2; column index
  1340.                         $column = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1341.                         // offset: 4; size: 2; index to XF record
  1342.                         $xfindex = $this->_GetInt2d($recordData, 4);
  1343.                         // offset: 6; size: 1; the boolean value or error value
  1344.                         $value = ord($recordData[6]);
  1345.                         // offset: 7; size: 1; 0=boolean; 1=error
  1346.                         $isError = ord($recordData[7]);
  1347.                         if (!$isError) {
  1348.                             $sheet->getCellByColumnAndRow($column, $row + 1)->setValueExplicit((bool) $value, PHPExcel_Cell_DataType::TYPE_BOOL);
  1349.                         }
  1350.                         // add BIFF8 style information
  1351.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1352.                             $sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1353.                         }
  1354.                         break;
  1355.  
  1356.                     case self::XLS_Type_ROW:
  1357.                         /**
  1358.                          * ROW
  1359.                          *
  1360.                          * This record contains the properties of a single row in a
  1361.                          * sheet. Rows and cells in a sheet are divided into blocks
  1362.                          * of 32 rows.
  1363.                          *
  1364.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1365.                          *         Excel File Format"
  1366.                          */
  1367.                         if (!$this->_readDataOnly) {
  1368.                             // offset: 0; size: 2; index of this row
  1369.                             $r = $this->_GetInt2d($recordData, 0);
  1370.                             // offset: 2; size: 2; index to column of the first cell which is described by a cell record
  1371.                             // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
  1372.                             // offset: 6; size: 2;
  1373.                                 // bit: 14-0; mask: 0x7FF; height of the row, in twips = 1/20 of a point
  1374.                                 $height = (0x7FF & $this->_GetInt2d($recordData, 6)) >> 0;
  1375.                                 // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
  1376.                                 $useDefaultHeight = (0x8000 & $this->_GetInt2d($recordData, 6)) >> 15;
  1377.                                 
  1378.                                 if (!$useDefaultHeight) {
  1379.                                     $sheet->getRowDimension($r + 1)->setRowHeight($height / 20);
  1380.                                 }
  1381.                             // offset: 8; size: 2; not used
  1382.                             // offset: 10; size: 2; not used in BIFF5-BIFF8
  1383.                             // offset: 12; size: 4; option flags and default row formatting
  1384.                                 // bit: 2-0: mask: 0x00000007; outline level of the row
  1385.                                 $level = (0x00000007 & $this->_GetInt4d($recordData, 12)) >> 0;
  1386.                                 $sheet->getRowDimension($r + 1)->setOutlineLevel($level);
  1387.                                 // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
  1388.                                 $isCollapsed = (0x00000010 & $this->_GetInt4d($recordData, 12)) >> 4;
  1389.                                 $sheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
  1390.                                 // bit: 5; mask: 0x00000020; 1 = row is hidden
  1391.                                 $isHidden = (0x00000020 & $this->_GetInt4d($recordData, 12)) >> 5;
  1392.                                 $sheet->getRowDimension($r + 1)->setVisible(!$isHidden);
  1393.                         }
  1394.                         break;
  1395.                         
  1396.                     case self::XLS_Type_DBCELL:
  1397.                         /**
  1398.                          * DBCELL
  1399.                          *
  1400.                          * This record is written once in a Row Block. It contains
  1401.                          * relative offsets to calculate the stream position of the
  1402.                          * first cell record for each row. The offset list in this
  1403.                          * record contains as many offsets as ROW records are
  1404.                          * present in the Row Block.
  1405.                          *
  1406.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1407.                          *         Excel File Format"
  1408.                          */
  1409.                         break;
  1410.                         
  1411.                     case self::XLS_Type_MULBLANK:
  1412.                         /**
  1413.                          * MULBLANK - Multiple BLANK
  1414.                          *
  1415.                          * This record represents a cell range of empty cells. All
  1416.                          * cells are located in the same row
  1417.                          *
  1418.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1419.                          *         Excel File Format"
  1420.                          */
  1421.                         // offset: 0; size: 2; index to row
  1422.                         $row = $this->_GetInt2d($recordData, 0);
  1423.                         // offset: 2; size: 2; index to first column
  1424.                         $fc = $this->_GetInt2d($recordData, 2);
  1425.                         // offset: 4; size: 2 x nc; list of indexes to XF records
  1426.                         // add BIFF8 style information
  1427.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1428.                             for ($i = 0; $i < $length / 2 - 4; $i++) {
  1429.                                 $xfindex = $this->_GetInt2d($recordData, 4 + 2 * $i);
  1430.                                 $sheet->getStyleByColumnAndRow($fc + $i, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1431.                             }
  1432.                         }
  1433.                         // offset: 6; size 2; index to last column (not needed)
  1434.                         
  1435.                         break;
  1436.  
  1437.                     case self::XLS_Type_LABEL:
  1438.                         /**
  1439.                          * LABEL
  1440.                          *
  1441.                          * This record represents a cell that contains a string. In
  1442.                          * BIFF8 it is usually replaced by the LABELSST record.
  1443.                          * Excel still uses this record, if it copies unformatted
  1444.                          * text cells to the clipboard.
  1445.                          *
  1446.                          * --    "OpenOffice.org's Documentation of the Microsoft
  1447.                          *         Excel File Format"
  1448.                          */
  1449.                         $row = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  1450.                         $column = ord($this->_data[$spos + 2]) | ord($this->_data[$spos + 3]) << 8;
  1451.                         /*
  1452.                         $this->_addcell($row, $column, substr($this->_data, $spos + 8,
  1453.                             ord($this->_data[$spos + 6]) | ord($this->_data[$spos + 7]) << 8));
  1454.                         */
  1455.                         $sheet->setCellValueByColumnAndRow($column, $row + 1, substr($this->_data, $spos + 8,
  1456.                             ord($this->_data[$spos + 6]) | ord($this->_data[$spos + 7]) << 8));
  1457.                         break;
  1458.                         
  1459.                     case self::XLS_Type_PROTECT:
  1460.                         /**
  1461.                          * PROTECT - Sheet protection (BIFF2 through BIFF8)
  1462.                          *   if this record is omitted, then it also means no sheet protection
  1463.                          */
  1464.                         if (!$this->_readDataOnly) {
  1465.                             // offset: 0; size: 2;
  1466.                             // bit 0, mask 0x01; sheet protection
  1467.                             $isSheetProtected = (0x01 & $this->_GetInt2d($recordData, 0)) >> 0;
  1468.                             switch ($isSheetProtected) {
  1469.                                 case 0: break;
  1470.                                 case 1: $sheet->getProtection()->setSheet(true); break;
  1471.                             }
  1472.                         }
  1473.                         break;
  1474.                         
  1475.                     case self::XLS_Type_PASSWORD:
  1476.                         /**
  1477.                          * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
  1478.                          */
  1479.                         if (!$this->_readDataOnly) {
  1480.                             // offset: 0; size: 2; 16-bit hash value of password
  1481.                             $password = strtoupper(dechex($this->_GetInt2d($recordData, 0))); // the hashed password
  1482.                             $sheet->getProtection()->setPassword($password, true);
  1483.                         }
  1484.                         break;
  1485.                         
  1486.                     case self::XLS_Type_COLINFO:
  1487.                         /**
  1488.                          * COLINFO - Column information
  1489.                          */
  1490.                         if (!$this->_readDataOnly) {
  1491.                             // offset: 0; size: 2; index to first column in range
  1492.                             $fc = $this->_GetInt2d($recordData, 0); // first column index
  1493.                             // offset: 2; size: 2; index to last column in range
  1494.                             $lc = $this->_GetInt2d($recordData, 2); // first column index
  1495.                             // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
  1496.                             $width = $this->_GetInt2d($recordData, 4);
  1497.                             
  1498.                             // offset: 6; size: 2; index to XF record for default column formatting
  1499.                             
  1500.                             // offset: 8; size: 2; option flags
  1501.                                 // bit: 0; mask: 0x0001; 1= columns are hidden
  1502.                                 $isHidden = (0x0001 & $this->_GetInt2d($recordData, 8)) >> 0;
  1503.                                 // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
  1504.                                 $level = (0x0700 & $this->_GetInt2d($recordData, 8)) >> 8;
  1505.                                 // bit: 12; mask: 0x1000; 1 = collapsed
  1506.                                 $isCollapsed = (0x1000 & $this->_GetInt2d($recordData, 8)) >> 12;
  1507.                             
  1508.                             // offset: 10; size: 2; not used
  1509.                             
  1510.                             for ($i = $fc; $i <= $lc; $i++) {
  1511.                                 $sheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
  1512.                                 $sheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
  1513.                                 $sheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  1514.                                 $sheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
  1515.                             }
  1516.                         }
  1517.                         break;
  1518.                         
  1519.                     case self::XLS_Type_DEFCOLWIDTH:
  1520.                         // offset: 0; size: 2; row index
  1521.                         $width = $this->_GetInt2d($recordData, 0);
  1522.                         $sheet->getDefaultColumnDimension()->setWidth($width);
  1523.                         break;
  1524.                         
  1525.                     case self::XLS_Type_DEFAULTROWHEIGHT:
  1526.                         // offset: 0; size: 2; option flags
  1527.                         // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
  1528.                         $height = $this->_GetInt2d($recordData, 2);
  1529.                         $sheet->getDefaultRowDimension()->setRowHeight($height / 20);
  1530.                         break;
  1531.                         
  1532.                     case self::XLS_Type_BLANK:
  1533.                         // offset: 0; size: 2; row index
  1534.                         $row = $this->_GetInt2d($recordData, 0);
  1535.                         // offset: 2; size: 2; col index
  1536.                         $col = $this->_GetInt2d($recordData, 2);
  1537.                         // offset: 4; size: 2; XF index
  1538.                         $xfindex = $this->_GetInt2d($recordData, 4);
  1539.                         
  1540.                         // add BIFF8 style information
  1541.                         if ($version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1542.                             $sheet->getStyleByColumnAndRow($col, $row + 1)->applyFromArray($this->_xf[$xfindex]);
  1543.                         }
  1544.                         break;
  1545.                         
  1546.                     case self::XLS_Type_SHEETPR:
  1547.                         // offset: 0; size: 2
  1548.                         // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
  1549.                         $isSummaryBelow = (0x0040 & $this->_GetInt2d($recordData, 0)) >> 6;
  1550.                         $sheet->setShowSummaryBelow($isSummaryBelow);
  1551.                         // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
  1552.                         $isSummaryRight = (0x0080 & $this->_GetInt2d($recordData, 0)) >> 7;
  1553.                         $sheet->setShowSummaryRight($isSummaryRight);
  1554.                         break;
  1555.                     
  1556.                     case self::XLS_Type_EOF:
  1557.                         $cont = false;
  1558.                         break;
  1559.  
  1560.                     default:
  1561.                         break;
  1562.                 }
  1563.                 $spos += $length;
  1564.             }
  1565.             if (!isset($this->_sheets[$this->_sn]['numRows'])){
  1566.                 $this->_sheets[$this->_sn]['numRows'] = $this->_sheets[$this->_sn]['maxrow'];
  1567.             }
  1568.             if (!isset($this->_sheets[$this->_sn]['numCols'])){
  1569.                 $this->_sheets[$this->_sn]['numCols'] = $this->_sheets[$this->_sn]['maxcol'];
  1570.             }
  1571.         }
  1572.         
  1573.  
  1574.         /*
  1575.         foreach($this->_boundsheets as $index => $details) {
  1576.             $sheet = $excel->getSheet($index);
  1577.  
  1578.             // read all the columns of all the rows !
  1579.             $numrows = $this->_sheets[$index]['numRows'];
  1580.             $numcols = $this->_sheets[$index]['numCols'];
  1581.             for ($row = 0; $row < $numrows; $row++) {
  1582.                 for ($col = 0; $col < $numcols; $col++) {
  1583.                     $cellcontent = $cellinfo = null;
  1584.                     if (isset($this->_sheets[$index]['cells'][$row][$col])===true) {
  1585.                         $cellcontent = $this->_sheets[$index]['cells'][$row][$col];
  1586.                     } else {
  1587.                         continue;
  1588.                     }
  1589.                     
  1590.                     if (isset($this->_sheets[$index]['cellsInfo'][$row][$col])===true) {
  1591.                         $cellinfo = $this->_sheets[$index]['cellsInfo'][$row][$col];
  1592.                     }
  1593.  
  1594.                     $sheet->setCellValueByColumnAndRow($col, $row + 1,
  1595.                         $cellcontent);
  1596.                 }
  1597.             }
  1598.         };
  1599.         */
  1600.         return $excel;
  1601.     }
  1602.  
  1603.     // set $encoder for method of UTF-16LE encoding
  1604.     private function _setUTFEncoder($encoder = 'iconv')
  1605.     {
  1606.         $this->_encoderFunction = '';
  1607.         if ($encoder == 'iconv') {
  1608.             $this->_encoderFunction = function_exists('iconv') ? 'iconv' : 'mb_convert_encoding';
  1609.  
  1610.         } elseif ($encoder == 'mb') {
  1611.  
  1612.         }
  1613.     }
  1614.     
  1615.     private function _isDate($spos)
  1616.     {
  1617.         $xfindex = ord($this->_data[$spos + 4]) | ord($this->_data[$spos + 5]) << 8;
  1618.         $this->_curformat = $this->_formatRecords['xfrecords'][$xfindex]['format'];
  1619.         $this->_fmtcode = $this->_formatRecords['xfrecords'][$xfindex]['code'];
  1620.  
  1621.         if ($this->_formatRecords['xfrecords'][$xfindex]['type'] == 'date') {
  1622.             $this->_rectype = 'date';
  1623.             return true;
  1624.  
  1625.         } else if (($xfindex == 0x9) || ($xfindex == 0xa) || ($this->_formatRecords['xfrecords'][$xfindex]['type'] == 'percent')) {
  1626.             $this->_rectype = 'number';
  1627.             $this->_multiplier = 100;
  1628.             }
  1629.  
  1630.         else if ($this->_formatRecords['xfrecords'][$xfindex]['type'] == 'number') {
  1631.             $this->_rectype = 'number';
  1632.  
  1633.         } else {
  1634.             $this->_rectype = 'unknown';
  1635.         }
  1636.         return false;
  1637.     }
  1638.  
  1639.     private function _createDate($numValue)
  1640.     {
  1641.         if ($numValue > 1){
  1642.             $utcDays = $numValue - ($this->_nineteenFour ? self::XLS_utcOffsetDays1904 : self::XLS_utcOffsetDays);
  1643.             $utcValue = round(($utcDays * self::XLS_SecInADay));
  1644.             // dvc: excel returns local date/time as absolutes,
  1645.             // i.e. 1 hr = 0.04166, 1 day = 1,
  1646.             // so need to treat as GMT to translate
  1647.             $string = gmdate ($this->_curformat, $utcValue);
  1648.             $raw = $utcValue;
  1649.         } else {
  1650.             // assume a time format...
  1651.             $raw = $numValue;
  1652.             $hours = round($numValue * 24);
  1653.             $mins = round($numValue * 24*60) - $hours * 60;
  1654.             $secs = round($numValue * self::XLS_SecInADay) - $hours *60*60 - $mins * 60;
  1655.             $string = date ($this->_curformat, mktime($hours, $mins, $secs));
  1656.         }
  1657.         return array($string, $raw);
  1658.     }
  1659.  
  1660.     /**
  1661.      * Reads 8 bytes and returns IEEE 754 float
  1662.      */
  1663.     private function _createNumber($spos)
  1664.     {
  1665.         $rknumhigh = $this->_GetInt4d($this->_data, $spos + 10);
  1666.         $rknumlow = $this->_GetInt4d($this->_data, $spos + 6);
  1667.         $sign = ($rknumhigh & 0x80000000) >> 31;
  1668.         $exp = ($rknumhigh & 0x7ff00000) >> 20;
  1669.         $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  1670.         $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  1671.         $mantissalow2 = ($rknumlow & 0x7fffffff);
  1672.         $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1673.  
  1674.         if ($mantissalow1 != 0) {
  1675.             $value += 1 / pow (2 , (21 - ($exp - 1023)));
  1676.         }
  1677.  
  1678.         $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  1679.         if ($sign) {
  1680.             $value = -1 * $value;
  1681.         }
  1682.  
  1683.         return    $value;
  1684.     }
  1685.  
  1686.     /**
  1687.      * Same as _createNumber, but not hardcoded to read from $this->_data
  1688.      */
  1689.     private function _readNumber($subData)
  1690.     {
  1691.         $rknumhigh = $this->_GetInt4d($subData, 4);
  1692.         $rknumlow = $this->_GetInt4d($subData, 0);
  1693.         $sign = ($rknumhigh & 0x80000000) >> 31;
  1694.         $exp = ($rknumhigh & 0x7ff00000) >> 20;
  1695.         $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  1696.         $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  1697.         $mantissalow2 = ($rknumlow & 0x7fffffff);
  1698.         $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1699.  
  1700.         if ($mantissalow1 != 0) {
  1701.             $value += 1 / pow (2 , (21 - ($exp - 1023)));
  1702.         }
  1703.  
  1704.         $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  1705.         if ($sign) {
  1706.             $value = -1 * $value;
  1707.         }
  1708.  
  1709.         return    $value;
  1710.     }
  1711.  
  1712.     /*
  1713.     private function _addcell($row, $col, $string, $raw = '')
  1714.     {
  1715.         $this->_sheets[$this->_sn]['maxrow'] =
  1716.             max($this->_sheets[$this->_sn]['maxrow'], $row + $this->_rowoffset);
  1717.         $this->_sheets[$this->_sn]['maxcol'] =
  1718.             max($this->_sheets[$this->_sn]['maxcol'], $col + $this->_coloffset);
  1719.         $this->_sheets[$this->_sn]['cells'][$row +
  1720.             $this->_rowoffset][$col + $this->_coloffset] = $string;
  1721.  
  1722.         if ($raw) {
  1723.             $this->_sheets[$this->_sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw;
  1724.         }
  1725.  
  1726.         if (isset($this->_rectype)) {
  1727.             $this->_sheets[$this->_sn]['cellsInfo'][$row +
  1728.                 $this->_rowoffset][$col + $this->_coloffset]['type'] =
  1729.                     $this->_rectype;
  1730.  
  1731.             if (isset($this->_curformat)) {
  1732.                 $this->_sheets[$this->_sn]['cellsInfo'][$row +
  1733.                     $this->_rowoffset][$col + $this->_coloffset]['format'] =
  1734.                         $this->_curformat;
  1735.             }
  1736.  
  1737.             if (isset($this->_fmtcode)) {
  1738.                 $this->_sheets[$this->_sn]['cellsInfo'][$row +
  1739.                     $this->_rowoffset][$col + $this->_coloffset]['code'] =
  1740.                         $this->_fmtcode;
  1741.             }
  1742.  
  1743.             if (isset($this->_formula)) {
  1744.                 $this->_sheets[$this->_sn]['cellsInfo'][$row +
  1745.                     $this->_rowoffset][$col + $this->_coloffset]['formula_tokens'] =
  1746.                         $this->_formula;
  1747.             }
  1748.  
  1749.             if (isset($this->_formula_result)) {
  1750.                 $this->_sheets[$this->_sn]['cellsInfo'][$row +
  1751.                     $this->_rowoffset][$col + $this->_coloffset]['formula_result'] =
  1752.                         $this->_formula_result;
  1753.             }
  1754.         }
  1755.     }
  1756.     */
  1757.  
  1758.     private function _GetIEEE754($rknum)
  1759.     {
  1760.         if (($rknum & 0x02) != 0) {
  1761.             $value = $rknum >> 2;
  1762.         }
  1763.         else {
  1764.             // changes by mmp, info on IEEE754 encoding from
  1765.             // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  1766.             // The RK format calls for using only the most significant 30 bits
  1767.             // of the 64 bit floating point value. The other 34 bits are assumed
  1768.             // to be 0 so we use the upper 30 bits of $rknum as follows...
  1769.             $sign = ($rknum & 0x80000000) >> 31;
  1770.             $exp = ($rknum & 0x7ff00000) >> 20;
  1771.             $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  1772.             $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1773.             if ($sign) {
  1774.                 $value = -1 * $value;
  1775.             }
  1776.             //end of changes by mmp
  1777.         }
  1778.         if (($rknum & 0x01) != 0) {
  1779.             $value /= 100;
  1780.         }
  1781.         return $value;
  1782.     }
  1783.  
  1784.     // returns UTF-8 string from UTF-16 string either compressed or uncompressed
  1785.     private function _encodeUTF16($string, $compressed = '')
  1786.     {
  1787.         $result = $string;
  1788.         //if ($this->_defaultEncoding) {
  1789.         //    if($compressed) {
  1790.         //        $string = $this->_uncompressByteString($string);
  1791.         //    }
  1792.         //    switch ($this->_encoderFunction){
  1793.         //        case 'iconv' :
  1794.         //            $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
  1795.         //            break;
  1796.         //        case 'mb_convert_encoding' :
  1797.         //            $result = mb_convert_encoding($string, $this->_defaultEncoding,
  1798.         //                'UTF-16LE' );
  1799.         //            break;
  1800.         //    }
  1801.         //}
  1802.         if($compressed) {
  1803.             $string = $this->_uncompressByteString($string);
  1804.          }
  1805.         switch ($this->_encoderFunction){
  1806.             case 'iconv' :
  1807.                 $result = iconv('UTF-16LE', 'UTF-8', $string);
  1808.                 break;
  1809.             case 'mb_convert_encoding' :
  1810.                 $result = mb_convert_encoding($string, 'UTF-8', 'UTF-16LE');
  1811.                 break;
  1812.         }
  1813.         return $result;
  1814.     }
  1815.  
  1816.     private function _GetInt2d($data, $pos)
  1817.     {
  1818.         return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
  1819.     }
  1820.  
  1821.     private function _GetInt4d($data, $pos)
  1822.     {
  1823.         return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) |
  1824.             (ord($data[$pos + 2]) << 16) | (ord($data[$pos + 3]) << 24);
  1825.     }
  1826.  
  1827.     private function _uncompressByteString($string)
  1828.     {
  1829.         $uncompressedString = "";
  1830.         for($i = 0; $i < strlen($string); $i++) {
  1831.             $uncompressedString .= $string[$i]."\0";
  1832.         }
  1833.  
  1834.         return $uncompressedString;
  1835.     }
  1836.  
  1837.     private function _decodeCodepage($string)
  1838.     {
  1839.         $result = $string;
  1840.         if ($this->_defaultEncoding && $this->_codepage) {
  1841.             switch ($this->_encoderFunction) {
  1842.                 case 'iconv' :
  1843.                     $result = iconv($this->_codepage,$this->_defaultEncoding,$string);
  1844.                     break;
  1845.                 case 'mb_convert_encoding' :
  1846.                     $result = mb_convert_encoding($string, $this->_defaultEncoding, $this->_codepage );
  1847.                     break;
  1848.             }
  1849.         }
  1850.         return $result;
  1851.     }
  1852.     
  1853.     /**
  1854.      * Get numberFormat from index
  1855.      * returns false if we couldn't resolve numberFormat
  1856.      */
  1857.     private function _readBIFF8NumberFormat($numberFormatIndex)
  1858.     {
  1859.         // there are two possibilites
  1860.         // A. numberFormatIndex refers to a built-in numberformat and will not be found in the numberFormat array
  1861.         //        instead, it is omitted, and we have to determine numberFormat based on table look-up
  1862.         // B. numberFormatIndex refers to a user-defined numberformat and will be found in the numberFormat array
  1863.         switch ($numberFormatIndex) {
  1864.         case  0: $code = PHPExcel_Style_NumberFormat::FORMAT_GENERAL; break;
  1865.         case  1: $code = PHPExcel_Style_NumberFormat::FORMAT_NUMBER; break;
  1866.         case  2: $code = PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00; break;
  1867.         case  3: $code = '#,##0'; break;
  1868.         case  4: $code = PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1; break;
  1869.         // case 5 is omitted since it is always written by Excel, depend on locale;
  1870.         // case 6 is omitted since it is always written by Excel, depend on locale;
  1871.         // case 7 is omitted since it is always written by Excel, depend on locale;
  1872.         // case 8 is omitted since it is always written by Excel, depend on locale;
  1873.         case  9: $code = PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE; break;
  1874.         case 10: $code = PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00; break;
  1875.         case 11: $code = '0.00E+00'; break;
  1876.         case 12: $code = '# ?/?'; break;
  1877.         case 13: $code = '# ??/??'; break;
  1878.         case 14: $code = 'M/D/YY'; break; // not always correct, but the best we can do: taken from Microsoft Windws regional settings
  1879.         case 15: $code = 'D-MMM-YY'; break;
  1880.         case 16: $code = 'D-MMM'; break;
  1881.         case 17: $code = 'MMM-YY'; break;
  1882.         case 18: $code = 'h:mm AM/PM'; break;
  1883.         case 19: $code = 'h:mm:ss AM/PM'; break;
  1884.         case 20: $code = 'h:mm'; break;
  1885.         case 21: $code = 'h:mm:ss'; break;
  1886.         case 22: $code = 'M/D/YY h:mm'; break; // not always correct, but the best we can do: taken from Microsoft Windws regional settings
  1887.         case 37: $code = '_(#,##0_);(#,##0)'; break;
  1888.         case 38: $code = '_(#,##0_);[Red(#,##0)]'; break;
  1889.         case 39: $code = '_(#,##0.00_);(#,##0.00)'; break;
  1890.         // case 41 is omitted since it is always written by Excel, depend on locale;
  1891.         // case 42 is omitted since it is always written by Excel, depend on locale;
  1892.         // case 43 is omitted since it is always written by Excel, depend on locale;
  1893.         // case 44 is omitted since it is always written by Excel, depend on locale;
  1894.         case 45: $code = 'mm:ss'; break;
  1895.         case 46: $code = '[h]:mm:ss'; break;
  1896.         case 47: $code = 'mm:ss.0'; break;
  1897.         case 48: $code = '##0.0E+0'; break;
  1898.         case 49: $code = '@'; break;
  1899.         default:
  1900.             // check for user-defined number format
  1901.             $code = isset($this->_numberFormat[$numberFormatIndex]) ?
  1902.                 $this->_numberFormat[$numberFormatIndex] : PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  1903.             break;
  1904.         }
  1905.         return array('code' => $code);
  1906.     }
  1907.     
  1908.     /**
  1909.      * Extracts style from XF record data (not everything is extracted at the moment)
  1910.      */
  1911.     private function _readBIFF8Style($recordData)
  1912.     {
  1913.         $style = array();
  1914.         
  1915.         // offset:  0; size: 2; Index to FONT record
  1916.         if ($this->_GetInt2d($recordData, 0) < 4) {
  1917.             $fontIndex = $this->_GetInt2d($recordData, 0);
  1918.         } else {
  1919.             // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1920.             // check the OpenOffice documentation of the FONT record
  1921.             $fontIndex = $this->_GetInt2d($recordData, 0) - 1;
  1922.         }
  1923.         $style['font'] = $this->_font[$fontIndex];
  1924.         
  1925.         // offset:  2; size: 2; Index to FORMAT record
  1926.             $numberFormatIndex = $this->_GetInt2d($recordData, 2);
  1927.             $style['numberformat'] = $this->_readBIFF8NumberFormat($numberFormatIndex);
  1928.         
  1929.         $style['protection'] = array();
  1930.         // offset:  4; size: 2; XF type, cell protection, and parent style XF
  1931.             // bit 2-0; mask 0x0007; XF_TYPE_PROT
  1932.             $xfTypeProt = $this->_GetInt2d($recordData, 4);
  1933.                 // bit 0; mask 0x01; 1 = cell is locked
  1934.                 $isLocked = (0x01 & $xfTypeProt) >> 0;
  1935.                 $style['protection']['locked'] = $isLocked ? true : false;
  1936.                 // bit 1;
  1937.                 // bit 2;
  1938.         
  1939.         // offset:  6; size: 1; Alignment and text break
  1940.         $style['alignment'] = array();
  1941.             // bit 2-0, mask 0x07; horizontal alignment
  1942.             $horAlign = (0x07 & ord($recordData[6])) >> 0;
  1943.             switch ($horAlign) {
  1944.                 case 0: $style['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL; break;
  1945.                 case 1: $style['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_LEFT; break;
  1946.                 case 2: $style['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_CENTER; break;
  1947.                 case 3: $style['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT; break;
  1948.                 case 5: $style['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY; break;
  1949.             }
  1950.             // bit 3, mask 0x08; wrap text
  1951.             $wrapText = (0x08 & ord($recordData[6])) >> 3;
  1952.             switch ($wrapText) {
  1953.                 case 0: $style['alignment']['wrap'] = false; break;
  1954.                 case 1: $style['alignment']['wrap'] = true; break;
  1955.             }
  1956.             // bit 6-4, mask 0x70; vertical alignment
  1957.             $vertAlign = (0x70 & ord($recordData[6])) >> 4;
  1958.             switch ($vertAlign) {
  1959.                 case 0: $style['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_TOP; break;
  1960.                 case 1: $style['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_CENTER; break;
  1961.                 case 2: $style['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_BOTTOM; break;
  1962.                 case 3: $style['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_JUSTIFY; break;
  1963.             }
  1964.         // offset:  7; size: 1; XF_ROTATION: Text rotation angle
  1965.         // offset:  8; size: 1; Indentation, shrink to cell size, and text direction
  1966.         // offset:  9; size: 1; Flags used for attribute groups
  1967.         
  1968.         $style['borders'] = array(
  1969.             'left' => array(),
  1970.             'right' => array(),
  1971.             'top' => array(),
  1972.             'bottom' => array(),
  1973.         );
  1974.         // offset: 10; size: 4; Cell border lines and background area
  1975.             // bit: 3-0; mask: 0x0000000F; left style
  1976.             if ($bordersLeftStyle = $this->_readBorderStyle((0x0000000F & $this->_GetInt4d($recordData, 10)) >> 0)) {
  1977.                 $style['borders']['left']['style'] = $bordersLeftStyle;
  1978.             }
  1979.             // bit: 7-4; mask: 0x000000F0; right style
  1980.             if ($bordersRightStyle = $this->_readBorderStyle((0x000000F0 & $this->_GetInt4d($recordData, 10)) >> 4)) {
  1981.                 $style['borders']['right']['style'] = $bordersRightStyle;
  1982.             }
  1983.             // bit: 11-8; mask: 0x00000F00; top style
  1984.             if ($bordersTopStyle = $this->_readBorderStyle((0x00000F00 & $this->_GetInt4d($recordData, 10)) >> 8)) {
  1985.                 $style['borders']['top']['style'] = $bordersTopStyle;
  1986.             }
  1987.             // bit: 15-12; mask: 0x0000F000; bottom style
  1988.             if ($bordersBottomStyle = $this->_readBorderStyle((0x0000F000 & $this->_GetInt4d($recordData, 10)) >> 12)) {
  1989.                 $style['borders']['bottom']['style'] = $bordersBottomStyle;
  1990.             }
  1991.             // bit: 22-16; mask: 0x007F0000; left color
  1992.             if ($bordersLeftColor = $this->_readColor((0x007F0000 & $this->_GetInt4d($recordData, 10)) >> 16)) {
  1993.                 $style['borders']['left']['color'] = $bordersLeftColor;
  1994.             }
  1995.             // bit: 29-23; mask: 0x3F800000; right color
  1996.             if ($bordersRightColor = $this->_readColor((0x3F800000 & $this->_GetInt4d($recordData, 10)) >> 23)) {
  1997.                 $style['borders']['right']['color'] = $bordersRightColor;
  1998.             }
  1999.             
  2000.         $style['fill'] = array();
  2001.         // offset: 14; size: 4;
  2002.             // bit: 6-0; mask: 0x0000007F; top color
  2003.             if ($bordersTopColor = $this->_readColor((0x0000007F & $this->_GetInt4d($recordData, 14)) >> 0)) {
  2004.                 $style['borders']['top']['color'] = $bordersTopColor;
  2005.             }
  2006.             // bit: 13-7; mask: 0x00003F80; top color
  2007.             if ($bordersBottomColor = $this->_readColor((0x00003F80 & $this->_GetInt4d($recordData, 14)) >> 7)) {
  2008.                 $style['borders']['bottom']['color'] = $bordersBottomColor;
  2009.             }
  2010.             // bit: 31-26; mask: 0xFC000000 fill pattern
  2011.             if ($fillType = $this->_getFillPatternByIndex((0xFC000000 & $this->_GetInt4d($recordData, 14)) >> 26)) {
  2012.                 $style['fill']['type'] = $fillType;
  2013.             }
  2014.         // offset: 18; size: 2; pattern and background colour
  2015.             // bit: 6-0; mask: 0x007F; color index for pattern color
  2016.             if ($rgb = $this->_readColor((0x007F & $this->_GetInt2d($recordData, 18)) >> 0)) {
  2017.                 $style['fill']['startcolor'] = $rgb;
  2018.             }
  2019.             // bit: 13-7; mask: 0x3F80; color index for pattern background
  2020.             if ($rgb = $this->_readColor((0x3F80 & $this->_GetInt2d($recordData, 18)) >> 7)) {
  2021.                 $style['fill']['endcolor'] = $rgb;
  2022.             }
  2023.         return $style;
  2024.     }
  2025.     
  2026.     /**
  2027.      * OpenOffice documentation: 2.5.11
  2028.      */
  2029.     private function _readBorderStyle($index)
  2030.     {
  2031.         switch ($index) {
  2032.         case 0x00: return PHPExcel_Style_Border::BORDER_NONE;
  2033.         case 0x01: return PHPExcel_Style_Border::BORDER_THIN;
  2034.         case 0x02: return PHPExcel_Style_Border::BORDER_MEDIUM;
  2035.         case 0x03: return PHPExcel_Style_Border::BORDER_DASHED;
  2036.         case 0x04: return PHPExcel_Style_Border::BORDER_DOTTED;
  2037.         case 0x05: return PHPExcel_Style_Border::BORDER_THICK;
  2038.         case 0x06: return PHPExcel_Style_Border::BORDER_DOUBLE;
  2039.         case 0x07: return PHPExcel_Style_Border::BORDER_HAIR;
  2040.         case 0x08: return PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
  2041.         case 0x09: return PHPExcel_Style_Border::BORDER_DASHDOT;
  2042.         case 0x0A: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
  2043.         case 0x0B: return PHPExcel_Style_Border::BORDER_DASHDOTDOT;
  2044.         case 0x0C: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  2045.         case 0x0D: return PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
  2046.         default: return false;
  2047.         }
  2048.     }
  2049.     
  2050.     /**
  2051.      * Get fill pattern from index
  2052.      * OpenOffice documentation: 2.5.12
  2053.      */
  2054.     private function _getFillPatternByIndex($index)
  2055.     {
  2056.         switch ($index) {
  2057.         case 0x00: return PHPExcel_Style_Fill::FILL_NONE;
  2058.         case 0x01: return PHPExcel_Style_Fill::FILL_SOLID;
  2059.         case 0x02: return PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
  2060.         case 0x03: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
  2061.         case 0x04: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
  2062.         case 0x05: return PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
  2063.         case 0x06: return PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
  2064.         case 0x07: return PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
  2065.         case 0x08: return PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
  2066.         case 0x09: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
  2067.         case 0x0A: return PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
  2068.         case 0x0B: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
  2069.         case 0x0C: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
  2070.         case 0x0D: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
  2071.         case 0x0E: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
  2072.         case 0x0F: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
  2073.         case 0x10: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
  2074.         case 0x11: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
  2075.         case 0x12: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
  2076.         default: return false;
  2077.         }
  2078.     }
  2079.     
  2080.     /**
  2081.      * Extracts font details from FONT record data (not everything is extracted yet)
  2082.      *
  2083.      **/
  2084.  
  2085.     private function _readFont($recordData)
  2086.     {
  2087.         $font = array();
  2088.         // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
  2089.         $size = $this->_GetInt2d($recordData, 0);
  2090.         $font['size'] = $size / 20;
  2091.         
  2092.         // offset: 2; size: 2; option flags
  2093.             // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
  2094.             // bit: 1; mask 0x0002; italic
  2095.             $isItalic = (0x0002 & $this->_GetInt2d($recordData, 2)) >> 1;
  2096.             if ($isItalic) $font['italic'] = true;
  2097.             
  2098.             // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
  2099.             // bit: 3; mask 0x0008; strike
  2100.             $isStrike = (0x0008 & $this->_GetInt2d($recordData, 2)) >> 3;
  2101.             if ($isStrike) $font['strike'] = true;
  2102.         
  2103.         // offset: 4; size: 2; colour index
  2104.         if ($color = $this->_readColor($this->_GetInt2d($recordData, 4))) {
  2105.             $font['color'] = $color;
  2106.         }
  2107.         
  2108.         // offset: 6; size: 2; font weight
  2109.         $weight = $this->_GetInt2d($recordData, 6);
  2110.         switch ($weight) {
  2111.             case 0x02BC: $font['bold'] = true;
  2112.         }
  2113.         
  2114.         // offset: 8; size: 2; escapement type
  2115.         
  2116.         // offset: 10; size: 1; underline type
  2117.         $underlineType = ord($recordData[10]);
  2118.         switch ($underlineType) {
  2119.             case 0x00: break; // no underline
  2120.             case 0x01: $font['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLE; break;
  2121.             case 0x02: $font['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLE; break;
  2122.             case 0x21: $font['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING; break;
  2123.             case 0x22: $font['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING; break;
  2124.         }
  2125.         
  2126.         // offset: 11; size: 1; font family
  2127.         // offset: 12; size: 1; character set
  2128.         // offset: 13; size: 1; not used
  2129.         // offset: 14; size: var; font name
  2130.         $unicode = $this->_readUnicodeStringShort(substr($recordData, 14));
  2131.         $font['name'] = $unicode['string'];
  2132.         
  2133.         return $font;
  2134.     }
  2135.     
  2136.     /**
  2137.      * Extracts an Excel Unicode short string (8-bit string length)
  2138.      * OpenOffice documentation: 2.5.3
  2139.      * this function is under construction, needs to support rich text, and Asian phonetic settings
  2140.      * function will automatically find out where the Unicode string ends.
  2141.      */
  2142.     private function _readUnicodeStringShort($subData)
  2143.     {
  2144.         $string = '';
  2145.         
  2146.         // offset: 0: size: 1; length of the string (character count)
  2147.         $characterCount = ord($subData[0]);
  2148.         // offset: 1: size: 1; option flags
  2149.             // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  2150.             $isCompressed = !((0x01 & ord($subData[1])) >> 0);
  2151.             
  2152.             // bit: 2; mask: 0x04; Asian phonetic settings
  2153.             $hasAsian = (0x04) & ord($subData[1]) >> 2;
  2154.             
  2155.             // bit: 3; mask: 0x08; Rich-Text settings
  2156.             $hasRichText = (0x08) & ord($subData[1]) >> 3;
  2157.             
  2158.         // offset: 2: size: var; character array
  2159.         // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  2160.         // needs to be fixed
  2161.         $string = $this->_encodeUTF16(substr($subData, 2, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
  2162.         
  2163.         return array(
  2164.             'size' => $isCompressed ? 2 + $characterCount : 2 + 2 * $characterCount, // the size in bytes including the length byte + option flags
  2165.             'string' => $string,
  2166.         );
  2167.     }
  2168.     
  2169.     /**
  2170.      * Extracts an Excel Unicode long string (16-bit string length)
  2171.      * OpenOffice documentation: 2.5.3
  2172.      * this function is under construction, needs to support rich text, and Asian phonetic settings
  2173.      *
  2174.      * this function should look like _readUnicodeStringShort and also return the size in bytes
  2175.      */
  2176.     private function _readUnicodeStringLong($subData)
  2177.     {
  2178.         $string = '';
  2179.         
  2180.         // offset: 0: size: 2; length of the string (character count)
  2181.         $characterCount = $this->_GetInt2d($subData, 0);
  2182.         // offset: 2: size: 1; option flags
  2183.             // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  2184.             $isCompressed = !((0x01 & ord($subData[2])) >> 0);
  2185.             
  2186.             // bit: 2; mask: 0x04; Asian phonetic settings
  2187.             $hasAsian = (0x04) & ord($subData[2]) >> 2;
  2188.             
  2189.             // bit: 3; mask: 0x08; Rich-Text settings
  2190.             $hasRichText = (0x08) & ord($subData[2]) >> 3;
  2191.             
  2192.         // offset: 3: size: var; character array
  2193.         // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  2194.         // needs to be fixed
  2195.         $string = $this->_encodeUTF16(substr($subData, 3), $isCompressed);
  2196.         
  2197.         return $string;
  2198.     }
  2199.     
  2200.     /**
  2201.      * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
  2202.      * section 3.3.4
  2203.      */
  2204.     private function _readBIFF8CellAddress($cellAddressStructure)
  2205.     {
  2206.         // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  2207.             $row = $this->_getInt2d($cellAddressStructure, 0) + 1;
  2208.             
  2209.         // offset: 2; size: 2; index to column or column offset + relative flags
  2210.             // bit: 7-0; mask 0x00FF; column index
  2211.             $column = PHPExcel_Cell::stringFromColumnIndex(0x00FF & $this->_getInt2d($cellAddressStructure, 2));
  2212.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  2213.             if (!(0x4000 & $this->_getInt2d($cellAddressStructure, 2))) {
  2214.                 $column = '$' . $column;
  2215.             }
  2216.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  2217.             if (!(0x8000 & $this->_getInt2d($cellAddressStructure, 2))) {
  2218.                 $row = '$' . $row;
  2219.             }
  2220.             
  2221.         
  2222.         return $column . $row;
  2223.     }
  2224.     
  2225.     /**
  2226.      * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
  2227.      * section 3.3.4
  2228.      */
  2229.     private function _readBIFF8CellRangeAddress($subData)
  2230.     {
  2231.         // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
  2232.             $fr = $this->_getInt2d($subData, 0) + 1;
  2233.         // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
  2234.             $lr = $this->_getInt2d($subData, 2) + 1;
  2235.         // offset: 4; size: 2; index to first column or column offset + relative flags
  2236.             // bit: 7-0; mask 0x00FF; column index
  2237.             $fc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & $this->_getInt2d($subData, 4));
  2238.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  2239.             if (!(0x4000 & $this->_getInt2d($subData, 4))) {
  2240.                 $fc = '$' . $fc;
  2241.             }
  2242.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  2243.             if (!(0x8000 & $this->_getInt2d($subData, 4))) {
  2244.                 $fr = '$' . $fr;
  2245.             }
  2246.         // offset: 6; size: 2; index to last column or column offset + relative flags
  2247.             // bit: 7-0; mask 0x00FF; column index
  2248.             $lc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & $this->_getInt2d($subData, 6));
  2249.             // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  2250.             if (!(0x4000 & $this->_getInt2d($subData, 6))) {
  2251.                 $lc = '$' . $lc;
  2252.             }
  2253.             // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  2254.             if (!(0x8000 & $this->_getInt2d($subData, 6))) {
  2255.                 $lr = '$' . $lr;
  2256.             }
  2257.             
  2258.         return "$fc$fr:$lc$lr";
  2259.     }
  2260.     
  2261.     /**
  2262.      * Extracts color array from BIFF8 built-in color index
  2263.      */
  2264.     private function _readColor($subData)
  2265.     {
  2266.         switch ($subData) {
  2267.             case 0x08: return array('rgb' => '000000');
  2268.             case 0x09: return array('rgb' => 'FFFFFF');
  2269.             case 0x0A: return array('rgb' => 'FF0000');
  2270.             case 0x0B: return array('rgb' => '00FF00');
  2271.             case 0x0C: return array('rgb' => '0000FF');
  2272.             case 0x0D: return array('rgb' => 'FFFF00');
  2273.             case 0x0E: return array('rgb' => 'FF00FF');
  2274.             case 0x0F: return array('rgb' => '00FFFF');
  2275.             case 0x10: return array('rgb' => '800000');
  2276.             case 0x11: return array('rgb' => '008000');
  2277.             case 0x12: return array('rgb' => '000080');
  2278.             case 0x13: return array('rgb' => '808000');
  2279.             case 0x14: return array('rgb' => '800080');
  2280.             case 0x15: return array('rgb' => '008080');
  2281.             case 0x16: return array('rgb' => 'C0C0C0');
  2282.             case 0x17: return array('rgb' => '808080');
  2283.             case 0x18: return array('rgb' => '9999FF');
  2284.             case 0x19: return array('rgb' => '993366');
  2285.             case 0x1A: return array('rgb' => 'FFFFCC');
  2286.             case 0x1B: return array('rgb' => 'CCFFFF');
  2287.             case 0x1C: return array('rgb' => '660066');
  2288.             case 0x1D: return array('rgb' => 'FF8080');
  2289.             case 0x1E: return array('rgb' => '0066CC');
  2290.             case 0x1F: return array('rgb' => 'CCCCFF');
  2291.             case 0x20: return array('rgb' => '000080');
  2292.             case 0x21: return array('rgb' => 'FF00FF');
  2293.             case 0x22: return array('rgb' => 'FFFF00');
  2294.             case 0x23: return array('rgb' => '00FFFF');
  2295.             case 0x24: return array('rgb' => '800080');
  2296.             case 0x25: return array('rgb' => '800000');
  2297.             case 0x26: return array('rgb' => '008080');
  2298.             case 0x27: return array('rgb' => '0000FF');
  2299.             case 0x28: return array('rgb' => '00CCFF');
  2300.             case 0x29: return array('rgb' => 'CCFFFF');
  2301.             case 0x2A: return array('rgb' => 'CCFFCC');
  2302.             case 0x2B: return array('rgb' => 'FFFF99');
  2303.             case 0x2C: return array('rgb' => '99CCFF');
  2304.             case 0x2D: return array('rgb' => 'FF99CC');
  2305.             case 0x2E: return array('rgb' => 'CC99FF');
  2306.             case 0x2F: return array('rgb' => 'FFCC99');
  2307.             case 0x30: return array('rgb' => '3366FF');
  2308.             case 0x31: return array('rgb' => '33CCCC');
  2309.             case 0x32: return array('rgb' => '99CC00');
  2310.             case 0x33: return array('rgb' => 'FFCC00');
  2311.             case 0x34: return array('rgb' => 'FF9900');
  2312.             case 0x35: return array('rgb' => 'FF6600');
  2313.             case 0x36: return array('rgb' => '666699');
  2314.             case 0x37: return array('rgb' => '969696');
  2315.             case 0x38: return array('rgb' => '003366');
  2316.             case 0x39: return array('rgb' => '339966');
  2317.             case 0x3A: return array('rgb' => '003300');
  2318.             case 0x3B: return array('rgb' => '333300');
  2319.             case 0x3C: return array('rgb' => '993300');
  2320.             case 0x3D: return array('rgb' => '993366');
  2321.             case 0x3E: return array('rgb' => '333399');
  2322.             case 0x3F: return array('rgb' => '333333');
  2323.             default: return false;
  2324.         }
  2325.     }
  2326.     
  2327.     /**
  2328.      * Get a sheet range like Sheet1:Sheet3 from REF index
  2329.      * Note: If there is only one sheet in the range, one gets e.g Sheet1
  2330.      */
  2331.     private function _readSheetRangeByRefIndex($index)
  2332.     {
  2333.         // we are assuming that ref index refers to internal workbook
  2334.         // in general, this is wrong, fix later
  2335.         echo "Index:$index:";
  2336.         if (isset($this->_ref[$index])) {
  2337.             $firstSheetName = $this->_boundsheets[$this->_ref[$index]['firstSheetIndex']]['name'];
  2338.             $firstSheetName = (strpos($firstSheetName, ' ') !== false) ?
  2339.                 "'$firstSheetName'" : $firstSheetName;
  2340.             $lastSheetName = $this->_boundsheets[$this->_ref[$index]['lastSheetIndex']]['name'];
  2341.             $lastSheetName = (strpos($lastSheetName, ' ') !== false) ?
  2342.                 "'$lastSheetName'" : $lastSheetName;
  2343.             if ($firstSheetName == $lastSheetName) {
  2344.                 return $firstSheetName;
  2345.             } else {
  2346.                 return "$firstSheetName:$lastSheetName";
  2347.             }
  2348.         }
  2349.         return false;
  2350.     }
  2351.     
  2352.     /**
  2353.      * Convert formula structure (HEX data) into human readable Excel formula
  2354.      * Experimental function under construction not currently in use
  2355.      * returns e.g. 'A3+A5*5'
  2356.      */
  2357.     private function _getFormulaStringFromStructure($formulaStructure)
  2358.     {
  2359.         $formulaHL = '=';
  2360.         
  2361.         echo '<xmp>';
  2362.         // offset: 0; size: 2; size of the following formula data
  2363.         $sz = $this->_getInt2d($formulaStructure, 0);
  2364.         echo 'size: ' . $sz . "\n";
  2365.         
  2366.         // offset: 2; size: sz
  2367.         $formulaData = substr($formulaStructure, 2, $sz);
  2368.         
  2369.         // offset: 2 + sz; size: variable (optional)
  2370.         if (strlen($formulaStructure) > 2 + $sz) {
  2371.             $additionalData = substr($formulaStructure, 2 + $sz);
  2372.         }
  2373.         
  2374.         // dump the entire formula structure
  2375.         echo 'the entire formulastructure: ';
  2376.         echo '<span style="background:yellow">';
  2377.         for ($i = 0; $i < 2; $i++) {
  2378.             echo sprintf('%02X', ord($formulaStructure[$i])) . ' ';
  2379.         }
  2380.         echo '</span>';
  2381.         echo '<span style="background:cyan">';
  2382.         for ($i = 2; $i < 2 + $sz; $i++) {
  2383.             echo sprintf('%02X', ord($formulaStructure[$i])) . ' ';
  2384.         }
  2385.         echo '</span>';
  2386.         echo "\n----\n";
  2387.         
  2388.         // start parsing the $formulaData
  2389.         $tokens = array();
  2390.         $formulaDataRemain = $formulaData;
  2391.         while (strlen($formulaDataRemain) > 0 and $token = $this->_getNextToken($formulaDataRemain)) {
  2392.             $tokens[] = $token;
  2393.             var_dump($token);
  2394.             $formulaDataRemain = substr($formulaDataRemain, $token['size']);
  2395.         }
  2396.         
  2397.         $formulaString = $this->_createFormulaStringFromTokens($tokens);
  2398.         
  2399.         return false;
  2400.     }
  2401.     
  2402.     /**
  2403.      * Convert array of tokens into string
  2404.      * Experimental function under construction not currently in use
  2405.      * returns e.g. 'A3+A5*5'
  2406.      */
  2407.     private function _createFormulaStringFromTokens($tokens)
  2408.     {
  2409.         $formulaStrings = array();
  2410.         foreach ($tokens as $token) {
  2411.             // initialize space
  2412.             $space0 = isset($space0) ? $space0 : '';
  2413.             
  2414.             switch ($token['name']) {
  2415.             case 'tAdd': // addition
  2416.             case 'tConcat': // addition
  2417.             case 'tDiv': // division
  2418.             case 'tEQ': // equaltiy
  2419.             case 'tGT': // greater than
  2420.             case 'tLE': // less than or equals
  2421.             case 'tLT': // less than
  2422.             case 'tMul': // multiplication
  2423.             case 'tNE': // multiplication
  2424.             case 'tPower': // power
  2425.             case 'tSub': // subtraction
  2426.                 $op2 = array_pop($formulaStrings);
  2427.                 $op1 = array_pop($formulaStrings);
  2428.                 $formulaStrings[] = "$op1$space0{$token['data']}$op2";
  2429.                 unset($space0);
  2430.                 break;
  2431.             case 'tArea': // cell range address
  2432.                 $formulaStrings[] = $space0 . $token['data'];
  2433.                 unset($space0);
  2434.                 break;
  2435.             case 'tAttrIf':
  2436.                 // token is only important for Excel formula evaluator
  2437.                 // not needed here
  2438.                 break;
  2439.             case 'tAttrSkip':
  2440.                 // token is only important for Excel formula evaluator
  2441.                 // not needed here
  2442.                 break;
  2443.             case 'tAttrSpace': // space / carriage return
  2444.                 // space will be used when next token arrives, do not alter formulaString stack
  2445.                 switch ($token['data']['spacetype']) {
  2446.                 case 'type0': $space0 = str_repeat(' ', $token['data']['spacecount']); break;
  2447.                 default: break; // unrecognized
  2448.                 }
  2449.                 break;
  2450.             case 'tAttrSum': // SUM function with one parameter
  2451.                 $op = array_pop($formulaStrings);
  2452.                 $formulaStrings[] = "{$space0}SUM($op)";
  2453.                 unset($space0);
  2454.                 break;
  2455.             case 'tAttrVolatile': // indicates volatile function
  2456.                 // token is only important for Excel formula evaluator
  2457.                 // not needed here
  2458.                 break;
  2459.             case 'tBool': // boolean
  2460.                 $formulaStrings[] = "$space0{$token['data']}";
  2461.                 unset($space0);
  2462.                 break;
  2463.             case 'tFunc': // function with fixed number of arguments
  2464.             case 'tFuncV': // function with variable number of arguments
  2465.                 $ops = array(); // array of operators
  2466.                 for ($i = 0; $i < $token['data']['args']; $i++) {
  2467.                     $ops[] = array_pop($formulaStrings);
  2468.                 }
  2469.                 $ops = array_reverse($ops);
  2470.                 $formulaStrings[] = "$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
  2471.                 unset($space0);
  2472.                 break;
  2473.             case 'tParen': // parenthesis
  2474.                 $expression = array_pop($formulaStrings);
  2475.                 $formulaStrings[] = "($expression)";
  2476.                 // space0 won't occur
  2477.                 break;
  2478.             case 'tInt': // integer
  2479.                 $formulaStrings[] = "$space0{$token['data']}";
  2480.                 unset($space0);
  2481.                 break;
  2482.             case 'tNum': // number
  2483.                 $formulaStrings[] = "$space0{$token['data']}";
  2484.                 unset($space0);
  2485.                 break;
  2486.             case 'tRef': // single cell reference
  2487.                 $formulaStrings[] = "$space0{$token['data']}";
  2488.                 unset($space0);
  2489.                 break;
  2490.             case 'tRef3d': // 3d cell reference V
  2491.                 $formulaStrings[] = "$space0{$token['data']}";
  2492.                 unset($space0);
  2493.                 break;
  2494.             case 'tStr': // string
  2495.                 $formulaStrings[] = "$space0{$token['data']}";
  2496.                 unset($space0);
  2497.                 break;
  2498.             }
  2499.         }
  2500.         $formulaString = $formulaStrings[0];
  2501.         
  2502.         echo '----' . "\n";
  2503.         echo 'Formula string: ' . $formulaString;
  2504.         return $formulaString;
  2505.     }
  2506.     
  2507.     /**
  2508.      * Fetch next token from portion of formula data (HEX)
  2509.      * Experimental function under construction not currently in use
  2510.      */
  2511.     private function _getNextToken($formulaDataRemain)
  2512.     {
  2513.         // offset: 0; size: 1; token id
  2514.         $id = ord($formulaDataRemain[0]); // token id
  2515.         $name = false; // initialize token name
  2516.         
  2517.         switch ($id) {
  2518.         case 0x03: $name = 'tAdd';        $size = 1;    $data = '+';    break;
  2519.         case 0x04: $name = 'tSub';        $size = 1;    $data = '-';    break;
  2520.         case 0x05: $name = 'tMul';        $size = 1;    $data = '*';    break;
  2521.         case 0x06: $name = 'tDiv';        $size = 1;    $data = '/';    break;
  2522.         case 0x07: $name = 'tPower';    $size = 1;    $data = '^';    break;
  2523.         case 0x08: $name = 'tConcat';    $size = 1;    $data = '&';    break;
  2524.         case 0x09: $name = 'tLT';        $size = 1;    $data = '<';    break;
  2525.         case 0x0A: $name = 'tLE';        $size = 1;    $data = '<=';    break;
  2526.         case 0x0B: $name = 'tEQ';        $size = 1;    $data = '=';    break;
  2527.         case 0x0D: $name = 'tGT';        $size = 1;    $data = '>';    break;
  2528.         case 0x0E: $name = 'tNE';        $size = 1;    $data = '<>';    break;
  2529.         case 0x15: // parenthesis
  2530.             $name  = 'tParen';
  2531.             $size  = 1;
  2532.             $data = null;
  2533.             break;
  2534.         case 0x17: // string
  2535.             $name = 'tStr';
  2536.             // offset: 1; size: var; Unicode string, 8-bit string length
  2537.             $unicode = $this->_readUnicodeStringShort(substr($formulaDataRemain, 1));
  2538.             $size = 1 + $unicode['size'];
  2539.             $data = "\"$unicode[string]\"";
  2540.             break;
  2541.         case 0x19: // Special attribute
  2542.             // offset: 1; size: 1; attribute type flags:
  2543.             switch (ord($formulaDataRemain[1])) {
  2544.             case 0x01:
  2545.                 $name = 'tAttrVolatile';
  2546.                 $size = 4;
  2547.                 $data = null;
  2548.                 break;
  2549.             case 0x02:
  2550.                 $name = 'tAttrIf';
  2551.                 $size = 4;
  2552.                 $data = null;
  2553.                 break;
  2554.             case 0x08:
  2555.                 $name = 'tAttrSkip';
  2556.                 $size = 4;
  2557.                 $data = null;
  2558.                 break;
  2559.             case 0x10:
  2560.                 $name = 'tAttrSum';
  2561.                 $size = 4;
  2562.                 $data = null;
  2563.                 break;
  2564.             case 0x40:
  2565.                 $name = 'tAttrSpace';
  2566.                 $size = 4;
  2567.                 // offset: 2; size: 2; space type and position
  2568.                 switch (ord($formulaDataRemain[2])) {
  2569.                 case 0x00: $spacetype = 'type0'; break;
  2570.                 default: $name = null; break; // unrecognized
  2571.                 }
  2572.                 // offset: 3; size: 1; number of inserted spaces/carriage returns
  2573.                 $spacecount = ord($formulaDataRemain[3]);
  2574.                 
  2575.                 $data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
  2576.                 break;
  2577.             default:
  2578.                 $name = null; // unrecognized
  2579.                 break;
  2580.             }
  2581.             break;
  2582.         case 0x1D: // boolean
  2583.             // offset: 1; size: 1; 0 = false, 1 = true;
  2584.             $name = 'tBool';
  2585.             $size = 2;
  2586.             $data = ord($formulaDataRemain[1]) ? 'TRUE' : 'FALSE';
  2587.             break;
  2588.         case 0x1E: // integer
  2589.             // offset: 1; size: 2; unsigned 16-bit integer
  2590.             $name = 'tInt';
  2591.             $size = 3;
  2592.             $data = $this->_getInt2d($formulaDataRemain, 1);
  2593.             break;
  2594.         case 0x1F: // number
  2595.             // offset: 1; size: 8;
  2596.             $name = 'tNum';
  2597.             $size = 9;
  2598.             $data = $this->_readNumber(substr($formulaDataRemain, 1));
  2599.             var_dump($data);
  2600.             break;
  2601.         case 0x24: // single cell reference e.g. A5
  2602.         case 0x44:
  2603.             $name = 'tRef';
  2604.             $size = 5;
  2605.             $data = $this->_readBIFF8CellAddress(substr($formulaDataRemain, 1, 4));
  2606.             break;
  2607.         case 0x25: // cell range reference to cells in the same sheet
  2608.         case 0x45:
  2609.             $name = 'tArea';
  2610.             $size = 9;
  2611.             $data = $this->_readBIFF8CellRangeAddress(substr($formulaDataRemain, 1, 8));
  2612.             break;
  2613.         case 0x41: // function with fixed number of arguments
  2614.             $name = 'tFunc';
  2615.             $size = 3;
  2616.             // offset: 1; size: 2; index to built-in sheet function
  2617.             switch ($this->_GetInt2d($formulaDataRemain, 1)) {
  2618.             case   3: $function = 'ISERROR';     $args = 1;     break;
  2619.             case  10: $function = 'NA';         $args = 0;     break;
  2620.             case  15: $function = 'SIN';         $args = 1;     break;
  2621.             case  19: $function = 'PI';         $args = 0;     break;
  2622.             case  20: $function = 'SQRT';         $args = 1;     break;
  2623.             case  25: $function = 'INT';         $args = 1;     break;
  2624.             case  38: $function = 'NOT';         $args = 1;     break;
  2625.             case  39: $function = 'MOD';         $args = 2;    break;
  2626.             case  48: $function = 'TEXT';         $args = 2;    break;
  2627.             case  65: $function = 'DATE';         $args = 3;    break;
  2628.             case  66: $function = 'TIME';         $args = 3;    break;
  2629.             case  67: $function = 'DAY';         $args = 1;    break;
  2630.             case  68: $function = 'MONTH';         $args = 1;    break;
  2631.             case  69: $function = 'YEAR';         $args = 1;    break;
  2632.             case  71: $function = 'HOUR';         $args = 1;    break;
  2633.             case  72: $function = 'MINUTE';     $args = 1;    break;
  2634.             case  73: $function = 'SECOND';     $args = 1;    break;
  2635.             case  74: $function = 'NOW';         $args = 0;    break;
  2636.             case 140: $function = 'DATEVALUE';     $args = 1;    break;
  2637.             case 221: $function = 'TODAY';         $args = 0;    break;
  2638.             default: $name = null; break; // unrecognized
  2639.             }
  2640.             if ($name) {
  2641.                 $data = array('function' => $function, 'args' => $args);
  2642.             }
  2643.             break;
  2644.         case 0x42: // function with variable number of arguments
  2645.         case 0x62: // function with variable number of arguments
  2646.             $name = 'tFuncV';
  2647.             $size = 4;
  2648.             // offset: 1; size: 1; number of arguments
  2649.             $args = ord($formulaDataRemain[1]);
  2650.             // offset: 2: size: 2; index to built-in sheet function
  2651.             switch ($this->_GetInt2d($formulaDataRemain, 2)) {
  2652.             case   1: $function = 'IF';            break;
  2653.             case   4: $function = 'SUM';        break;
  2654.             case   6: $function = 'MIN';        break;
  2655.             case  29: $function = 'INDEX';        break;
  2656.             case  36: $function = 'AND';        break;
  2657.             case  37: $function = 'OR';            break;
  2658.             case  64: $function = 'MATCH';        break;
  2659.             case  78: $function = 'OFFSET';        break;
  2660.             case 102: $function = 'VLOOKUP';    break;
  2661.             case 345: $function = 'SUMIF';        break;
  2662.             default: $name = null; // unrecognized
  2663.             }
  2664.             if ($name) {
  2665.                 $data = array('function' => $function, 'args' => $args);
  2666.             }
  2667.             break;
  2668.         case 0x5A: // 3d cell reference V
  2669.             $name = 'tRef3d';
  2670.             $size = 7;
  2671.             // offset: 1; size: 2; index to REF entry
  2672.             $sheetRange = $this->_readSheetRangeByRefIndex($this->_getInt2d($formulaDataRemain, 1));
  2673.             // offset: 3; size: 4; cell address
  2674.             $cellAddress = $this->_readBIFF8CellAddress(substr($formulaDataRemain, 3, 4));
  2675.             $data = "$sheetRange!$cellAddress";
  2676.             break;
  2677.         // case 0x39: // don't know how to deal with
  2678.         default:
  2679.             $name = null; // unregognized
  2680.             break;
  2681.         }
  2682.         
  2683.         if ($name) {
  2684.             return array(
  2685.                 'id' => $id,
  2686.                 'name' => $name,
  2687.                 'size' => $size,
  2688.                 'data' => $data,
  2689.             );
  2690.         }
  2691.         
  2692.         echo 'Could not recognize this structure: ' . sprintf('%02X', $id) . "\n";
  2693.         
  2694.         return false;
  2695.     }
  2696.     
  2697.     /**
  2698.      * Dump a byte sequence, only used for debugging
  2699.      * Experimental function to be removed
  2700.      */
  2701.     private function _dump($data)
  2702.     {
  2703.         for ($i = 0; $i < strlen($data); $i++) {
  2704.             echo sprintf('%02X', ord($data[$i])) . ' ';
  2705.         }
  2706.     }
  2707.     
  2708. }

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