Advertisement
gotopa

excel_reader2.php

Jan 11th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 58.53 KB | None | 0 0
  1. <?php
  2. /**
  3.  * A class for reading Microsoft Excel (97/2003) Spreadsheets.
  4.  *
  5.  * Version 2.21
  6.  *
  7.  * Enhanced and maintained by Matt Kruse < http://mattkruse.com >
  8.  * Maintained at http://code.google.com/p/php-excel-reader/
  9.  *
  10.  * Format parsing and MUCH more contributed by:
  11.  *    Matt Roxburgh < http://www.roxburgh.me.uk >
  12.  *
  13.  * DOCUMENTATION
  14.  * =============
  15.  *   http://code.google.com/p/php-excel-reader/wiki/Documentation
  16.  *
  17.  * CHANGE LOG
  18.  * ==========
  19.  *   http://code.google.com/p/php-excel-reader/wiki/ChangeHistory
  20.  *
  21.  * DISCUSSION/SUPPORT
  22.  * ==================
  23.  *   http://groups.google.com/group/php-excel-reader-discuss/topics
  24.  *
  25.  * --------------------------------------------------------------------------
  26.  *
  27.  * Originally developed by Vadim Tkachenko under the name PHPExcelReader.
  28.  * (http://sourceforge.net/projects/phpexcelreader)
  29.  * Based on the Java version by Andy Khan (http://www.andykhan.com).  Now
  30.  * maintained by David Sanders.  Reads only Biff 7 and Biff 8 formats.
  31.  *
  32.  * PHP versions 4 and 5
  33.  *
  34.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  35.  * that is available through the world-wide-web at the following URI:
  36.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  37.  * the PHP License and are unable to obtain it through the web, please
  38.  * send a note to license@php.net so we can mail you a copy immediately.
  39.  *
  40.  * @category   Spreadsheet
  41.  * @package Spreadsheet_Excel_Reader
  42.  * @author   Vadim Tkachenko <vt@apachephp.com>
  43.  * @license http://www.php.net/license/3_0.txt  PHP License 3.0
  44.  * @version CVS: $Id: reader.php 19 2007-03-13 12:42:41Z shangxiao $
  45.  * @link       http://pear.php.net/package/Spreadsheet_Excel_Reader
  46.  * @see     OLE, Spreadsheet_Excel_Writer
  47.  * --------------------------------------------------------------------------
  48.  */
  49.  
  50. define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
  51. define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
  52. define('ROOT_START_BLOCK_POS', 0x30);
  53. define('BIG_BLOCK_SIZE', 0x200);
  54. define('SMALL_BLOCK_SIZE', 0x40);
  55. define('EXTENSION_BLOCK_POS', 0x44);
  56. define('NUM_EXTENSION_BLOCK_POS', 0x48);
  57. define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
  58. define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
  59. define('SMALL_BLOCK_THRESHOLD', 0x1000);
  60. // property storage offsets
  61. define('SIZE_OF_NAME_POS', 0x40);
  62. define('TYPE_POS', 0x42);
  63. define('START_BLOCK_POS', 0x74);
  64. define('SIZE_POS', 0x78);
  65. define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
  66.  
  67.  
  68. function GetInt4d($data, $pos) {
  69.     $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  70.     if ($value>=4294967294) {
  71.         $value=-2;
  72.     }
  73.     return $value;
  74. }
  75.  
  76. // http://uk.php.net/manual/en/function.getdate.php
  77. function gmgetdate($ts = null){
  78.     $k = array('seconds','minutes','hours','mday','wday','mon','year','yday','weekday','month',0);
  79.     //return(array_comb($k,split(":",gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts))));
  80.     return(array_comb($k,explode(":",gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts))));
  81.     }
  82.  
  83. // Added for PHP4 compatibility
  84. function array_comb($array1, $array2) {
  85.     $out = array();
  86.     foreach ($array1 as $key => $value) {
  87.         $out[$value] = $array2[$key];
  88.     }
  89.     return $out;
  90. }
  91.  
  92. function v($data,$pos) {
  93.     return ord($data[$pos]) | ord($data[$pos+1])<<8;
  94. }
  95.  
  96. class OLERead {
  97.     var $data = '';
  98.     function OLERead(){ }
  99.  
  100.     function read($sFileName){
  101.         // check if file exist and is readable (Darko Miljanovic)
  102.         if(!is_readable($sFileName)) {
  103.             $this->error = 1;
  104.             return false;
  105.         }
  106.         $this->data = @file_get_contents($sFileName);
  107.         if (!$this->data) {
  108.             $this->error = 1;
  109.             return false;
  110.         }
  111.         if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
  112.             $this->error = 1;
  113.             return false;
  114.         }
  115.         $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  116.         $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
  117.         $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS);
  118.         $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS);
  119.         $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);
  120.  
  121.         $bigBlockDepotBlocks = array();
  122.         $pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
  123.         $bbdBlocks = $this->numBigBlockDepotBlocks;
  124.         if ($this->numExtensionBlocks != 0) {
  125.             $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
  126.         }
  127.  
  128.         for ($i = 0; $i < $bbdBlocks; $i++) {
  129.             $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
  130.             $pos += 4;
  131.         }
  132.  
  133.  
  134.         for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
  135.             $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
  136.             $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);
  137.  
  138.             for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
  139.                 $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
  140.                 $pos += 4;
  141.             }
  142.  
  143.             $bbdBlocks += $blocksToRead;
  144.             if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
  145.                 $this->extensionBlock = GetInt4d($this->data, $pos);
  146.             }
  147.         }
  148.  
  149.         // readBigBlockDepot
  150.         $pos = 0;
  151.         $index = 0;
  152.         $this->bigBlockChain = array();
  153.  
  154.         for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
  155.             $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
  156.             //echo "pos = $pos";
  157.             for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
  158.                 $this->bigBlockChain[$index] = GetInt4d($this->data, $pos);
  159.                 $pos += 4 ;
  160.                 $index++;
  161.             }
  162.         }
  163.  
  164.         // readSmallBlockDepot();
  165.         $pos = 0;
  166.         $index = 0;
  167.         $sbdBlock = $this->sbdStartBlock;
  168.         $this->smallBlockChain = array();
  169.  
  170.         while ($sbdBlock != -2) {
  171.           $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;
  172.           for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
  173.             $this->smallBlockChain[$index] = GetInt4d($this->data, $pos);
  174.             $pos += 4;
  175.             $index++;
  176.           }
  177.           $sbdBlock = $this->bigBlockChain[$sbdBlock];
  178.         }
  179.  
  180.  
  181.         // readData(rootStartBlock)
  182.         $block = $this->rootStartBlock;
  183.         $pos = 0;
  184.         $this->entry = $this->__readData($block);
  185.         $this->__readPropertySets();
  186.     }
  187.  
  188.     function __readData($bl) {
  189.         $block = $bl;
  190.         $pos = 0;
  191.         $data = '';
  192.         while ($block != -2)  {
  193.             $pos = ($block + 1) * BIG_BLOCK_SIZE;
  194.             $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
  195.             $block = $this->bigBlockChain[$block];
  196.         }
  197.         return $data;
  198.      }
  199.  
  200.     function __readPropertySets(){
  201.         $offset = 0;
  202.         while ($offset < strlen($this->entry)) {
  203.             $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);
  204.             $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8);
  205.             $type = ord($d[TYPE_POS]);
  206.             $startBlock = GetInt4d($d, START_BLOCK_POS);
  207.             $size = GetInt4d($d, SIZE_POS);
  208.             $name = '';
  209.             for ($i = 0; $i < $nameSize ; $i++) {
  210.                 $name .= $d[$i];
  211.             }
  212.             $name = str_replace("\x00", "", $name);
  213.             $this->props[] = array (
  214.                 'name' => $name,
  215.                 'type' => $type,
  216.                 'startBlock' => $startBlock,
  217.                 'size' => $size);
  218.             if ((strtolower($name) == "workbook") || ( strtolower($name) == "book")) {
  219.                 $this->wrkbook = count($this->props) - 1;
  220.             }
  221.             if ($name == "Root Entry") {
  222.                 $this->rootentry = count($this->props) - 1;
  223.             }
  224.             $offset += PROPERTY_STORAGE_BLOCK_SIZE;
  225.         }
  226.  
  227.     }
  228.  
  229.  
  230.     function getWorkBook(){
  231.         if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
  232.             $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);
  233.             $streamData = '';
  234.             $block = $this->props[$this->wrkbook]['startBlock'];
  235.             $pos = 0;
  236.             while ($block != -2) {
  237.                   $pos = $block * SMALL_BLOCK_SIZE;
  238.                   $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);
  239.                   $block = $this->smallBlockChain[$block];
  240.             }
  241.             return $streamData;
  242.         }else{
  243.             $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
  244.             if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
  245.                 $numBlocks++;
  246.             }
  247.  
  248.             if ($numBlocks == 0) return '';
  249.             $streamData = '';
  250.             $block = $this->props[$this->wrkbook]['startBlock'];
  251.             $pos = 0;
  252.             while ($block != -2) {
  253.               $pos = ($block + 1) * BIG_BLOCK_SIZE;
  254.               $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
  255.               $block = $this->bigBlockChain[$block];
  256.             }
  257.             return $streamData;
  258.         }
  259.     }
  260.  
  261. }
  262.  
  263. define('SPREADSHEET_EXCEL_READER_BIFF8',             0x600);
  264. define('SPREADSHEET_EXCEL_READER_BIFF7',             0x500);
  265. define('SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS',   0x5);
  266. define('SPREADSHEET_EXCEL_READER_WORKSHEET',         0x10);
  267. define('SPREADSHEET_EXCEL_READER_TYPE_BOF',       0x809);
  268. define('SPREADSHEET_EXCEL_READER_TYPE_EOF',       0x0a);
  269. define('SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET',   0x85);
  270. define('SPREADSHEET_EXCEL_READER_TYPE_DIMENSION',   0x200);
  271. define('SPREADSHEET_EXCEL_READER_TYPE_ROW',       0x208);
  272. define('SPREADSHEET_EXCEL_READER_TYPE_DBCELL',     0xd7);
  273. define('SPREADSHEET_EXCEL_READER_TYPE_FILEPASS',     0x2f);
  274. define('SPREADSHEET_EXCEL_READER_TYPE_NOTE',         0x1c);
  275. define('SPREADSHEET_EXCEL_READER_TYPE_TXO',       0x1b6);
  276. define('SPREADSHEET_EXCEL_READER_TYPE_RK',         0x7e);
  277. define('SPREADSHEET_EXCEL_READER_TYPE_RK2',       0x27e);
  278. define('SPREADSHEET_EXCEL_READER_TYPE_MULRK',       0xbd);
  279. define('SPREADSHEET_EXCEL_READER_TYPE_MULBLANK',     0xbe);
  280. define('SPREADSHEET_EXCEL_READER_TYPE_INDEX',       0x20b);
  281. define('SPREADSHEET_EXCEL_READER_TYPE_SST',       0xfc);
  282. define('SPREADSHEET_EXCEL_READER_TYPE_EXTSST',     0xff);
  283. define('SPREADSHEET_EXCEL_READER_TYPE_CONTINUE',     0x3c);
  284. define('SPREADSHEET_EXCEL_READER_TYPE_LABEL',       0x204);
  285. define('SPREADSHEET_EXCEL_READER_TYPE_LABELSST',     0xfd);
  286. define('SPREADSHEET_EXCEL_READER_TYPE_NUMBER',     0x203);
  287. define('SPREADSHEET_EXCEL_READER_TYPE_NAME',         0x18);
  288. define('SPREADSHEET_EXCEL_READER_TYPE_ARRAY',       0x221);
  289. define('SPREADSHEET_EXCEL_READER_TYPE_STRING',     0x207);
  290. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA',   0x406);
  291. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA2',     0x6);
  292. define('SPREADSHEET_EXCEL_READER_TYPE_FORMAT',     0x41e);
  293. define('SPREADSHEET_EXCEL_READER_TYPE_XF',         0xe0);
  294. define('SPREADSHEET_EXCEL_READER_TYPE_BOOLERR',   0x205);
  295. define('SPREADSHEET_EXCEL_READER_TYPE_FONT',      0x0031);
  296. define('SPREADSHEET_EXCEL_READER_TYPE_PALETTE',   0x0092);
  297. define('SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN',   0xffff);
  298. define('SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22);
  299. define('SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS',  0xE5);
  300. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS' ,   25569);
  301. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107);
  302. define('SPREADSHEET_EXCEL_READER_MSINADAY',       86400);
  303. define('SPREADSHEET_EXCEL_READER_TYPE_HYPER',        0x01b8);
  304. define('SPREADSHEET_EXCEL_READER_TYPE_COLINFO',      0x7d);
  305. define('SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH',  0x55);
  306. define('SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH', 0x99);
  307. define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT',   "%s");
  308.  
  309.  
  310. /*
  311. * Main Class
  312. */
  313. class Spreadsheet_Excel_Reader {
  314.  
  315.     // MK: Added to make data retrieval easier
  316.     var $colnames = array();
  317.     var $colindexes = array();
  318.     var $standardColWidth = 0;
  319.     var $defaultColWidth = 0;
  320.  
  321.     function myHex($d) {
  322.         if ($d < 16) return "0" . dechex($d);
  323.         return dechex($d);
  324.     }
  325.    
  326.     function dumpHexData($data, $pos, $length) {
  327.         $info = "";
  328.         for ($i = 0; $i <= $length; $i++) {
  329.             $info .= ($i==0?"":" ") . $this->myHex(ord($data[$pos + $i])) . (ord($data[$pos + $i])>31? "[" . $data[$pos + $i] . "]":'');
  330.         }
  331.         return $info;
  332.     }
  333.  
  334.     function getCol($col) {
  335.         if (is_string($col)) {
  336.             $col = strtolower($col);
  337.             if (array_key_exists($col,$this->colnames)) {
  338.                 $col = $this->colnames[$col];
  339.             }
  340.         }
  341.         return $col;
  342.     }
  343.  
  344.     // PUBLIC API FUNCTIONS
  345.     // --------------------
  346.  
  347.     function val($row,$col,$sheet=0) {
  348.         $col = $this->getCol($col);
  349.         if (array_key_exists($row,$this->sheets[$sheet]['cells']) && array_key_exists($col,$this->sheets[$sheet]['cells'][$row])) {
  350.             return $this->sheets[$sheet]['cells'][$row][$col];
  351.         }
  352.         return "";
  353.     }
  354.     function value($row,$col,$sheet=0) {
  355.         return $this->val($row,$col,$sheet);
  356.     }
  357.     function info($row,$col,$type='',$sheet=0) {
  358.         $col = $this->getCol($col);
  359.         if (array_key_exists('cellsInfo',$this->sheets[$sheet])
  360.                 && array_key_exists($row,$this->sheets[$sheet]['cellsInfo'])
  361.                 && array_key_exists($col,$this->sheets[$sheet]['cellsInfo'][$row])
  362.                 && array_key_exists($type,$this->sheets[$sheet]['cellsInfo'][$row][$col])) {
  363.             return $this->sheets[$sheet]['cellsInfo'][$row][$col][$type];
  364.         }
  365.         return "";
  366.     }
  367.     function type($row,$col,$sheet=0) {
  368.         return $this->info($row,$col,'type',$sheet);
  369.     }
  370.     function raw($row,$col,$sheet=0) {
  371.         return $this->info($row,$col,'raw',$sheet);
  372.     }
  373.     function rowspan($row,$col,$sheet=0) {
  374.         $val = $this->info($row,$col,'rowspan',$sheet);
  375.         if ($val=="") { return 1; }
  376.         return $val;
  377.     }
  378.     function colspan($row,$col,$sheet=0) {
  379.         $val = $this->info($row,$col,'colspan',$sheet);
  380.         if ($val=="") { return 1; }
  381.         return $val;
  382.     }
  383.     function hyperlink($row,$col,$sheet=0) {
  384.         $link = $this->sheets[$sheet]['cellsInfo'][$row][$col]['hyperlink'];
  385.         if ($link) {
  386.             return $link['link'];
  387.         }
  388.         return '';
  389.     }
  390.     function rowcount($sheet=0) {
  391.         return $this->sheets[$sheet]['numRows'];
  392.     }
  393.     function colcount($sheet=0) {
  394.         return $this->sheets[$sheet]['numCols'];
  395.     }
  396.     function colwidth($col,$sheet=0) {
  397.         // Col width is actually the width of the number 0. So we have to estimate and come close
  398.         return $this->colInfo[$sheet][$col]['width']/9142*200;
  399.     }
  400.     function colhidden($col,$sheet=0) {
  401.         return !!$this->colInfo[$sheet][$col]['hidden'];
  402.     }
  403.     function rowheight($row,$sheet=0) {
  404.         return $this->rowInfo[$sheet][$row]['height'];
  405.     }
  406.     function rowhidden($row,$sheet=0) {
  407.         return !!$this->rowInfo[$sheet][$row]['hidden'];
  408.     }
  409.    
  410.     // GET THE CSS FOR FORMATTING
  411.     // ==========================
  412.     function style($row,$col,$sheet=0,$properties='') {
  413.         $css = "";
  414.         $font=$this->font($row,$col,$sheet);
  415.         if ($font!="") {
  416.             $css .= "font-family:$font;";
  417.         }
  418.         $align=$this->align($row,$col,$sheet);
  419.         if ($align!="") {
  420.             $css .= "text-align:$align;";
  421.         }
  422.         $height=$this->height($row,$col,$sheet);
  423.         if ($height!="") {
  424.             $css .= "font-size:$height"."px;";
  425.         }
  426.         $bgcolor=$this->bgColor($row,$col,$sheet);
  427.         if ($bgcolor!="") {
  428.             $bgcolor = $this->colors[$bgcolor];
  429.             $css .= "background-color:$bgcolor;";
  430.         }
  431.         $color=$this->color($row,$col,$sheet);
  432.         if ($color!="") {
  433.             $css .= "color:$color;";
  434.         }
  435.         $bold=$this->bold($row,$col,$sheet);
  436.         if ($bold) {
  437.             $css .= "font-weight:bold;";
  438.         }
  439.         $italic=$this->italic($row,$col,$sheet);
  440.         if ($italic) {
  441.             $css .= "font-style:italic;";
  442.         }
  443.         $underline=$this->underline($row,$col,$sheet);
  444.         if ($underline) {
  445.             $css .= "text-decoration:underline;";
  446.         }
  447.         // Borders
  448.         $bLeft = $this->borderLeft($row,$col,$sheet);
  449.         $bRight = $this->borderRight($row,$col,$sheet);
  450.         $bTop = $this->borderTop($row,$col,$sheet);
  451.         $bBottom = $this->borderBottom($row,$col,$sheet);
  452.         $bLeftCol = $this->borderLeftColor($row,$col,$sheet);
  453.         $bRightCol = $this->borderRightColor($row,$col,$sheet);
  454.         $bTopCol = $this->borderTopColor($row,$col,$sheet);
  455.         $bBottomCol = $this->borderBottomColor($row,$col,$sheet);
  456.         // Try to output the minimal required style
  457.         if ($bLeft!="" && $bLeft==$bRight && $bRight==$bTop && $bTop==$bBottom) {
  458.             $css .= "border:" . $this->lineStylesCss[$bLeft] .";";
  459.         }
  460.         else {
  461.             if ($bLeft!="") { $css .= "border-left:" . $this->lineStylesCss[$bLeft] .";"; }
  462.             if ($bRight!="") { $css .= "border-right:" . $this->lineStylesCss[$bRight] .";"; }
  463.             if ($bTop!="") { $css .= "border-top:" . $this->lineStylesCss[$bTop] .";"; }
  464.             if ($bBottom!="") { $css .= "border-bottom:" . $this->lineStylesCss[$bBottom] .";"; }
  465.         }
  466.         // Only output border colors if there is an actual border specified
  467.         if ($bLeft!="" && $bLeftCol!="") { $css .= "border-left-color:" . $bLeftCol .";"; }
  468.         if ($bRight!="" && $bRightCol!="") { $css .= "border-right-color:" . $bRightCol .";"; }
  469.         if ($bTop!="" && $bTopCol!="") { $css .= "border-top-color:" . $bTopCol . ";"; }
  470.         if ($bBottom!="" && $bBottomCol!="") { $css .= "border-bottom-color:" . $bBottomCol .";"; }
  471.        
  472.         return $css;
  473.     }
  474.    
  475.     // FORMAT PROPERTIES
  476.     // =================
  477.     function format($row,$col,$sheet=0) {
  478.         return $this->info($row,$col,'format',$sheet);
  479.     }
  480.     function formatIndex($row,$col,$sheet=0) {
  481.         return $this->info($row,$col,'formatIndex',$sheet);
  482.     }
  483.     function formatColor($row,$col,$sheet=0) {
  484.         return $this->info($row,$col,'formatColor',$sheet);
  485.     }
  486.    
  487.     // CELL (XF) PROPERTIES
  488.     // ====================
  489.     function xfRecord($row,$col,$sheet=0) {
  490.         $xfIndex = $this->info($row,$col,'xfIndex',$sheet);
  491.         if ($xfIndex!="") {
  492.             return $this->xfRecords[$xfIndex];
  493.         }
  494.         return null;
  495.     }
  496.     function xfProperty($row,$col,$sheet,$prop) {
  497.         $xfRecord = $this->xfRecord($row,$col,$sheet);
  498.         if ($xfRecord!=null) {
  499.             return $xfRecord[$prop];
  500.         }
  501.         return "";
  502.     }
  503.     function align($row,$col,$sheet=0) {
  504.         return $this->xfProperty($row,$col,$sheet,'align');
  505.     }
  506.     function bgColor($row,$col,$sheet=0) {
  507.         return $this->xfProperty($row,$col,$sheet,'bgColor');
  508.     }
  509.     function borderLeft($row,$col,$sheet=0) {
  510.         return $this->xfProperty($row,$col,$sheet,'borderLeft');
  511.     }
  512.     function borderRight($row,$col,$sheet=0) {
  513.         return $this->xfProperty($row,$col,$sheet,'borderRight');
  514.     }
  515.     function borderTop($row,$col,$sheet=0) {
  516.         return $this->xfProperty($row,$col,$sheet,'borderTop');
  517.     }
  518.     function borderBottom($row,$col,$sheet=0) {
  519.         return $this->xfProperty($row,$col,$sheet,'borderBottom');
  520.     }
  521.     function borderLeftColor($row,$col,$sheet=0) {
  522.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderLeftColor')];
  523.     }
  524.     function borderRightColor($row,$col,$sheet=0) {
  525.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderRightColor')];
  526.     }
  527.     function borderTopColor($row,$col,$sheet=0) {
  528.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderTopColor')];
  529.     }
  530.     function borderBottomColor($row,$col,$sheet=0) {
  531.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderBottomColor')];
  532.     }
  533.  
  534.     // FONT PROPERTIES
  535.     // ===============
  536.     function fontRecord($row,$col,$sheet=0) {
  537.         $xfRecord = $this->xfRecord($row,$col,$sheet);
  538.         if ($xfRecord!=null) {
  539.             $font = $xfRecord['fontIndex'];
  540.             if ($font!=null) {
  541.                 return $this->fontRecords[$font];
  542.             }
  543.         }
  544.         return null;
  545.     }
  546.     function fontProperty($row,$col,$sheet=0,$prop) {
  547.         $font = $this->fontRecord($row,$col,$sheet);
  548.         if ($font!=null) {
  549.             return $font[$prop];
  550.         }
  551.         return false;
  552.     }
  553.     function fontIndex($row,$col,$sheet=0) {
  554.         return $this->xfProperty($row,$col,$sheet,'fontIndex');
  555.     }
  556.     function color($row,$col,$sheet=0) {
  557.         $formatColor = $this->formatColor($row,$col,$sheet);
  558.         if ($formatColor!="") {
  559.             return $formatColor;
  560.         }
  561.         $ci = $this->fontProperty($row,$col,$sheet,'color');
  562.                 return $this->rawColor($ci);
  563.         }
  564.         function rawColor($ci) {
  565.         if (($ci <> 0x7FFF) && ($ci <> '')) {
  566.             return $this->colors[$ci];
  567.         }
  568.         return "";
  569.     }
  570.     function bold($row,$col,$sheet=0) {
  571.         return $this->fontProperty($row,$col,$sheet,'bold');
  572.     }
  573.     function italic($row,$col,$sheet=0) {
  574.         return $this->fontProperty($row,$col,$sheet,'italic');
  575.     }
  576.     function underline($row,$col,$sheet=0) {
  577.         return $this->fontProperty($row,$col,$sheet,'under');
  578.     }
  579.     function height($row,$col,$sheet=0) {
  580.         return $this->fontProperty($row,$col,$sheet,'height');
  581.     }
  582.     function font($row,$col,$sheet=0) {
  583.         return $this->fontProperty($row,$col,$sheet,'font');
  584.     }
  585.    
  586.     // DUMP AN HTML TABLE OF THE ENTIRE XLS DATA
  587.     // =========================================
  588.     function dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel') {
  589.         $out = "<table class=\"$table_class\" cellspacing=0>";
  590.         if ($col_letters) {
  591.             $out .= "<thead>\n\t<tr>";
  592.             if ($row_numbers) {
  593.                 $out .= "\n\t\t<th>&nbsp</th>";
  594.             }
  595.             for($i=1;$i<=$this->colcount($sheet);$i++) {
  596.                 $style = "width:" . ($this->colwidth($i,$sheet)*1) . "px;";
  597.                 if ($this->colhidden($i,$sheet)) {
  598.                     $style .= "display:none;";
  599.                 }
  600.                 $out .= "\n\t\t<th style=\"$style\">" . strtoupper($this->colindexes[$i]) . "</th>";
  601.             }
  602.             $out .= "</tr></thead>\n";
  603.         }
  604.        
  605.         $out .= "<tbody>\n";
  606.         for($row=1;$row<=$this->rowcount($sheet);$row++) {
  607.             $rowheight = $this->rowheight($row,$sheet);
  608.             $style = "height:" . ($rowheight*(4/3)) . "px;";
  609.             if ($this->rowhidden($row,$sheet)) {
  610.                 $style .= "display:none;";
  611.             }
  612.             $out .= "\n\t<tr style=\"$style\">";
  613.             if ($row_numbers) {
  614.                 $out .= "\n\t\t<th>$row</th>";
  615.             }
  616.             for($col=1;$col<=$this->colcount($sheet);$col++) {
  617.                 // Account for Rowspans/Colspans
  618.                 $rowspan = $this->rowspan($row,$col,$sheet);
  619.                 $colspan = $this->colspan($row,$col,$sheet);
  620.                 for($i=0;$i<$rowspan;$i++) {
  621.                     for($j=0;$j<$colspan;$j++) {
  622.                         if ($i>0 || $j>0) {
  623.                             $this->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
  624.                         }
  625.                     }
  626.                 }
  627.                 if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
  628.                     $style = $this->style($row,$col,$sheet);
  629.                     if ($this->colhidden($col,$sheet)) {
  630.                         $style .= "display:none;";
  631.                     }
  632.                     $out .= "\n\t\t<td style=\"$style\"" . ($colspan > 1?" colspan=$colspan":"") . ($rowspan > 1?" rowspan=$rowspan":"") . ">";
  633.                     $val = $this->val($row,$col,$sheet);
  634.                     if ($val=='') { $val="&nbsp;"; }
  635.                     else {
  636.                         $val = htmlentities($val);
  637.                         $link = $this->hyperlink($row,$col,$sheet);
  638.                         if ($link!='') {
  639.                             $val = "<a href=\"$link\">$val</a>";
  640.                         }
  641.                     }
  642.                     $out .= "<nobr>".nl2br($val)."</nobr>";
  643.                     $out .= "</td>";
  644.                 }
  645.             }
  646.             $out .= "</tr>\n";
  647.         }
  648.         $out .= "</tbody></table>";
  649.         return $out;
  650.     }
  651.    
  652.     // --------------
  653.     // END PUBLIC API
  654.  
  655.  
  656.     var $boundsheets = array();
  657.     var $formatRecords = array();
  658.     var $fontRecords = array();
  659.     var $xfRecords = array();
  660.     var $colInfo = array();
  661.     var $rowInfo = array();
  662.    
  663.     var $sst = array();
  664.     var $sheets = array();
  665.  
  666.     var $data;
  667.     var $_ole;
  668.     var $_defaultEncoding = "UTF-8";
  669.     var $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT;
  670.     var $_columnsFormat = array();
  671.     var $_rowoffset = 1;
  672.     var $_coloffset = 1;
  673.  
  674.     /**
  675.      * List of default date formats used by Excel
  676.      */
  677.     var $dateFormats = array (
  678.         0xe => "m/d/Y",
  679.         0xf => "M-d-Y",
  680.         0x10 => "d-M",
  681.         0x11 => "M-Y",
  682.         0x12 => "h:i a",
  683.         0x13 => "h:i:s a",
  684.         0x14 => "H:i",
  685.         0x15 => "H:i:s",
  686.         0x16 => "d/m/Y H:i",
  687.         0x2d => "i:s",
  688.         0x2e => "H:i:s",
  689.         0x2f => "i:s.S"
  690.     );
  691.  
  692.     /**
  693.      * Default number formats used by Excel
  694.      */
  695.     var $numberFormats = array(
  696.         0x1 => "0",
  697.         0x2 => "0.00",
  698.         0x3 => "#,##0",
  699.         0x4 => "#,##0.00",
  700.         0x5 => "\$#,##0;(\$#,##0)",
  701.         0x6 => "\$#,##0;[Red](\$#,##0)",
  702.         0x7 => "\$#,##0.00;(\$#,##0.00)",
  703.         0x8 => "\$#,##0.00;[Red](\$#,##0.00)",
  704.         0x9 => "0%",
  705.         0xa => "0.00%",
  706.         0xb => "0.00E+00",
  707.         0x25 => "#,##0;(#,##0)",
  708.         0x26 => "#,##0;[Red](#,##0)",
  709.         0x27 => "#,##0.00;(#,##0.00)",
  710.         0x28 => "#,##0.00;[Red](#,##0.00)",
  711.         0x29 => "#,##0;(#,##0)",  // Not exactly
  712.         0x2a => "\$#,##0;(\$#,##0)",  // Not exactly
  713.         0x2b => "#,##0.00;(#,##0.00)",  // Not exactly
  714.         0x2c => "\$#,##0.00;(\$#,##0.00)",  // Not exactly
  715.         0x30 => "##0.0E+0"
  716.     );
  717.  
  718.     var $colors = Array(
  719.         0x00 => "#000000",
  720.         0x01 => "#FFFFFF",
  721.         0x02 => "#FF0000",
  722.         0x03 => "#00FF00",
  723.         0x04 => "#0000FF",
  724.         0x05 => "#FFFF00",
  725.         0x06 => "#FF00FF",
  726.         0x07 => "#00FFFF",
  727.         0x08 => "#000000",
  728.         0x09 => "#FFFFFF",
  729.         0x0A => "#FF0000",
  730.         0x0B => "#00FF00",
  731.         0x0C => "#0000FF",
  732.         0x0D => "#FFFF00",
  733.         0x0E => "#FF00FF",
  734.         0x0F => "#00FFFF",
  735.         0x10 => "#800000",
  736.         0x11 => "#008000",
  737.         0x12 => "#000080",
  738.         0x13 => "#808000",
  739.         0x14 => "#800080",
  740.         0x15 => "#008080",
  741.         0x16 => "#C0C0C0",
  742.         0x17 => "#808080",
  743.         0x18 => "#9999FF",
  744.         0x19 => "#993366",
  745.         0x1A => "#FFFFCC",
  746.         0x1B => "#CCFFFF",
  747.         0x1C => "#660066",
  748.         0x1D => "#FF8080",
  749.         0x1E => "#0066CC",
  750.         0x1F => "#CCCCFF",
  751.         0x20 => "#000080",
  752.         0x21 => "#FF00FF",
  753.         0x22 => "#FFFF00",
  754.         0x23 => "#00FFFF",
  755.         0x24 => "#800080",
  756.         0x25 => "#800000",
  757.         0x26 => "#008080",
  758.         0x27 => "#0000FF",
  759.         0x28 => "#00CCFF",
  760.         0x29 => "#CCFFFF",
  761.         0x2A => "#CCFFCC",
  762.         0x2B => "#FFFF99",
  763.         0x2C => "#99CCFF",
  764.         0x2D => "#FF99CC",
  765.         0x2E => "#CC99FF",
  766.         0x2F => "#FFCC99",
  767.         0x30 => "#3366FF",
  768.         0x31 => "#33CCCC",
  769.         0x32 => "#99CC00",
  770.         0x33 => "#FFCC00",
  771.         0x34 => "#FF9900",
  772.         0x35 => "#FF6600",
  773.         0x36 => "#666699",
  774.         0x37 => "#969696",
  775.         0x38 => "#003366",
  776.         0x39 => "#339966",
  777.         0x3A => "#003300",
  778.         0x3B => "#333300",
  779.         0x3C => "#993300",
  780.         0x3D => "#993366",
  781.         0x3E => "#333399",
  782.         0x3F => "#333333",
  783.         0x40 => "#000000",
  784.         0x41 => "#FFFFFF",
  785.  
  786.         0x43 => "#000000",
  787.         0x4D => "#000000",
  788.         0x4E => "#FFFFFF",
  789.         0x4F => "#000000",
  790.         0x50 => "#FFFFFF",
  791.         0x51 => "#000000",
  792.  
  793.         0x7FFF => "#000000"
  794.     );
  795.  
  796.     var $lineStyles = array(
  797.         0x00 => "",
  798.         0x01 => "Thin",
  799.         0x02 => "Medium",
  800.         0x03 => "Dashed",
  801.         0x04 => "Dotted",
  802.         0x05 => "Thick",
  803.         0x06 => "Double",
  804.         0x07 => "Hair",
  805.         0x08 => "Medium dashed",
  806.         0x09 => "Thin dash-dotted",
  807.         0x0A => "Medium dash-dotted",
  808.         0x0B => "Thin dash-dot-dotted",
  809.         0x0C => "Medium dash-dot-dotted",
  810.         0x0D => "Slanted medium dash-dotted"
  811.     ); 
  812.  
  813.     var $lineStylesCss = array(
  814.         "Thin" => "1px solid",
  815.         "Medium" => "2px solid",
  816.         "Dashed" => "1px dashed",
  817.         "Dotted" => "1px dotted",
  818.         "Thick" => "3px solid",
  819.         "Double" => "double",
  820.         "Hair" => "1px solid",
  821.         "Medium dashed" => "2px dashed",
  822.         "Thin dash-dotted" => "1px dashed",
  823.         "Medium dash-dotted" => "2px dashed",
  824.         "Thin dash-dot-dotted" => "1px dashed",
  825.         "Medium dash-dot-dotted" => "2px dashed",
  826.         "Slanted medium dash-dotte" => "2px dashed"
  827.     );
  828.    
  829.     function read16bitstring($data, $start) {
  830.         $len = 0;
  831.         while (ord($data[$start + $len]) + ord($data[$start + $len + 1]) > 0) $len++;
  832.         return substr($data, $start, $len);
  833.     }
  834.    
  835.     // ADDED by Matt Kruse for better formatting
  836.     function _format_value($format,$num,$f) {
  837.         // 49==TEXT format
  838.         // http://code.google.com/p/php-excel-reader/issues/detail?id=7
  839.         if ( (!$f && $format=="%s") || ($f==49) || ($format=="GENERAL") ) {
  840.             return array('string'=>$num, 'formatColor'=>null);
  841.         }
  842.  
  843.         // Custom pattern can be POSITIVE;NEGATIVE;ZERO
  844.         // The "text" option as 4th parameter is not handled
  845.         //$parts = split(";",$format); // Old
  846.         $parts = explode(";",$format);
  847.         $pattern = $parts[0];
  848.         // Negative pattern
  849.         if (count($parts)>2 && $num==0) {
  850.             $pattern = $parts[2];
  851.         }
  852.         // Zero pattern
  853.         if (count($parts)>1 && $num<0) {
  854.             $pattern = $parts[1];
  855.             $num = abs($num);
  856.         }
  857.  
  858.         $color = "";
  859.         $matches = array();
  860.         $color_regex = "/^\[(BLACK|BLUE|CYAN|GREEN|MAGENTA|RED|WHITE|YELLOW)\]/i";
  861.         if (preg_match($color_regex,$pattern,$matches)) {
  862.             $color = strtolower($matches[1]);
  863.             $pattern = preg_replace($color_regex,"",$pattern);
  864.         }
  865.        
  866.         // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  867.         $pattern = preg_replace("/_./","",$pattern);
  868.        
  869.         // Some non-number characters are escaped with \, which we don't need
  870.         $pattern = preg_replace("/\\\/","",$pattern);
  871.        
  872.         // Some non-number strings are quoted, so we'll get rid of the quotes
  873.         $pattern = preg_replace("/\"/","",$pattern);
  874.  
  875.         // TEMPORARY - Convert # to 0
  876.         $pattern = preg_replace("/\#/","0",$pattern);
  877.  
  878.         // Find out if we need comma formatting
  879.         $has_commas = preg_match("/,/",$pattern);
  880.         if ($has_commas) {
  881.             $pattern = preg_replace("/,/","",$pattern);
  882.         }
  883.  
  884.         // Handle Percentages
  885.         if (preg_match("/\d(\%)([^\%]|$)/",$pattern,$matches)) {
  886.             $num = $num * 100;
  887.             $pattern = preg_replace("/(\d)(\%)([^\%]|$)/","$1%$3",$pattern);
  888.         }
  889.  
  890.         // Handle the number itself
  891.         $number_regex = "/(\d+)(\.?)(\d*)/";
  892.         if (preg_match($number_regex,$pattern,$matches)) {
  893.             $left = $matches[1];
  894.             $dec = $matches[2];
  895.             $right = $matches[3];
  896.             if ($has_commas) {
  897.                 $formatted = number_format($num,strlen($right));
  898.             }
  899.             else {
  900.                 $sprintf_pattern = "%1.".strlen($right)."f";
  901.                 $formatted = sprintf($sprintf_pattern, $num);
  902.             }
  903.             $pattern = preg_replace($number_regex, $formatted, $pattern);
  904.         }
  905.  
  906.         return array(
  907.             'string'=>$pattern,
  908.             'formatColor'=>$color
  909.         );
  910.     }
  911.  
  912.     /**
  913.      * Constructor
  914.      *
  915.      * Some basic initialisation
  916.      */
  917.     function Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding='') {
  918.         $t = new OLERead();
  919.         $this->_ole =& $t;
  920.         //$this->_ole =& new OLERead();
  921.         $this->setUTFEncoder('iconv');
  922.         if ($outputEncoding != '') {
  923.             $this->setOutputEncoding($outputEncoding);
  924.         }
  925.         for ($i=1; $i<245; $i++) {
  926.             $name = strtolower(( (($i-1)/26>=1)?chr(($i-1)/26+64):'') . chr(($i-1)%26+65));
  927.             $this->colnames[$name] = $i;
  928.             $this->colindexes[$i] = $name;
  929.         }
  930.         $this->store_extended_info = $store_extended_info;
  931.         if ($file!="") {
  932.             $this->read($file);
  933.         }
  934.     }
  935.  
  936.     /**
  937.      * Set the encoding method
  938.      */
  939.     function setOutputEncoding($encoding) {
  940.         $this->_defaultEncoding = $encoding;
  941.     }
  942.  
  943.     /**
  944.      *  $encoder = 'iconv' or 'mb'
  945.      *  set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
  946.      *  set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
  947.      */
  948.     function setUTFEncoder($encoder = 'iconv') {
  949.         $this->_encoderFunction = '';
  950.         if ($encoder == 'iconv') {
  951.             $this->_encoderFunction = function_exists('iconv') ? 'iconv' : '';
  952.         } elseif ($encoder == 'mb') {
  953.             $this->_encoderFunction = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '';
  954.         }
  955.     }
  956.  
  957.     function setRowColOffset($iOffset) {
  958.         $this->_rowoffset = $iOffset;
  959.         $this->_coloffset = $iOffset;
  960.     }
  961.  
  962.     /**
  963.      * Set the default number format
  964.      */
  965.     function setDefaultFormat($sFormat) {
  966.         $this->_defaultFormat = $sFormat;
  967.     }
  968.  
  969.     /**
  970.      * Force a column to use a certain format
  971.      */
  972.     function setColumnFormat($column, $sFormat) {
  973.         $this->_columnsFormat[$column] = $sFormat;
  974.     }
  975.  
  976.     /**
  977.      * Read the spreadsheet file using OLE, then parse
  978.      */
  979.     function read($sFileName) {
  980.         $res = $this->_ole->read($sFileName);
  981.  
  982.         // oops, something goes wrong (Darko Miljanovic)
  983.         if($res === false) {
  984.             // check error code
  985.             if($this->_ole->error == 1) {
  986.                 // bad file
  987.                 die('The filename ' . $sFileName . ' is not readable');
  988.             }
  989.             // check other error codes here (eg bad fileformat, etc...)
  990.         }
  991.         $this->data = $this->_ole->getWorkBook();
  992.         $this->_parse();
  993.     }
  994.  
  995.     /**
  996.      * Parse a workbook
  997.      *
  998.      * @access private
  999.      * @return bool
  1000.      */
  1001.     function _parse() {
  1002.         $pos = 0;
  1003.         $data = $this->data;
  1004.  
  1005.         $code = v($data,$pos);
  1006.         $length = v($data,$pos+2);
  1007.         $version = v($data,$pos+4);
  1008.         $substreamType = v($data,$pos+6);
  1009.  
  1010.         $this->version = $version;
  1011.  
  1012.         if (($version != SPREADSHEET_EXCEL_READER_BIFF8) &&
  1013.             ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  1014.             return false;
  1015.         }
  1016.  
  1017.         if ($substreamType != SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS){
  1018.             return false;
  1019.         }
  1020.  
  1021.         $pos += $length + 4;
  1022.  
  1023.         $code = v($data,$pos);
  1024.         $length = v($data,$pos+2);
  1025.  
  1026.         while ($code != SPREADSHEET_EXCEL_READER_TYPE_EOF) {
  1027.             switch ($code) {
  1028.                 case SPREADSHEET_EXCEL_READER_TYPE_SST:
  1029.                     $spos = $pos + 4;
  1030.                     $limitpos = $spos + $length;
  1031.                     $uniqueStrings = $this->_GetInt4d($data, $spos+4);
  1032.                     $spos += 8;
  1033.                     for ($i = 0; $i < $uniqueStrings; $i++) {
  1034.                         // Read in the number of characters
  1035.                         if ($spos == $limitpos) {
  1036.                             $opcode = v($data,$spos);
  1037.                             $conlength = v($data,$spos+2);
  1038.                             if ($opcode != 0x3c) {
  1039.                                 return -1;
  1040.                             }
  1041.                             $spos += 4;
  1042.                             $limitpos = $spos + $conlength;
  1043.                         }
  1044.                         $numChars = ord($data[$spos]) | (ord($data[$spos+1]) << 8);
  1045.                         $spos += 2;
  1046.                         $optionFlags = ord($data[$spos]);
  1047.                         $spos++;
  1048.                         $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  1049.                         $extendedString = ( ($optionFlags & 0x04) != 0);
  1050.  
  1051.                         // See if string contains formatting information
  1052.                         $richString = ( ($optionFlags & 0x08) != 0);
  1053.  
  1054.                         if ($richString) {
  1055.                             // Read in the crun
  1056.                             $formattingRuns = v($data,$spos);
  1057.                             $spos += 2;
  1058.                         }
  1059.  
  1060.                         if ($extendedString) {
  1061.                             // Read in cchExtRst
  1062.                             $extendedRunLength = $this->_GetInt4d($data, $spos);
  1063.                             $spos += 4;
  1064.                         }
  1065.  
  1066.                         $len = ($asciiEncoding)? $numChars : $numChars*2;
  1067.                         if ($spos + $len < $limitpos) {
  1068.                             $retstr = substr($data, $spos, $len);
  1069.                             $spos += $len;
  1070.                         }
  1071.                         else{
  1072.                             // found countinue
  1073.                             $retstr = substr($data, $spos, $limitpos - $spos);
  1074.                             $bytesRead = $limitpos - $spos;
  1075.                             $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  1076.                             $spos = $limitpos;
  1077.  
  1078.                             while ($charsLeft > 0){
  1079.                                 $opcode = v($data,$spos);
  1080.                                 $conlength = v($data,$spos+2);
  1081.                                 if ($opcode != 0x3c) {
  1082.                                     return -1;
  1083.                                 }
  1084.                                 $spos += 4;
  1085.                                 $limitpos = $spos + $conlength;
  1086.                                 $option = ord($data[$spos]);
  1087.                                 $spos += 1;
  1088.                                 if ($asciiEncoding && ($option == 0)) {
  1089.                                     $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  1090.                                     $retstr .= substr($data, $spos, $len);
  1091.                                     $charsLeft -= $len;
  1092.                                     $asciiEncoding = true;
  1093.                                 }
  1094.                                 elseif (!$asciiEncoding && ($option != 0)) {
  1095.                                     $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  1096.                                     $retstr .= substr($data, $spos, $len);
  1097.                                     $charsLeft -= $len/2;
  1098.                                     $asciiEncoding = false;
  1099.                                 }
  1100.                                 elseif (!$asciiEncoding && ($option == 0)) {
  1101.                                     // Bummer - the string starts off as Unicode, but after the
  1102.                                     // continuation it is in straightforward ASCII encoding
  1103.                                     $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  1104.                                     for ($j = 0; $j < $len; $j++) {
  1105.                                         $retstr .= $data[$spos + $j].chr(0);
  1106.                                     }
  1107.                                     $charsLeft -= $len;
  1108.                                     $asciiEncoding = false;
  1109.                                 }
  1110.                                 else{
  1111.                                     $newstr = '';
  1112.                                     for ($j = 0; $j < strlen($retstr); $j++) {
  1113.                                         $newstr = $retstr[$j].chr(0);
  1114.                                     }
  1115.                                     $retstr = $newstr;
  1116.                                     $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  1117.                                     $retstr .= substr($data, $spos, $len);
  1118.                                     $charsLeft -= $len/2;
  1119.                                     $asciiEncoding = false;
  1120.                                 }
  1121.                                 $spos += $len;
  1122.                             }
  1123.                         }
  1124.                         $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);
  1125.  
  1126.                         if ($richString){
  1127.                             $spos += 4 * $formattingRuns;
  1128.                         }
  1129.  
  1130.                         // For extended strings, skip over the extended string data
  1131.                         if ($extendedString) {
  1132.                             $spos += $extendedRunLength;
  1133.                         }
  1134.                         $this->sst[]=$retstr;
  1135.                     }
  1136.                     break;
  1137.                 case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS:
  1138.                     return false;
  1139.                     break;
  1140.                 case SPREADSHEET_EXCEL_READER_TYPE_NAME:
  1141.                     break;
  1142.                 case SPREADSHEET_EXCEL_READER_TYPE_FORMAT:
  1143.                     $indexCode = v($data,$pos+4);
  1144.                     if ($version == SPREADSHEET_EXCEL_READER_BIFF8) {
  1145.                         $numchars = v($data,$pos+6);
  1146.                         if (ord($data[$pos+8]) == 0){
  1147.                             $formatString = substr($data, $pos+9, $numchars);
  1148.                         } else {
  1149.                             $formatString = substr($data, $pos+9, $numchars*2);
  1150.                         }
  1151.                     } else {
  1152.                         $numchars = ord($data[$pos+6]);
  1153.                         $formatString = substr($data, $pos+7, $numchars*2);
  1154.                     }
  1155.                     $this->formatRecords[$indexCode] = $formatString;
  1156.                     break;
  1157.                 case SPREADSHEET_EXCEL_READER_TYPE_FONT:
  1158.                         $height = v($data,$pos+4);
  1159.                         $option = v($data,$pos+6);
  1160.                         $color = v($data,$pos+8);
  1161.                         $weight = v($data,$pos+10);
  1162.                         $under  = ord($data[$pos+14]);
  1163.                         $font = "";
  1164.                         // Font name
  1165.                         $numchars = ord($data[$pos+18]);
  1166.                         if ((ord($data[$pos+19]) & 1) == 0){
  1167.                             $font = substr($data, $pos+20, $numchars);
  1168.                         } else {
  1169.                             $font = substr($data, $pos+20, $numchars*2);
  1170.                             $font =  $this->_encodeUTF16($font);
  1171.                         }
  1172.                         $this->fontRecords[] = array(
  1173.                                 'height' => $height / 20,
  1174.                                 'italic' => !!($option & 2),
  1175.                                 'color' => $color,
  1176.                                 'under' => !($under==0),
  1177.                                 'bold' => ($weight==700),
  1178.                                 'font' => $font,
  1179.                                 'raw' => $this->dumpHexData($data, $pos+3, $length)
  1180.                                 );
  1181.                         break;
  1182.  
  1183.                 case SPREADSHEET_EXCEL_READER_TYPE_PALETTE:
  1184.                         $colors = ord($data[$pos+4]) | ord($data[$pos+5]) << 8;
  1185.                         for ($coli = 0; $coli < $colors; $coli++) {
  1186.                             $colOff = $pos + 2 + ($coli * 4);
  1187.                             $colr = ord($data[$colOff]);
  1188.                             $colg = ord($data[$colOff+1]);
  1189.                             $colb = ord($data[$colOff+2]);
  1190.                             $this->colors[0x07 + $coli] = '#' . $this->myhex($colr) . $this->myhex($colg) . $this->myhex($colb);
  1191.                         }
  1192.                         break;
  1193.  
  1194.                 case SPREADSHEET_EXCEL_READER_TYPE_XF:
  1195.                         $fontIndexCode = (ord($data[$pos+4]) | ord($data[$pos+5]) << 8) - 1;
  1196.                         $fontIndexCode = max(0,$fontIndexCode);
  1197.                         $indexCode = ord($data[$pos+6]) | ord($data[$pos+7]) << 8;
  1198.                         $alignbit = ord($data[$pos+10]) & 3;
  1199.                         $bgi = (ord($data[$pos+22]) | ord($data[$pos+23]) << 8) & 0x3FFF;
  1200.                         $bgcolor = ($bgi & 0x7F);
  1201. //                      $bgcolor = ($bgi & 0x3f80) >> 7;
  1202.                         $align = "";
  1203.                         if ($alignbit==3) { $align="right"; }
  1204.                         if ($alignbit==2) { $align="center"; }
  1205.  
  1206.                         $fillPattern = (ord($data[$pos+21]) & 0xFC) >> 2;
  1207.                         if ($fillPattern == 0) {
  1208.                             $bgcolor = "";
  1209.                         }
  1210.  
  1211.                         $xf = array();
  1212.                         $xf['formatIndex'] = $indexCode;
  1213.                         $xf['align'] = $align;
  1214.                         $xf['fontIndex'] = $fontIndexCode;
  1215.                         $xf['bgColor'] = $bgcolor;
  1216.                         $xf['fillPattern'] = $fillPattern;
  1217.  
  1218.                         $border = ord($data[$pos+14]) | (ord($data[$pos+15]) << 8) | (ord($data[$pos+16]) << 16) | (ord($data[$pos+17]) << 24);
  1219.                         $xf['borderLeft'] = $this->lineStyles[($border & 0xF)];
  1220.                         $xf['borderRight'] = $this->lineStyles[($border & 0xF0) >> 4];
  1221.                         $xf['borderTop'] = $this->lineStyles[($border & 0xF00) >> 8];
  1222.                         $xf['borderBottom'] = $this->lineStyles[($border & 0xF000) >> 12];
  1223.                        
  1224.                         $xf['borderLeftColor'] = ($border & 0x7F0000) >> 16;
  1225.                         $xf['borderRightColor'] = ($border & 0x3F800000) >> 23;
  1226.                         $border = (ord($data[$pos+18]) | ord($data[$pos+19]) << 8);
  1227.  
  1228.                         $xf['borderTopColor'] = ($border & 0x7F);
  1229.                         $xf['borderBottomColor'] = ($border & 0x3F80) >> 7;
  1230.                                                
  1231.                         if (array_key_exists($indexCode, $this->dateFormats)) {
  1232.                             $xf['type'] = 'date';
  1233.                             $xf['format'] = $this->dateFormats[$indexCode];
  1234.                             if ($align=='') { $xf['align'] = 'right'; }
  1235.                         }elseif (array_key_exists($indexCode, $this->numberFormats)) {
  1236.                             $xf['type'] = 'number';
  1237.                             $xf['format'] = $this->numberFormats[$indexCode];
  1238.                             if ($align=='') { $xf['align'] = 'right'; }
  1239.                         }else{
  1240.                             $isdate = FALSE;
  1241.                             $formatstr = '';
  1242.                             if ($indexCode > 0){
  1243.                                 if (isset($this->formatRecords[$indexCode]))
  1244.                                     $formatstr = $this->formatRecords[$indexCode];
  1245.                                 if ($formatstr!="") {
  1246.                                     $tmp = preg_replace("/\;.*/","",$formatstr);
  1247.                                     $tmp = preg_replace("/^\[[^\]]*\]/","",$tmp);
  1248.                                     if (preg_match("/[^hmsday\/\-:\s\\\,AMP]/i", $tmp) == 0) { // found day and time format
  1249.                                         $isdate = TRUE;
  1250.                                         $formatstr = $tmp;
  1251.                                         $formatstr = str_replace(array('AM/PM','mmmm','mmm'), array('a','F','M'), $formatstr);
  1252.                                         // m/mm are used for both minutes and months - oh SNAP!
  1253.                                         // This mess tries to fix for that.
  1254.                                         // 'm' == minutes only if following h/hh or preceding s/ss
  1255.                                         $formatstr = preg_replace("/(h:?)mm?/","$1i", $formatstr);
  1256.                                         $formatstr = preg_replace("/mm?(:?s)/","i$1", $formatstr);
  1257.                                         // A single 'm' = n in PHP
  1258.                                         $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1259.                                         $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1260.                                         // else it's months
  1261.                                         $formatstr = str_replace('mm', 'm', $formatstr);
  1262.                                         // Convert single 'd' to 'j'
  1263.                                         $formatstr = preg_replace("/(^|[^d])d([^d]|$)/", '$1j$2', $formatstr);
  1264.                                         $formatstr = str_replace(array('dddd','ddd','dd','yyyy','yy','hh','h'), array('l','D','d','Y','y','H','g'), $formatstr);
  1265.                                         $formatstr = preg_replace("/ss?/", 's', $formatstr);
  1266.                                         //New Line 2015-11-25 Add
  1267.                                         $formatstr = str_replace( array( 'DD', 'MM', 'YYYY', 'HH', ), array( 'd', 'm', 'y', 'H' ), $formatstr );                                   
  1268.                                     }
  1269.                                 }
  1270.                             }
  1271.                             if ($isdate){
  1272.                                 $xf['type'] = 'date';
  1273.                                 $xf['format'] = $formatstr;
  1274.                                 if ($align=='') { $xf['align'] = 'right'; }
  1275.                             }else{
  1276.                                 // If the format string has a 0 or # in it, we'll assume it's a number
  1277.                                 if (preg_match("/[0#]/", $formatstr)) {
  1278.                                     $xf['type'] = 'number';
  1279.                                     if ($align=='') { $xf['align']='right'; }
  1280.                                 }
  1281.                                 else {
  1282.                                 $xf['type'] = 'other';
  1283.                                 }
  1284.                                 $xf['format'] = $formatstr;
  1285.                                 $xf['code'] = $indexCode;
  1286.                             }
  1287.                         }
  1288.                         $this->xfRecords[] = $xf;
  1289.                     break;
  1290.                 case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR:
  1291.                     $this->nineteenFour = (ord($data[$pos+4]) == 1);
  1292.                     break;
  1293.                 case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET:
  1294.                         $rec_offset = $this->_GetInt4d($data, $pos+4);
  1295.                         $rec_typeFlag = ord($data[$pos+8]);
  1296.                         $rec_visibilityFlag = ord($data[$pos+9]);
  1297.                         $rec_length = ord($data[$pos+10]);
  1298.  
  1299.                         if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1300.                             $chartype =  ord($data[$pos+11]);
  1301.                             if ($chartype == 0){
  1302.                                 $rec_name   = substr($data, $pos+12, $rec_length);
  1303.                             } else {
  1304.                                 $rec_name   = $this->_encodeUTF16(substr($data, $pos+12, $rec_length*2));
  1305.                             }
  1306.                         }elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1307.                                 $rec_name   = substr($data, $pos+11, $rec_length);
  1308.                         }
  1309.                     $this->boundsheets[] = array('name'=>$rec_name,'offset'=>$rec_offset);
  1310.                     break;
  1311.  
  1312.             }
  1313.  
  1314.             $pos += $length + 4;
  1315.             $code = ord($data[$pos]) | ord($data[$pos+1])<<8;
  1316.             $length = ord($data[$pos+2]) | ord($data[$pos+3])<<8;
  1317.         }
  1318.  
  1319.         foreach ($this->boundsheets as $key=>$val){
  1320.             $this->sn = $key;
  1321.             $this->_parsesheet($val['offset']);
  1322.         }
  1323.         return true;
  1324.     }
  1325.  
  1326.     /**
  1327.      * Parse a worksheet
  1328.      */
  1329.     function _parsesheet($spos) {
  1330.         $cont = true;
  1331.         $data = $this->data;
  1332.         // read BOF
  1333.         $code = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1334.         $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1335.  
  1336.         $version = ord($data[$spos + 4]) | ord($data[$spos + 5])<<8;
  1337.         $substreamType = ord($data[$spos + 6]) | ord($data[$spos + 7])<<8;
  1338.  
  1339.         if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  1340.             return -1;
  1341.         }
  1342.  
  1343.         if ($substreamType != SPREADSHEET_EXCEL_READER_WORKSHEET){
  1344.             return -2;
  1345.         }
  1346.         $spos += $length + 4;
  1347.         while($cont) {
  1348.             $lowcode = ord($data[$spos]);
  1349.             if ($lowcode == SPREADSHEET_EXCEL_READER_TYPE_EOF) break;
  1350.             $code = $lowcode | ord($data[$spos+1])<<8;
  1351.             $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1352.             $spos += 4;
  1353.             $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
  1354.             $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
  1355.             unset($this->rectype);
  1356.             switch ($code) {
  1357.                 case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION:
  1358.                     if (!isset($this->numRows)) {
  1359.                         if (($length == 10) ||  ($version == SPREADSHEET_EXCEL_READER_BIFF7)){
  1360.                             $this->sheets[$this->sn]['numRows'] = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1361.                             $this->sheets[$this->sn]['numCols'] = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1362.                         } else {
  1363.                             $this->sheets[$this->sn]['numRows'] = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1364.                             $this->sheets[$this->sn]['numCols'] = ord($data[$spos+10]) | ord($data[$spos+11]) << 8;
  1365.                         }
  1366.                     }
  1367.                     break;
  1368.                 case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS:
  1369.                     $cellRanges = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1370.                     for ($i = 0; $i < $cellRanges; $i++) {
  1371.                         $fr =  ord($data[$spos + 8*$i + 2]) | ord($data[$spos + 8*$i + 3])<<8;
  1372.                         $lr =  ord($data[$spos + 8*$i + 4]) | ord($data[$spos + 8*$i + 5])<<8;
  1373.                         $fc =  ord($data[$spos + 8*$i + 6]) | ord($data[$spos + 8*$i + 7])<<8;
  1374.                         $lc =  ord($data[$spos + 8*$i + 8]) | ord($data[$spos + 8*$i + 9])<<8;
  1375.                         if ($lr - $fr > 0) {
  1376.                             $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
  1377.                         }
  1378.                         if ($lc - $fc > 0) {
  1379.                             $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
  1380.                         }
  1381.                     }
  1382.                     break;
  1383.                 case SPREADSHEET_EXCEL_READER_TYPE_RK:
  1384.                 case SPREADSHEET_EXCEL_READER_TYPE_RK2:
  1385.                     $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1386.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1387.                     $rknum = $this->_GetInt4d($data, $spos + 6);
  1388.                     $numValue = $this->_GetIEEE754($rknum);
  1389.                     $info = $this->_getCellDetails($spos,$numValue,$column);
  1390.                     $this->addcell($row, $column, $info['string'],$info);
  1391.                     break;
  1392.                 case SPREADSHEET_EXCEL_READER_TYPE_LABELSST:
  1393.                     $row        = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1394.                     $column  = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1395.                     $xfindex    = ord($data[$spos+4]) | ord($data[$spos+5])<<8;
  1396.                     $index  = $this->_GetInt4d($data, $spos + 6);
  1397.                     $this->addcell($row, $column, $this->sst[$index], array('xfIndex'=>$xfindex) );
  1398.                     break;
  1399.                 case SPREADSHEET_EXCEL_READER_TYPE_MULRK:
  1400.                     $row        = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1401.                     $colFirst   = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1402.                     $colLast    = ord($data[$spos + $length - 2]) | ord($data[$spos + $length - 1])<<8;
  1403.                     $columns    = $colLast - $colFirst + 1;
  1404.                     $tmppos = $spos+4;
  1405.                     for ($i = 0; $i < $columns; $i++) {
  1406.                         $numValue = $this->_GetIEEE754($this->_GetInt4d($data, $tmppos + 2));
  1407.                         $info = $this->_getCellDetails($tmppos-4,$numValue,$colFirst + $i + 1);
  1408.                         $tmppos += 6;
  1409.                         $this->addcell($row, $colFirst + $i, $info['string'], $info);
  1410.                     }
  1411.                     break;
  1412.                 case SPREADSHEET_EXCEL_READER_TYPE_NUMBER:
  1413.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1414.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1415.                     $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1416.                     if ($this->isDate($spos)) {
  1417.                         $numValue = $tmp['double'];
  1418.                     }
  1419.                     else {
  1420.                         $numValue = $this->createNumber($spos);
  1421.                     }
  1422.                     $info = $this->_getCellDetails($spos,$numValue,$column);
  1423.                     $this->addcell($row, $column, $info['string'], $info);
  1424.                     break;
  1425.  
  1426.                 case SPREADSHEET_EXCEL_READER_TYPE_FORMULA:
  1427.                 case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2:
  1428.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1429.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1430.                     if ((ord($data[$spos+6])==0) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1431.                         //String formula. Result follows in a STRING record
  1432.                         // This row/col are stored to be referenced in that record
  1433.                         // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1434.                         $previousRow = $row;
  1435.                         $previousCol = $column;
  1436.                     } elseif ((ord($data[$spos+6])==1) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1437.                         //Boolean formula. Result is in +2; 0=false,1=true
  1438.                         // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1439.                         if (ord($this->data[$spos+8])==1) {
  1440.                             $this->addcell($row, $column, "TRUE");
  1441.                         } else {
  1442.                             $this->addcell($row, $column, "FALSE");
  1443.                         }
  1444.                     } elseif ((ord($data[$spos+6])==2) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1445.                         //Error formula. Error code is in +2;
  1446.                     } elseif ((ord($data[$spos+6])==3) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1447.                         //Formula result is a null string.
  1448.                         $this->addcell($row, $column, '');
  1449.                     } else {
  1450.                         // result is a number, so first 14 bytes are just like a _NUMBER record
  1451.                         $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1452.                               if ($this->isDate($spos)) {
  1453.                                 $numValue = $tmp['double'];
  1454.                               }
  1455.                               else {
  1456.                                 $numValue = $this->createNumber($spos);
  1457.                               }
  1458.                         $info = $this->_getCellDetails($spos,$numValue,$column);
  1459.                         $this->addcell($row, $column, $info['string'], $info);
  1460.                     }
  1461.                     break;
  1462.                 case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR:
  1463.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1464.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1465.                     $string = ord($data[$spos+6]);
  1466.                     $this->addcell($row, $column, $string);
  1467.                     break;
  1468.                 case SPREADSHEET_EXCEL_READER_TYPE_STRING:
  1469.                     // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1470.                     if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1471.                         // Unicode 16 string, like an SST record
  1472.                         $xpos = $spos;
  1473.                         $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1474.                         $xpos += 2;
  1475.                         $optionFlags =ord($data[$xpos]);
  1476.                         $xpos++;
  1477.                         $asciiEncoding = (($optionFlags &0x01) == 0) ;
  1478.                         $extendedString = (($optionFlags & 0x04) != 0);
  1479.                         // See if string contains formatting information
  1480.                         $richString = (($optionFlags & 0x08) != 0);
  1481.                         if ($richString) {
  1482.                             // Read in the crun
  1483.                             $formattingRuns =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1484.                             $xpos += 2;
  1485.                         }
  1486.                         if ($extendedString) {
  1487.                             // Read in cchExtRst
  1488.                             $extendedRunLength =$this->_GetInt4d($this->data, $xpos);
  1489.                             $xpos += 4;
  1490.                         }
  1491.                         $len = ($asciiEncoding)?$numChars : $numChars*2;
  1492.                         $retstr =substr($data, $xpos, $len);
  1493.                         $xpos += $len;
  1494.                         $retstr = ($asciiEncoding)? $retstr : $this->_encodeUTF16($retstr);
  1495.                     }
  1496.                     elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1497.                         // Simple byte string
  1498.                         $xpos = $spos;
  1499.                         $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1500.                         $xpos += 2;
  1501.                         $retstr =substr($data, $xpos, $numChars);
  1502.                     }
  1503.                     $this->addcell($previousRow, $previousCol, $retstr);
  1504.                     break;
  1505.                 case SPREADSHEET_EXCEL_READER_TYPE_ROW:
  1506.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1507.                     $rowInfo = ord($data[$spos + 6]) | ((ord($data[$spos+7]) << 8) & 0x7FFF);
  1508.                     if (($rowInfo & 0x8000) > 0) {
  1509.                         $rowHeight = -1;
  1510.                     } else {
  1511.                         $rowHeight = $rowInfo & 0x7FFF;
  1512.                     }
  1513.                     $rowHidden = (ord($data[$spos + 12]) & 0x20) >> 5;
  1514.                     $this->rowInfo[$this->sn][$row+1] = Array('height' => $rowHeight / 20, 'hidden'=>$rowHidden );
  1515.                     break;
  1516.                 case SPREADSHEET_EXCEL_READER_TYPE_DBCELL:
  1517.                     break;
  1518.                 case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK:
  1519.                     $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1520.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1521.                     $cols = ($length / 2) - 3;
  1522.                     for ($c = 0; $c < $cols; $c++) {
  1523.                         $xfindex = ord($data[$spos + 4 + ($c * 2)]) | ord($data[$spos + 5 + ($c * 2)])<<8;
  1524.                         $this->addcell($row, $column + $c, "", array('xfIndex'=>$xfindex));
  1525.                     }
  1526.                     break;
  1527.                 case SPREADSHEET_EXCEL_READER_TYPE_LABEL:
  1528.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1529.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1530.                     $this->addcell($row, $column, substr($data, $spos + 8, ord($data[$spos + 6]) | ord($data[$spos + 7])<<8));
  1531.                     break;
  1532.                 case SPREADSHEET_EXCEL_READER_TYPE_EOF:
  1533.                     $cont = false;
  1534.                     break;
  1535.                 case SPREADSHEET_EXCEL_READER_TYPE_HYPER:
  1536.                     //  Only handle hyperlinks to a URL
  1537.                     $row    = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  1538.                     $row2   = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  1539.                     $column = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8;
  1540.                     $column2 = ord($this->data[$spos+6]) | ord($this->data[$spos+7])<<8;
  1541.                     $linkdata = Array();
  1542.                     $flags = ord($this->data[$spos + 28]);
  1543.                     $udesc = "";
  1544.                     $ulink = "";
  1545.                     $uloc = 32;
  1546.                     $linkdata['flags'] = $flags;
  1547.                     if (($flags & 1) > 0 ) {   // is a type we understand
  1548.                         //  is there a description ?
  1549.                         if (($flags & 0x14) == 0x14 ) {   // has a description
  1550.                             $uloc += 4;
  1551.                             $descLen = ord($this->data[$spos + 32]) | ord($this->data[$spos + 33]) << 8;
  1552.                             $udesc = substr($this->data, $spos + $uloc, $descLen * 2);
  1553.                             $uloc += 2 * $descLen;
  1554.                         }
  1555.                         $ulink = $this->read16bitstring($this->data, $spos + $uloc + 20);
  1556.                         if ($udesc == "") {
  1557.                             $udesc = $ulink;
  1558.                         }
  1559.                     }
  1560.                     $linkdata['desc'] = $udesc;
  1561.                     $linkdata['link'] = $this->_encodeUTF16($ulink);
  1562.                     for ($r=$row; $r<=$row2; $r++) {
  1563.                         for ($c=$column; $c<=$column2; $c++) {
  1564.                             $this->sheets[$this->sn]['cellsInfo'][$r+1][$c+1]['hyperlink'] = $linkdata;
  1565.                         }
  1566.                     }
  1567.                     break;
  1568.                 case SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH:
  1569.                     $this->defaultColWidth  = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1570.                     break;
  1571.                 case SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH:
  1572.                     $this->standardColWidth  = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1573.                     break;
  1574.                 case SPREADSHEET_EXCEL_READER_TYPE_COLINFO:
  1575.                     $colfrom = ord($data[$spos+0]) | ord($data[$spos+1]) << 8;
  1576.                     $colto = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1577.                     $cw = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1578.                     $cxf = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1579.                     $co = ord($data[$spos+8]);
  1580.                     for ($coli = $colfrom; $coli <= $colto; $coli++) {
  1581.                         $this->colInfo[$this->sn][$coli+1] = Array('width' => $cw, 'xf' => $cxf, 'hidden' => ($co & 0x01), 'collapsed' => ($co & 0x1000) >> 12);
  1582.                     }
  1583.                     break;
  1584.  
  1585.                 default:
  1586.                     break;
  1587.             }
  1588.             $spos += $length;
  1589.         }
  1590.  
  1591.         if (!isset($this->sheets[$this->sn]['numRows']))
  1592.              $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow'];
  1593.         if (!isset($this->sheets[$this->sn]['numCols']))
  1594.              $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol'];
  1595.         }
  1596.  
  1597.         function isDate($spos) {
  1598.             $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  1599.             return ($this->xfRecords[$xfindex]['type'] == 'date');
  1600.         }
  1601.  
  1602.         // Get the details for a particular cell
  1603.         function _getCellDetails($spos,$numValue,$column) {
  1604.             $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  1605.             $xfrecord = $this->xfRecords[$xfindex];
  1606.             $type = $xfrecord['type'];
  1607.  
  1608.             $format = $xfrecord['format'];
  1609.             $formatIndex = $xfrecord['formatIndex'];
  1610.             $fontIndex = $xfrecord['fontIndex'];
  1611.             $formatColor = "";
  1612.             $rectype = '';
  1613.             $string = '';
  1614.             $raw = '';
  1615.  
  1616.             if (isset($this->_columnsFormat[$column + 1])){
  1617.                 $format = $this->_columnsFormat[$column + 1];
  1618.             }
  1619.  
  1620.             if ($type == 'date') {
  1621.                 // See http://groups.google.com/group/php-excel-reader-discuss/browse_frm/thread/9c3f9790d12d8e10/f2045c2369ac79de
  1622.                 $rectype = 'date';
  1623.                 // Convert numeric value into a date
  1624.                 $utcDays = floor($numValue - ($this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS));
  1625.                 $utcValue = ($utcDays) * SPREADSHEET_EXCEL_READER_MSINADAY;
  1626.                 $dateinfo = gmgetdate($utcValue);
  1627.  
  1628.                 $raw = $numValue;
  1629.                 $fractionalDay = $numValue - floor($numValue) + .0000001; // The .0000001 is to fix for php/excel fractional diffs
  1630.  
  1631.                 $totalseconds = floor(SPREADSHEET_EXCEL_READER_MSINADAY * $fractionalDay);
  1632.                 $secs = $totalseconds % 60;
  1633.                 $totalseconds -= $secs;
  1634.                 $hours = floor($totalseconds / (60 * 60));
  1635.                 $mins = floor($totalseconds / 60) % 60;
  1636.                 $string = date ($format, mktime($hours, $mins, $secs, $dateinfo["mon"], $dateinfo["mday"], $dateinfo["year"]));
  1637.             } else if ($type == 'number') {
  1638.                 $rectype = 'number';
  1639.                 $formatted = $this->_format_value($format, $numValue, $formatIndex);
  1640.                 $string = $formatted['string'];
  1641.                 $formatColor = $formatted['formatColor'];
  1642.                 $raw = $numValue;
  1643.             } else{
  1644.                 if ($format=="") {
  1645.                     $format = $this->_defaultFormat;
  1646.                 }
  1647.                 $rectype = 'unknown';
  1648.                 $formatted = $this->_format_value($format, $numValue, $formatIndex);
  1649.                 $string = $formatted['string'];
  1650.                 $formatColor = $formatted['formatColor'];
  1651.                 $raw = $numValue;
  1652.             }
  1653.  
  1654.             return array(
  1655.                 'string'=>$string,
  1656.                 'raw'=>$raw,
  1657.                 'rectype'=>$rectype,
  1658.                 'format'=>$format,
  1659.                 'formatIndex'=>$formatIndex,
  1660.                 'fontIndex'=>$fontIndex,
  1661.                 'formatColor'=>$formatColor,
  1662.                 'xfIndex'=>$xfindex
  1663.             );
  1664.  
  1665.         }
  1666.  
  1667.  
  1668.     function createNumber($spos) {
  1669.         $rknumhigh = $this->_GetInt4d($this->data, $spos + 10);
  1670.         $rknumlow = $this->_GetInt4d($this->data, $spos + 6);
  1671.         $sign = ($rknumhigh & 0x80000000) >> 31;
  1672.         $exp =  ($rknumhigh & 0x7ff00000) >> 20;
  1673.         $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  1674.         $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  1675.         $mantissalow2 = ($rknumlow & 0x7fffffff);
  1676.         $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1677.         if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023)));
  1678.         $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  1679.         if ($sign) {$value = -1 * $value;}
  1680.         return  $value;
  1681.     }
  1682.  
  1683.     function addcell($row, $col, $string, $info=null) {
  1684.         $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset);
  1685.         $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset);
  1686.         $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string;
  1687.         if ($this->store_extended_info && $info) {
  1688.             foreach ($info as $key=>$val) {
  1689.                 $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset][$key] = $val;
  1690.             }
  1691.         }
  1692.     }
  1693.  
  1694.  
  1695.     function _GetIEEE754($rknum) {
  1696.         if (($rknum & 0x02) != 0) {
  1697.                 $value = $rknum >> 2;
  1698.         } else {
  1699.             //mmp
  1700.             // I got my info on IEEE754 encoding from
  1701.             // http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  1702.             // The RK format calls for using only the most significant 30 bits of the
  1703.             // 64 bit floating point value. The other 34 bits are assumed to be 0
  1704.             // So, we use the upper 30 bits of $rknum as follows...
  1705.             $sign = ($rknum & 0x80000000) >> 31;
  1706.             $exp = ($rknum & 0x7ff00000) >> 20;
  1707.             $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  1708.             $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1709.             if ($sign) {
  1710.                 $value = -1 * $value;
  1711.             }
  1712.             //end of changes by mmp
  1713.         }
  1714.         if (($rknum & 0x01) != 0) {
  1715.             $value /= 100;
  1716.         }
  1717.         return $value;
  1718.     }
  1719.  
  1720.     function _encodeUTF16($string) {
  1721.         $result = $string;
  1722.         if ($this->_defaultEncoding){
  1723.             switch ($this->_encoderFunction){
  1724.                 case 'iconv' :   $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
  1725.                                 break;
  1726.                 case 'mb_convert_encoding' :     $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' );
  1727.                                 break;
  1728.             }
  1729.         }
  1730.         return $result;
  1731.     }
  1732.  
  1733.     function _GetInt4d($data, $pos) {
  1734.         $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  1735.         if ($value>=4294967294) {
  1736.             $value=-2;
  1737.         }
  1738.         return $value;
  1739.     }
  1740.  
  1741. }
  1742.  
  1743. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement