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