selimppc

Spreadsheet Excel Reader in PHP

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