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