PageRenderTime 90ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/framework/lib/classes/PHPExcel/Reader/Excel5.php

https://bitbucket.org/designbyheart/original
PHP | 6667 lines | 3767 code | 1018 blank | 1882 comment | 492 complexity | 0be0e05c81062da5161e5cdf36e4a8c5 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader_Excel5
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. // Original file header of ParseXL (used as the base for this class):
  28. // --------------------------------------------------------------------------------
  29. // Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
  30. // trex005, and mmp11 (SourceForge.net)
  31. // http://sourceforge.net/projects/phpexcelreader/
  32. // Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
  33. // Modelled moreso after Perl Excel Parse/Write modules
  34. // Added Parse_Excel_Spreadsheet object
  35. // Reads a whole worksheet or tab as row,column array or as
  36. // associated hash of indexed rows and named column fields
  37. // Added variables for worksheet (tab) indexes and names
  38. // Added an object call for loading individual woorksheets
  39. // Changed default indexing defaults to 0 based arrays
  40. // Fixed date/time and percent formats
  41. // Includes patches found at SourceForge...
  42. // unicode patch by nobody
  43. // unpack("d") machine depedency patch by matchy
  44. // boundsheet utf16 patch by bjaenichen
  45. // Renamed functions for shorter names
  46. // General code cleanup and rigor, including <80 column width
  47. // Included a testcase Excel file and PHP example calls
  48. // Code works for PHP 5.x
  49. // Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
  50. // http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
  51. // Decoding of formula conditions, results, and tokens.
  52. // Support for user-defined named cells added as an array "namedcells"
  53. // Patch code for user-defined named cells supports single cells only.
  54. // NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
  55. // external sheet reference structure
  56. /** PHPExcel root directory */
  57. if (!defined('PHPEXCEL_ROOT')) {
  58. /**
  59. * @ignore
  60. */
  61. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  62. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  63. }
  64. /**
  65. * PHPExcel_Reader_Excel5
  66. *
  67. * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
  68. *
  69. * @category PHPExcel
  70. * @package PHPExcel_Reader_Excel5
  71. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  72. */
  73. class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
  74. {
  75. // ParseXL definitions
  76. const XLS_BIFF8 = 0x0600;
  77. const XLS_BIFF7 = 0x0500;
  78. const XLS_WorkbookGlobals = 0x0005;
  79. const XLS_Worksheet = 0x0010;
  80. // record identifiers
  81. const XLS_Type_FORMULA = 0x0006;
  82. const XLS_Type_EOF = 0x000a;
  83. const XLS_Type_PROTECT = 0x0012;
  84. const XLS_Type_OBJECTPROTECT = 0x0063;
  85. const XLS_Type_SCENPROTECT = 0x00dd;
  86. const XLS_Type_PASSWORD = 0x0013;
  87. const XLS_Type_HEADER = 0x0014;
  88. const XLS_Type_FOOTER = 0x0015;
  89. const XLS_Type_EXTERNSHEET = 0x0017;
  90. const XLS_Type_DEFINEDNAME = 0x0018;
  91. const XLS_Type_VERTICALPAGEBREAKS = 0x001a;
  92. const XLS_Type_HORIZONTALPAGEBREAKS = 0x001b;
  93. const XLS_Type_NOTE = 0x001c;
  94. const XLS_Type_SELECTION = 0x001d;
  95. const XLS_Type_DATEMODE = 0x0022;
  96. const XLS_Type_EXTERNNAME = 0x0023;
  97. const XLS_Type_LEFTMARGIN = 0x0026;
  98. const XLS_Type_RIGHTMARGIN = 0x0027;
  99. const XLS_Type_TOPMARGIN = 0x0028;
  100. const XLS_Type_BOTTOMMARGIN = 0x0029;
  101. const XLS_Type_PRINTGRIDLINES = 0x002b;
  102. const XLS_Type_FILEPASS = 0x002f;
  103. const XLS_Type_FONT = 0x0031;
  104. const XLS_Type_CONTINUE = 0x003c;
  105. const XLS_Type_PANE = 0x0041;
  106. const XLS_Type_CODEPAGE = 0x0042;
  107. const XLS_Type_DEFCOLWIDTH = 0x0055;
  108. const XLS_Type_OBJ = 0x005d;
  109. const XLS_Type_COLINFO = 0x007d;
  110. const XLS_Type_IMDATA = 0x007f;
  111. const XLS_Type_SHEETPR = 0x0081;
  112. const XLS_Type_HCENTER = 0x0083;
  113. const XLS_Type_VCENTER = 0x0084;
  114. const XLS_Type_SHEET = 0x0085;
  115. const XLS_Type_PALETTE = 0x0092;
  116. const XLS_Type_SCL = 0x00a0;
  117. const XLS_Type_PAGESETUP = 0x00a1;
  118. const XLS_Type_MULRK = 0x00bd;
  119. const XLS_Type_MULBLANK = 0x00be;
  120. const XLS_Type_DBCELL = 0x00d7;
  121. const XLS_Type_XF = 0x00e0;
  122. const XLS_Type_MERGEDCELLS = 0x00e5;
  123. const XLS_Type_MSODRAWINGGROUP = 0x00eb;
  124. const XLS_Type_MSODRAWING = 0x00ec;
  125. const XLS_Type_SST = 0x00fc;
  126. const XLS_Type_LABELSST = 0x00fd;
  127. const XLS_Type_EXTSST = 0x00ff;
  128. const XLS_Type_EXTERNALBOOK = 0x01ae;
  129. const XLS_Type_DATAVALIDATIONS = 0x01b2;
  130. const XLS_Type_TXO = 0x01b6;
  131. const XLS_Type_HYPERLINK = 0x01b8;
  132. const XLS_Type_DATAVALIDATION = 0x01be;
  133. const XLS_Type_DIMENSION = 0x0200;
  134. const XLS_Type_BLANK = 0x0201;
  135. const XLS_Type_NUMBER = 0x0203;
  136. const XLS_Type_LABEL = 0x0204;
  137. const XLS_Type_BOOLERR = 0x0205;
  138. const XLS_Type_STRING = 0x0207;
  139. const XLS_Type_ROW = 0x0208;
  140. const XLS_Type_INDEX = 0x020b;
  141. const XLS_Type_ARRAY = 0x0221;
  142. const XLS_Type_DEFAULTROWHEIGHT = 0x0225;
  143. const XLS_Type_WINDOW2 = 0x023e;
  144. const XLS_Type_RK = 0x027e;
  145. const XLS_Type_STYLE = 0x0293;
  146. const XLS_Type_FORMAT = 0x041e;
  147. const XLS_Type_SHAREDFMLA = 0x04bc;
  148. const XLS_Type_BOF = 0x0809;
  149. const XLS_Type_SHEETPROTECTION = 0x0867;
  150. const XLS_Type_RANGEPROTECTION = 0x0868;
  151. const XLS_Type_SHEETLAYOUT = 0x0862;
  152. const XLS_Type_XFEXT = 0x087d;
  153. const XLS_Type_UNKNOWN = 0xffff;
  154. /**
  155. * Read data only?
  156. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  157. * or whether it should read both data and formatting
  158. *
  159. * @var boolean
  160. */
  161. private $_readDataOnly = false;
  162. /**
  163. * Restrict which sheets should be loaded?
  164. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  165. *
  166. * @var array of string
  167. */
  168. private $_loadSheetsOnly = null;
  169. /**
  170. * PHPExcel_Reader_IReadFilter instance
  171. *
  172. * @var PHPExcel_Reader_IReadFilter
  173. */
  174. private $_readFilter = null;
  175. /**
  176. * Summary Information stream data.
  177. *
  178. * @var string
  179. */
  180. private $_summaryInformation;
  181. /**
  182. * Extended Summary Information stream data.
  183. *
  184. * @var string
  185. */
  186. private $_documentSummaryInformation;
  187. /**
  188. * User-Defined Properties stream data.
  189. *
  190. * @var string
  191. */
  192. private $_userDefinedProperties;
  193. /**
  194. * Workbook stream data. (Includes workbook globals substream as well as sheet substreams)
  195. *
  196. * @var string
  197. */
  198. private $_data;
  199. /**
  200. * Size in bytes of $this->_data
  201. *
  202. * @var int
  203. */
  204. private $_dataSize;
  205. /**
  206. * Current position in stream
  207. *
  208. * @var integer
  209. */
  210. private $_pos;
  211. /**
  212. * Workbook to be returned by the reader.
  213. *
  214. * @var PHPExcel
  215. */
  216. private $_phpExcel;
  217. /**
  218. * Worksheet that is currently being built by the reader.
  219. *
  220. * @var PHPExcel_Worksheet
  221. */
  222. private $_phpSheet;
  223. /**
  224. * BIFF version
  225. *
  226. * @var int
  227. */
  228. private $_version;
  229. /**
  230. * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
  231. * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
  232. *
  233. * @var string
  234. */
  235. private $_codepage;
  236. /**
  237. * Shared formats
  238. *
  239. * @var array
  240. */
  241. private $_formats;
  242. /**
  243. * Shared fonts
  244. *
  245. * @var array
  246. */
  247. private $_objFonts;
  248. /**
  249. * Color palette
  250. *
  251. * @var array
  252. */
  253. private $_palette;
  254. /**
  255. * Worksheets
  256. *
  257. * @var array
  258. */
  259. private $_sheets;
  260. /**
  261. * External books
  262. *
  263. * @var array
  264. */
  265. private $_externalBooks;
  266. /**
  267. * REF structures. Only applies to BIFF8.
  268. *
  269. * @var array
  270. */
  271. private $_ref;
  272. /**
  273. * External names
  274. *
  275. * @var array
  276. */
  277. private $_externalNames;
  278. /**
  279. * Defined names
  280. *
  281. * @var array
  282. */
  283. private $_definedname;
  284. /**
  285. * Shared strings. Only applies to BIFF8.
  286. *
  287. * @var array
  288. */
  289. private $_sst;
  290. /**
  291. * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
  292. *
  293. * @var boolean
  294. */
  295. private $_frozen;
  296. /**
  297. * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
  298. *
  299. * @var boolean
  300. */
  301. private $_isFitToPages;
  302. /**
  303. * Objects. One OBJ record contributes with one entry.
  304. *
  305. * @var array
  306. */
  307. private $_objs;
  308. /**
  309. * Text Objects. One TXO record corresponds with one entry.
  310. *
  311. * @var array
  312. */
  313. private $_textObjects;
  314. /**
  315. * Cell Annotations (BIFF8)
  316. *
  317. * @var array
  318. */
  319. private $_cellNotes;
  320. /**
  321. * The combined MSODRAWINGGROUP data
  322. *
  323. * @var string
  324. */
  325. private $_drawingGroupData;
  326. /**
  327. * The combined MSODRAWING data (per sheet)
  328. *
  329. * @var string
  330. */
  331. private $_drawingData;
  332. /**
  333. * Keep track of XF index
  334. *
  335. * @var int
  336. */
  337. private $_xfIndex;
  338. /**
  339. * Mapping of XF index (that is a cell XF) to final index in cellXf collection
  340. *
  341. * @var array
  342. */
  343. private $_mapCellXfIndex;
  344. /**
  345. * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
  346. *
  347. * @var array
  348. */
  349. private $_mapCellStyleXfIndex;
  350. /**
  351. * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
  352. *
  353. * @var array
  354. */
  355. private $_sharedFormulas;
  356. /**
  357. * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
  358. * refers to a shared formula.
  359. *
  360. * @var array
  361. */
  362. private $_sharedFormulaParts;
  363. /**
  364. * Read data only?
  365. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  366. * If false (the default) it will read data and formatting.
  367. *
  368. * @return boolean
  369. */
  370. public function getReadDataOnly()
  371. {
  372. return $this->_readDataOnly;
  373. }
  374. /**
  375. * Set read data only
  376. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  377. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  378. *
  379. * @param boolean $pValue
  380. *
  381. * @return PHPExcel_Reader_Excel5
  382. */
  383. public function setReadDataOnly($pValue = false)
  384. {
  385. $this->_readDataOnly = $pValue;
  386. return $this;
  387. }
  388. /**
  389. * Get which sheets to load
  390. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  391. * indicating that all worksheets in the workbook should be loaded.
  392. *
  393. * @return mixed
  394. */
  395. public function getLoadSheetsOnly()
  396. {
  397. return $this->_loadSheetsOnly;
  398. }
  399. /**
  400. * Set which sheets to load
  401. *
  402. * @param mixed $value
  403. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  404. * If NULL, then it tells the Reader to read all worksheets in the workbook
  405. *
  406. * @return PHPExcel_Reader_Excel5
  407. */
  408. public function setLoadSheetsOnly($value = null)
  409. {
  410. $this->_loadSheetsOnly = is_array($value) ?
  411. $value : array($value);
  412. return $this;
  413. }
  414. /**
  415. * Set all sheets to load
  416. * Tells the Reader to load all worksheets from the workbook.
  417. *
  418. * @return PHPExcel_Reader_Excel5
  419. */
  420. public function setLoadAllSheets()
  421. {
  422. $this->_loadSheetsOnly = null;
  423. return $this;
  424. }
  425. /**
  426. * Read filter
  427. *
  428. * @return PHPExcel_Reader_IReadFilter
  429. */
  430. public function getReadFilter() {
  431. return $this->_readFilter;
  432. }
  433. /**
  434. * Set read filter
  435. *
  436. * @param PHPExcel_Reader_IReadFilter $pValue
  437. * @return PHPExcel_Reader_Excel5
  438. */
  439. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  440. $this->_readFilter = $pValue;
  441. return $this;
  442. }
  443. /**
  444. * Create a new PHPExcel_Reader_Excel5 instance
  445. */
  446. public function __construct() {
  447. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  448. }
  449. /**
  450. * Can the current PHPExcel_Reader_IReader read the file?
  451. *
  452. * @param string $pFileName
  453. * @return boolean
  454. */
  455. public function canRead($pFilename)
  456. {
  457. // Check if file exists
  458. if (!file_exists($pFilename)) {
  459. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  460. }
  461. try {
  462. // Use ParseXL for the hard work.
  463. $ole = new PHPExcel_Shared_OLERead();
  464. // get excel data
  465. $res = $ole->read($pFilename);
  466. return true;
  467. } catch (Exception $e) {
  468. return false;
  469. }
  470. }
  471. /**
  472. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  473. *
  474. * @param string $pFilename
  475. * @throws Exception
  476. */
  477. public function listWorksheetNames($pFilename)
  478. {
  479. // Check if file exists
  480. if (!file_exists($pFilename)) {
  481. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  482. }
  483. $worksheetNames = array();
  484. // Read the OLE file
  485. $this->_loadOLE($pFilename);
  486. // total byte size of Excel data (workbook global substream + sheet substreams)
  487. $this->_dataSize = strlen($this->_data);
  488. $this->_pos = 0;
  489. $this->_sheets = array();
  490. // Parse Workbook Global Substream
  491. while ($this->_pos < $this->_dataSize) {
  492. $code = self::_GetInt2d($this->_data, $this->_pos);
  493. switch ($code) {
  494. case self::XLS_Type_BOF: $this->_readBof(); break;
  495. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  496. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  497. default: $this->_readDefault(); break;
  498. }
  499. }
  500. foreach ($this->_sheets as $sheet) {
  501. if ($sheet['sheetType'] != 0x00) {
  502. // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
  503. continue;
  504. }
  505. $worksheetNames[] = $sheet['name'];
  506. }
  507. return $worksheetNames;
  508. }
  509. /**
  510. * Loads PHPExcel from file
  511. *
  512. * @param string $pFilename
  513. * @return PHPExcel
  514. * @throws Exception
  515. */
  516. public function load($pFilename)
  517. {
  518. // Read the OLE file
  519. $this->_loadOLE($pFilename);
  520. // Initialisations
  521. $this->_phpExcel = new PHPExcel;
  522. $this->_phpExcel->removeSheetByIndex(0); // remove 1st sheet
  523. if (!$this->_readDataOnly) {
  524. $this->_phpExcel->removeCellStyleXfByIndex(0); // remove the default style
  525. $this->_phpExcel->removeCellXfByIndex(0); // remove the default style
  526. }
  527. // Read the summary information stream (containing meta data)
  528. $this->_readSummaryInformation();
  529. // Read the Additional document summary information stream (containing application-specific meta data)
  530. $this->_readDocumentSummaryInformation();
  531. // total byte size of Excel data (workbook global substream + sheet substreams)
  532. $this->_dataSize = strlen($this->_data);
  533. // initialize
  534. $this->_pos = 0;
  535. $this->_codepage = 'CP1252';
  536. $this->_formats = array();
  537. $this->_objFonts = array();
  538. $this->_palette = array();
  539. $this->_sheets = array();
  540. $this->_externalBooks = array();
  541. $this->_ref = array();
  542. $this->_definedname = array();
  543. $this->_sst = array();
  544. $this->_drawingGroupData = '';
  545. $this->_xfIndex = '';
  546. $this->_mapCellXfIndex = array();
  547. $this->_mapCellStyleXfIndex = array();
  548. // Parse Workbook Global Substream
  549. while ($this->_pos < $this->_dataSize) {
  550. $code = self::_GetInt2d($this->_data, $this->_pos);
  551. switch ($code) {
  552. case self::XLS_Type_BOF: $this->_readBof(); break;
  553. case self::XLS_Type_FILEPASS: $this->_readFilepass(); break;
  554. case self::XLS_Type_CODEPAGE: $this->_readCodepage(); break;
  555. case self::XLS_Type_DATEMODE: $this->_readDateMode(); break;
  556. case self::XLS_Type_FONT: $this->_readFont(); break;
  557. case self::XLS_Type_FORMAT: $this->_readFormat(); break;
  558. case self::XLS_Type_XF: $this->_readXf(); break;
  559. case self::XLS_Type_XFEXT: $this->_readXfExt(); break;
  560. case self::XLS_Type_STYLE: $this->_readStyle(); break;
  561. case self::XLS_Type_PALETTE: $this->_readPalette(); break;
  562. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  563. case self::XLS_Type_EXTERNALBOOK: $this->_readExternalBook(); break;
  564. case self::XLS_Type_EXTERNNAME: $this->_readExternName(); break;
  565. case self::XLS_Type_EXTERNSHEET: $this->_readExternSheet(); break;
  566. case self::XLS_Type_DEFINEDNAME: $this->_readDefinedName(); break;
  567. case self::XLS_Type_MSODRAWINGGROUP: $this->_readMsoDrawingGroup(); break;
  568. case self::XLS_Type_SST: $this->_readSst(); break;
  569. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  570. default: $this->_readDefault(); break;
  571. }
  572. }
  573. // Resolve indexed colors for font, fill, and border colors
  574. // Cannot be resolved already in XF record, because PALETTE record comes afterwards
  575. if (!$this->_readDataOnly) {
  576. foreach ($this->_objFonts as $objFont) {
  577. if (isset($objFont->colorIndex)) {
  578. $color = self::_readColor($objFont->colorIndex,$this->_palette,$this->_version);
  579. $objFont->getColor()->setRGB($color['rgb']);
  580. }
  581. }
  582. foreach ($this->_phpExcel->getCellXfCollection() as $objStyle) {
  583. // fill start and end color
  584. $fill = $objStyle->getFill();
  585. if (isset($fill->startcolorIndex)) {
  586. $startColor = self::_readColor($fill->startcolorIndex,$this->_palette,$this->_version);
  587. $fill->getStartColor()->setRGB($startColor['rgb']);
  588. }
  589. if (isset($fill->endcolorIndex)) {
  590. $endColor = self::_readColor($fill->endcolorIndex,$this->_palette,$this->_version);
  591. $fill->getEndColor()->setRGB($endColor['rgb']);
  592. }
  593. // border colors
  594. $top = $objStyle->getBorders()->getTop();
  595. $right = $objStyle->getBorders()->getRight();
  596. $bottom = $objStyle->getBorders()->getBottom();
  597. $left = $objStyle->getBorders()->getLeft();
  598. $diagonal = $objStyle->getBorders()->getDiagonal();
  599. if (isset($top->colorIndex)) {
  600. $borderTopColor = self::_readColor($top->colorIndex,$this->_palette,$this->_version);
  601. $top->getColor()->setRGB($borderTopColor['rgb']);
  602. }
  603. if (isset($right->colorIndex)) {
  604. $borderRightColor = self::_readColor($right->colorIndex,$this->_palette,$this->_version);
  605. $right->getColor()->setRGB($borderRightColor['rgb']);
  606. }
  607. if (isset($bottom->colorIndex)) {
  608. $borderBottomColor = self::_readColor($bottom->colorIndex,$this->_palette,$this->_version);
  609. $bottom->getColor()->setRGB($borderBottomColor['rgb']);
  610. }
  611. if (isset($left->colorIndex)) {
  612. $borderLeftColor = self::_readColor($left->colorIndex,$this->_palette,$this->_version);
  613. $left->getColor()->setRGB($borderLeftColor['rgb']);
  614. }
  615. if (isset($diagonal->colorIndex)) {
  616. $borderDiagonalColor = self::_readColor($diagonal->colorIndex,$this->_palette,$this->_version);
  617. $diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
  618. }
  619. }
  620. }
  621. // treat MSODRAWINGGROUP records, workbook-level Escher
  622. if (!$this->_readDataOnly && $this->_drawingGroupData) {
  623. $escherWorkbook = new PHPExcel_Shared_Escher();
  624. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
  625. $escherWorkbook = $reader->load($this->_drawingGroupData);
  626. // debug Escher stream
  627. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  628. //$debug->load($this->_drawingGroupData);
  629. }
  630. // Parse the individual sheets
  631. foreach ($this->_sheets as $sheet) {
  632. if ($sheet['sheetType'] != 0x00) {
  633. // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
  634. continue;
  635. }
  636. // check if sheet should be skipped
  637. if (isset($this->_loadSheetsOnly) && !in_array($sheet['name'], $this->_loadSheetsOnly)) {
  638. continue;
  639. }
  640. // add sheet to PHPExcel object
  641. $this->_phpSheet = $this->_phpExcel->createSheet();
  642. $this->_phpSheet->setTitle($sheet['name']);
  643. $this->_phpSheet->setSheetState($sheet['sheetState']);
  644. $this->_pos = $sheet['offset'];
  645. // Initialize isFitToPages. May change after reading SHEETPR record.
  646. $this->_isFitToPages = false;
  647. // Initialize drawingData
  648. $this->_drawingData = '';
  649. // Initialize objs
  650. $this->_objs = array();
  651. // Initialize shared formula parts
  652. $this->_sharedFormulaParts = array();
  653. // Initialize shared formulas
  654. $this->_sharedFormulas = array();
  655. // Initialize text objs
  656. $this->_textObjects = array();
  657. // Initialize cell annotations
  658. $this->_cellNotes = array();
  659. $this->textObjRef = -1;
  660. while ($this->_pos <= $this->_dataSize - 4) {
  661. $code = self::_GetInt2d($this->_data, $this->_pos);
  662. switch ($code) {
  663. case self::XLS_Type_BOF: $this->_readBof(); break;
  664. case self::XLS_Type_PRINTGRIDLINES: $this->_readPrintGridlines(); break;
  665. case self::XLS_Type_DEFAULTROWHEIGHT: $this->_readDefaultRowHeight(); break;
  666. case self::XLS_Type_SHEETPR: $this->_readSheetPr(); break;
  667. case self::XLS_Type_HORIZONTALPAGEBREAKS: $this->_readHorizontalPageBreaks(); break;
  668. case self::XLS_Type_VERTICALPAGEBREAKS: $this->_readVerticalPageBreaks(); break;
  669. case self::XLS_Type_HEADER: $this->_readHeader(); break;
  670. case self::XLS_Type_FOOTER: $this->_readFooter(); break;
  671. case self::XLS_Type_HCENTER: $this->_readHcenter(); break;
  672. case self::XLS_Type_VCENTER: $this->_readVcenter(); break;
  673. case self::XLS_Type_LEFTMARGIN: $this->_readLeftMargin(); break;
  674. case self::XLS_Type_RIGHTMARGIN: $this->_readRightMargin(); break;
  675. case self::XLS_Type_TOPMARGIN: $this->_readTopMargin(); break;
  676. case self::XLS_Type_BOTTOMMARGIN: $this->_readBottomMargin(); break;
  677. case self::XLS_Type_PAGESETUP: $this->_readPageSetup(); break;
  678. case self::XLS_Type_PROTECT: $this->_readProtect(); break;
  679. case self::XLS_Type_SCENPROTECT: $this->_readScenProtect(); break;
  680. case self::XLS_Type_OBJECTPROTECT: $this->_readObjectProtect(); break;
  681. case self::XLS_Type_PASSWORD: $this->_readPassword(); break;
  682. case self::XLS_Type_DEFCOLWIDTH: $this->_readDefColWidth(); break;
  683. case self::XLS_Type_COLINFO: $this->_readColInfo(); break;
  684. case self::XLS_Type_DIMENSION: $this->_readDefault(); break;
  685. case self::XLS_Type_ROW: $this->_readRow(); break;
  686. case self::XLS_Type_DBCELL: $this->_readDefault(); break;
  687. case self::XLS_Type_RK: $this->_readRk(); break;
  688. case self::XLS_Type_LABELSST: $this->_readLabelSst(); break;
  689. case self::XLS_Type_MULRK: $this->_readMulRk(); break;
  690. case self::XLS_Type_NUMBER: $this->_readNumber(); break;
  691. case self::XLS_Type_FORMULA: $this->_readFormula(); break;
  692. case self::XLS_Type_SHAREDFMLA: $this->_readSharedFmla(); break;
  693. case self::XLS_Type_BOOLERR: $this->_readBoolErr(); break;
  694. case self::XLS_Type_MULBLANK: $this->_readMulBlank(); break;
  695. case self::XLS_Type_LABEL: $this->_readLabel(); break;
  696. case self::XLS_Type_BLANK: $this->_readBlank(); break;
  697. case self::XLS_Type_MSODRAWING: $this->_readMsoDrawing(); break;
  698. case self::XLS_Type_OBJ: $this->_readObj(); break;
  699. case self::XLS_Type_WINDOW2: $this->_readWindow2(); break;
  700. case self::XLS_Type_SCL: $this->_readScl(); break;
  701. case self::XLS_Type_PANE: $this->_readPane(); break;
  702. case self::XLS_Type_SELECTION: $this->_readSelection(); break;
  703. case self::XLS_Type_MERGEDCELLS: $this->_readMergedCells(); break;
  704. case self::XLS_Type_HYPERLINK: $this->_readHyperLink(); break;
  705. case self::XLS_Type_DATAVALIDATIONS: $this->_readDataValidations(); break;
  706. case self::XLS_Type_DATAVALIDATION: $this->_readDataValidation(); break;
  707. case self::XLS_Type_SHEETLAYOUT: $this->_readSheetLayout(); break;
  708. case self::XLS_Type_SHEETPROTECTION: $this->_readSheetProtection(); break;
  709. case self::XLS_Type_RANGEPROTECTION: $this->_readRangeProtection(); break;
  710. case self::XLS_Type_NOTE: $this->_readNote(); break;
  711. //case self::XLS_Type_IMDATA: $this->_readImData(); break;
  712. case self::XLS_Type_TXO: $this->_readTextObject(); break;
  713. case self::XLS_Type_CONTINUE: $this->_readContinue(); break;
  714. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  715. default: $this->_readDefault(); break;
  716. }
  717. }
  718. // treat MSODRAWING records, sheet-level Escher
  719. if (!$this->_readDataOnly && $this->_drawingData) {
  720. $escherWorksheet = new PHPExcel_Shared_Escher();
  721. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
  722. $escherWorksheet = $reader->load($this->_drawingData);
  723. // debug Escher stream
  724. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  725. //$debug->load($this->_drawingData);
  726. // get all spContainers in one long array, so they can be mapped to OBJ records
  727. $allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
  728. }
  729. // treat OBJ records
  730. foreach ($this->_objs as $n => $obj) {
  731. // echo '<hr /><b>Object</b> reference is ',$n,'<br />';
  732. // var_dump($obj);
  733. // echo '<br />';
  734. // the first shape container never has a corresponding OBJ record, hence $n + 1
  735. $spContainer = $allSpContainers[$n + 1];
  736. // we skip all spContainers that are a part of a group shape since we cannot yet handle those
  737. if ($spContainer->getNestingLevel() > 1) {
  738. continue;
  739. }
  740. // calculate the width and height of the shape
  741. list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($spContainer->getStartCoordinates());
  742. list($endColumn, $endRow) = PHPExcel_Cell::coordinateFromString($spContainer->getEndCoordinates());
  743. $startOffsetX = $spContainer->getStartOffsetX();
  744. $startOffsetY = $spContainer->getStartOffsetY();
  745. $endOffsetX = $spContainer->getEndOffsetX();
  746. $endOffsetY = $spContainer->getEndOffsetY();
  747. $width = PHPExcel_Shared_Excel5::getDistanceX($this->_phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
  748. $height = PHPExcel_Shared_Excel5::getDistanceY($this->_phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);
  749. // calculate offsetX and offsetY of the shape
  750. $offsetX = $startOffsetX * PHPExcel_Shared_Excel5::sizeCol($this->_phpSheet, $startColumn) / 1024;
  751. $offsetY = $startOffsetY * PHPExcel_Shared_Excel5::sizeRow($this->_phpSheet, $startRow) / 256;
  752. switch ($obj['otObjType']) {
  753. case 0x19:
  754. // Note
  755. // echo 'Cell Annotation Object<br />';
  756. // echo 'Object ID is ',$obj['idObjID'],'<br />';
  757. //
  758. if (isset($this->_cellNotes[$obj['idObjID']])) {
  759. $cellNote = $this->_cellNotes[$obj['idObjID']];
  760. // echo '_cellNotes[',$obj['idObjID'],']: ';
  761. // var_dump($cellNote);
  762. // echo '<br />';
  763. //
  764. if (isset($this->_textObjects[$obj['idObjID']])) {
  765. $textObject = $this->_textObjects[$obj['idObjID']];
  766. // echo '_textObject: ';
  767. // var_dump($textObject);
  768. // echo '<br />';
  769. //
  770. $this->_cellNotes[$obj['idObjID']]['objTextData'] = $textObject;
  771. $text = $textObject['text'];
  772. }
  773. // echo $text,'<br />';
  774. }
  775. break;
  776. case 0x08:
  777. // echo 'Picture Object<br />';
  778. // picture
  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. // need check because some blip types are not supported by Escher reader such as EMF
  785. if ($blip = $BSE->getBlip()) {
  786. $ih = imagecreatefromstring($blip->getData());
  787. $drawing = new PHPExcel_Worksheet_MemoryDrawing();
  788. $drawing->setImageResource($ih);
  789. // width, height, offsetX, offsetY
  790. $drawing->setResizeProportional(false);
  791. $drawing->setWidth($width);
  792. $drawing->setHeight($height);
  793. $drawing->setOffsetX($offsetX);
  794. $drawing->setOffsetY($offsetY);
  795. switch ($blipType) {
  796. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
  797. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
  798. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG);
  799. break;
  800. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
  801. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
  802. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG);
  803. break;
  804. }
  805. $drawing->setWorksheet($this->_phpSheet);
  806. $drawing->setCoordinates($spContainer->getStartCoordinates());
  807. }
  808. break;
  809. default:
  810. // other object type
  811. break;
  812. }
  813. }
  814. // treat SHAREDFMLA records
  815. if ($this->_version == self::XLS_BIFF8) {
  816. foreach ($this->_sharedFormulaParts as $cell => $baseCell) {
  817. list($column, $row) = PHPExcel_Cell::coordinateFromString($cell);
  818. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle()) ) {
  819. $formula = $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell], $cell);
  820. $this->_phpSheet->getCell($cell)->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  821. }
  822. }
  823. }
  824. if (count($this->_cellNotes) > 0) {
  825. foreach($this->_cellNotes as $note => $noteDetails) {
  826. // echo '<b>Cell annotation ',$note,'</b><br />';
  827. // var_dump($noteDetails);
  828. // echo '<br />';
  829. $cellAddress = str_replace('$','',$noteDetails['cellRef']);
  830. $this->_phpSheet->getComment( $cellAddress )
  831. ->setAuthor( $noteDetails['author'] )
  832. ->setText($this->_parseRichText($noteDetails['objTextData']['text']) );
  833. }
  834. }
  835. }
  836. // add the named ranges (defined names)
  837. foreach ($this->_definedname as $definedName) {
  838. if ($definedName['isBuiltInName']) {
  839. switch ($definedName['name']) {
  840. case pack('C', 0x06):
  841. // print area
  842. // in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
  843. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  844. $extractedRanges = array();
  845. foreach ($ranges as $range) {
  846. // $range should look like one of these
  847. // Foo!$C$7:$J$66
  848. // Bar!$A$1:$IV$2
  849. $explodes = explode('!', $range); // FIXME: what if sheetname contains exclamation mark?
  850. $sheetName = $explodes[0];
  851. if (count($explodes) == 2) {
  852. $extractedRanges[] = str_replace('$', '', $explodes[1]); // C7:J66
  853. }
  854. }
  855. if ($docSheet = $this->_phpExcel->getSheetByName($sheetName)) {
  856. $docSheet->getPageSetup()->setPrintArea(implode(',', $extractedRanges)); // C7:J66,A1:IV2
  857. }
  858. break;
  859. case pack('C', 0x07):
  860. // print titles (repeating rows)
  861. // Assuming BIFF8, there are 3 cases
  862. // 1. repeating rows
  863. // formula looks like this: Sheet!$A$1:$IV$2
  864. // rows 1-2 repeat
  865. // 2. repeating columns
  866. // formula looks like this: Sheet!$A$1:$B$65536
  867. // columns A-B repeat
  868. // 3. both repeating rows and repeating columns
  869. // formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
  870. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  871. foreach ($ranges as $range) {
  872. // $range should look like this one of these
  873. // Sheet!$A$1:$B$65536
  874. // Sheet!$A$1:$IV$2
  875. $explodes = explode('!', $range);
  876. if (count($explodes) == 2) {
  877. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
  878. $extractedRange = $explodes[1];
  879. $extractedRange = str_replace('$', '', $extractedRange);
  880. $coordinateStrings = explode(':', $extractedRange);
  881. if (count($coordinateStrings) == 2) {
  882. list($firstColumn, $firstRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[0]);
  883. list($lastColumn, $lastRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[1]);
  884. if ($firstColumn == 'A' and $lastColumn == 'IV') {
  885. // then we have repeating rows
  886. $docSheet->getPageSetup()->setRowsToRepeatAtTop(array($firstRow, $lastRow));
  887. } elseif ($firstRow == 1 and $lastRow == 65536) {
  888. // then we have repeating columns
  889. $docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($firstColumn, $lastColumn));
  890. }
  891. }
  892. }
  893. }
  894. }
  895. break;
  896. }
  897. } else {
  898. // Extract range
  899. $explodes = explode('!', $definedName['formula']);
  900. if (count($explodes) == 2) {
  901. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
  902. $extractedRange = $explodes[1];
  903. $extractedRange = str_replace('$', '', $extractedRange);
  904. $localOnly = ($definedName['scope'] == 0) ? false : true;
  905. $scope = ($definedName['scope'] == 0) ?
  906. null : $this->_phpExcel->getSheetByName($this->_sheets[$definedName['scope'] - 1]['name']);
  907. $this->_phpExcel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $docSheet, $extractedRange, $localOnly, $scope) );
  908. }
  909. }
  910. }
  911. }
  912. return $this->_phpExcel;
  913. }
  914. /**
  915. * Use OLE reader to extract the relevant data streams from the OLE file
  916. *
  917. * @param string $pFilename
  918. */
  919. private function _loadOLE($pFilename)
  920. {
  921. // OLE reader
  922. $ole = new PHPExcel_Shared_OLERead();
  923. // get excel data,
  924. $res = $ole->read($pFilename);
  925. // Get workbook data: workbook stream + sheet streams
  926. $this->_data = $ole->getStream($ole->wrkbook);
  927. // Get summary information data
  928. $this->_summaryInformation = $ole->getStream($ole->summaryInformation);
  929. // Get additional document summary information data
  930. $this->_documentSummaryInformation = $ole->getStream($ole->documentSummaryInformation);
  931. // Get user-defined property data
  932. // $this->_userDefinedProperties = $ole->getUserDefinedProperties();
  933. }
  934. /**
  935. * Read summary information
  936. */
  937. private function _readSummaryInformation()
  938. {
  939. if (!isset($this->_summaryInformation)) {
  940. return;
  941. }
  942. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  943. // offset: 2; size: 2;
  944. // offset: 4; size: 2; OS version
  945. // offset: 6; size: 2; OS indicator
  946. // offset: 8; size: 16
  947. // offset: 24; size: 4; section count
  948. $secCount = self::_GetInt4d($this->_summaryInformation, 24);
  949. // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
  950. // offset: 44; size: 4
  951. $secOffset = self::_GetInt4d($this->_summaryInformation, 44);
  952. // section header
  953. // offset: $secOffset; size: 4; section length
  954. $secLength = self::_GetInt4d($this->_summaryInformation, $secOffset);
  955. // offset: $secOffset+4; size: 4; property count
  956. $countProperties = self::_GetInt4d($this->_summaryInformation, $secOffset+4);
  957. // initialize code page (used to resolve string values)
  958. $codePage = 'CP1252';
  959. // offset: ($secOffset+8); size: var
  960. // loop through property decarations and properties
  961. for ($i = 0; $i < $countProperties; ++$i) {
  962. // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
  963. $id = self::_GetInt4d($this->_summaryInformation, ($secOffset+8) + (8 * $i));
  964. // Use value of property id as appropriate
  965. // offset: ($secOffset+12) + (8 * $i); size: 4; offset from beginning of section (48)
  966. $offset = self::_GetInt4d($this->_summaryInformation, ($secOffset+12) + (8 * $i));
  967. $type = self::_GetInt4d($this->_summaryInformation, $secOffset + $offset);
  968. // initialize property value
  969. $value = null;
  970. // extract property value based on property type
  971. switch ($type) {
  972. case 0x02: // 2 byte signed integer
  973. $value = self::_GetInt2d($this->_summaryInformation, $secOffset + 4 + $offset);
  974. break;
  975. case 0x03: // 4 byte signed integer
  976. $value = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
  977. break;
  978. case 0x13: // 4 byte unsigned integer
  979. // not needed yet, fix later if necessary
  980. break;
  981. case 0x1E: // null-terminated string prepended by dword string length
  982. $byteLength = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
  983. $value = substr($this->_summaryInformation, $secOffset + 8 + $offset, $byteLength);
  984. $value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
  985. $value = rtrim($value);
  986. break;
  987. case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  988. // PHP-time
  989. $value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_summaryInformation, $secOffset + 4 + $offset, 8));
  990. break;
  991. case 0x47: // Clipboard format
  992. // not needed yet, fix later if necessary
  993. break;
  994. }
  995. switch ($id) {
  996. case 0x01: // Code Page
  997. $codePage = PHPExcel_Shared_CodePage::NumberToName($value);
  998. break;
  999. case 0x02: // Title
  1000. $this->_phpExcel->getProperties()->setTitle($value);
  1001. break;
  1002. case 0x03: // Subject
  1003. $this->_phpExcel->getProperties()->setSubject($value);
  1004. break;
  1005. case 0x04: // Author (Creator)
  1006. $this->_phpExcel->getProperties()->setCreator($value);
  1007. break;
  1008. case 0x05: // Keywords
  1009. $this->_phpExcel->getProperties()->setKeywords($value);
  1010. break;
  1011. case 0x06: // Comments (Description)
  1012. $this->_phpExcel->getProperties()->setDescription($value);
  1013. break;
  1014. case 0x07: // Template
  1015. // Not supported by PHPExcel
  1016. break;
  1017. case 0x08: // Last Saved By (LastModifiedBy)
  1018. $this->_phpExcel->getProperties()->setLastModifiedBy($value);
  1019. break;
  1020. case 0x09: // Revision
  1021. // Not supported by PHPExcel
  1022. break;
  1023. case 0x0A: // Total Editing Time
  1024. // Not supported by PHPExcel
  1025. break;
  1026. case 0x0B: // Last Printed
  1027. // Not supported by PHPExcel
  1028. break;
  1029. case 0x0C: // Created Date/Time
  1030. $this->_phpExcel->getProperties()->setCreated($value);
  1031. break;
  1032. case 0x0D: // Modified Date/Time
  1033. $this->_phpExcel->getProperties()->setModified($value);
  1034. break;
  1035. case 0x0E: // Number of Pages
  1036. // Not supported by PHPExcel
  1037. break;
  1038. case 0x0F: // Number of Words
  1039. // Not supported by PHPExcel
  1040. break;
  1041. case 0x10: // Number of Characters
  1042. // Not supported by PHPExcel
  1043. break;
  1044. case 0x11: // Thumbnail
  1045. // Not supported by PHPExcel
  1046. break;
  1047. case 0x12: // Name of creating application
  1048. // Not supported by PHPExcel
  1049. break;
  1050. case 0x13: // Security
  1051. // Not supported by PHPExcel
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. /**
  1057. * Read additional document summary information
  1058. */
  1059. private function _readDocumentSummaryInformation()
  1060. {
  1061. if (!isset($this->_documentSummaryInformation)) {
  1062. return;
  1063. }
  1064. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  1065. // offset: 2; size: 2;
  1066. // offset: 4; size: 2; OS version
  1067. // offset: 6; size: 2; OS indicator
  1068. // offset: 8; size: 16
  1069. // offset: 24; size: 4; section count
  1070. $secCount = self::_GetInt4d($this->_documentSummaryInformation, 24);
  1071. // echo '$secCount = ',$secCount,'<br />';
  1072. // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
  1073. // offset: 44; size: 4; first section offset
  1074. $secOffset = self::_GetInt4d($this->_documentSummaryInformation, 44);
  1075. // echo '$secOffset = ',$secOffset,'<br />';
  1076. // section header
  1077. // offset: $secOffset; size: 4; section length
  1078. $secLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset);
  1079. // echo '$secLength = ',$secLength,'<br />';
  1080. // offset: $secOffset+4; size: 4; property count
  1081. $countProperties = self::_GetInt4d($this->_documentSummaryInformation, $secOffset+4);
  1082. // echo '$countProperties = ',$countProperties,'<br />';
  1083. // initialize code page (used to resolve string values)
  1084. $codePage = 'CP1252';
  1085. // offset: ($secOffset+8); size: var
  1086. // loop through property decarations and properties
  1087. for ($i = 0; $i < $countProperties; ++$i) {
  1088. // echo 'Property ',$i,'<br />';
  1089. // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
  1090. $id = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+8) + (8 * $i));
  1091. // echo 'ID is ',$id,'<br />';
  1092. // Use value of property id as appropriate
  1093. // offset: 60 + 8 * $i; size: 4; offset from beginning of section (48)
  1094. $offset = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+12) + (8 * $i));
  1095. $type = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + $offset);
  1096. // echo 'Type is ',$type,', ';
  1097. // initialize property value
  1098. $value = null;
  1099. // extract property value based on property type
  1100. switch ($type) {
  1101. case 0x02: // 2 byte signed integer
  1102. $value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1103. break;
  1104. case 0x03: // 4 byte signed integer
  1105. $value = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1106. break;
  1107. case 0x13: // 4 byte unsigned integer
  1108. // not needed yet, fix later if necessary
  1109. break;
  1110. case 0x1E: // null-terminated string prepended by dword string length
  1111. $byteLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1112. $value = substr($this->_documentSummaryInformation, $secOffset + 8 + $offset, $byteLength);
  1113. $value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
  1114. $value = rtrim($value);
  1115. break;
  1116. case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  1117. // PHP-Time
  1118. $value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_documentSummaryInformation, $secOffset + 4 + $offset, 8));
  1119. break;
  1120. case 0x47: // Clipboard format
  1121. // not needed yet, fix later if necessary
  1122. break;
  1123. }
  1124. switch ($id) {
  1125. case 0x01: // Code Page
  1126. $codePage = PHPExcel_Shared_CodePage::NumberToName($value);
  1127. break;
  1128. case 0x02: // Category
  1129. $this->_phpExcel->getProperties()->setCategory($value);
  1130. break;
  1131. case 0x03: // Presentation Target
  1132. // Not supported by PHPExcel
  1133. break;
  1134. case 0x04: // Bytes
  1135. // Not supported by PHPExcel
  1136. break;
  1137. case 0x05: // Lines
  1138. // Not supported by PHPExcel
  1139. break;
  1140. case 0x06: // Paragraphs
  1141. // Not supported by PHPExcel
  1142. break;
  1143. case 0x07: // Slides
  1144. // Not supported by PHPExcel
  1145. break;
  1146. case 0x08: // Notes
  1147. // Not supported by PHPExcel
  1148. break;
  1149. case 0x09: // Hidden Slides
  1150. // Not supported by PHPExcel
  1151. break;
  1152. case 0x0A: // MM Clips
  1153. // Not supported by PHPExcel
  1154. break;
  1155. case 0x0B: // Scale Crop
  1156. // Not supported by PHPExcel
  1157. break;
  1158. case 0x0C: // Heading Pairs
  1159. // Not supported by PHPExcel
  1160. break;
  1161. case 0x0D: // Titles of Parts
  1162. // Not supported by PHPExcel
  1163. break;
  1164. case 0x0E: // Manager
  1165. $this->_phpExcel->getProperties()->setManager($value);
  1166. break;
  1167. case 0x0F: // Company
  1168. $this->_phpExcel->getProperties()->setCompany($value);
  1169. break;
  1170. case 0x10: // Links up-to-date
  1171. // Not supported by PHPExcel
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. /**
  1177. * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
  1178. */
  1179. private function _readDefault()
  1180. {
  1181. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1182. // $recordData = substr($this->_data, $this->_pos + 4, $length);
  1183. // move stream pointer to next record
  1184. $this->_pos += 4 + $length;
  1185. }
  1186. /**
  1187. * The NOTE record specifies a comment associated with a particular cell. In Excel 95 (BIFF7) and earlier versions,
  1188. * this record stores a note (cell note). This feature was significantly enhanced in Excel 97.
  1189. */
  1190. private function _readNote()
  1191. {
  1192. // echo '<b>Read Cell Annotation</b><br />';
  1193. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1194. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1195. // move stream pointer to next record
  1196. $this->_pos += 4 + $length;
  1197. if ($this->_readDataOnly) {
  1198. return;
  1199. }
  1200. $cellAddress = $this->_readBIFF8CellAddress(substr($recordData, 0, 4));
  1201. if ($this->_version == self::XLS_BIFF8) {
  1202. $noteObjID = self::_GetInt2d($recordData, 6);
  1203. $noteAuthor = self::_readUnicodeStringLong(substr($recordData, 8));
  1204. $noteAuthor = $noteAuthor['value'];
  1205. // echo 'Note Address=',$cellAddress,'<br />';
  1206. // echo 'Note Object ID=',$noteObjID,'<br />';
  1207. // echo 'Note Author=',$noteAuthor,'<hr />';
  1208. //
  1209. $this->_cellNotes[$noteObjID] = array('cellRef' => $cellAddress,
  1210. 'objectID' => $noteObjID,
  1211. 'author' => $noteAuthor
  1212. );
  1213. } else {
  1214. $extension = false;
  1215. if ($cellAddress == '$B$65536') {
  1216. // If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation
  1217. // note from the previous cell annotation. We're not yet handling this, so annotations longer than the
  1218. // max 2048 bytes will probably throw a wobbly.
  1219. $row = self::_GetInt2d($recordData, 0);
  1220. $extension = true;
  1221. $cellAddress = array_pop(array_keys($this->_phpSheet->getComments()));
  1222. }
  1223. // echo 'Note Address=',$cellAddress,'<br />';
  1224. $cellAddress = str_replace('$','',$cellAddress);
  1225. $noteLength = self::_GetInt2d($recordData, 4);
  1226. $noteText = trim(substr($recordData, 6));
  1227. // echo 'Note Length=',$noteLength,'<br />';
  1228. // echo 'Note Text=',$noteText,'<br />';
  1229. if ($extension) {
  1230. // Concatenate this extension with the currently set comment for the cell
  1231. $comment = $this->_phpSheet->getComment( $cellAddress );
  1232. $commentText = $comment->getText()->getPlainText();
  1233. $comment->setText($this->_parseRichText($commentText.$noteText) );
  1234. } else {
  1235. // Set comment for the cell
  1236. $this->_phpSheet->getComment( $cellAddress )
  1237. // ->setAuthor( $author )
  1238. ->setText($this->_parseRichText($noteText) );
  1239. }
  1240. }
  1241. }
  1242. /**
  1243. * The TEXT Object record contains the text associated with a cell annotation.
  1244. */
  1245. private function _readTextObject()
  1246. {
  1247. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1248. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1249. // move stream pointer to next record
  1250. $this->_pos += 4 + $length;
  1251. if ($this->_readDataOnly) {
  1252. return;
  1253. }
  1254. // recordData consists of an array of subrecords looking like this:
  1255. // grbit: 2 bytes; Option Flags
  1256. // rot: 2 bytes; rotation
  1257. // cchText: 2 bytes; length of the text (in the first continue record)
  1258. // cbRuns: 2 bytes; length of the formatting (in the second continue record)
  1259. // followed by the continuation records containing the actual text and formatting
  1260. $grbitOpts = self::_GetInt2d($recordData, 0);
  1261. $rot = self::_GetInt2d($recordData, 2);
  1262. $cchText = self::_GetInt2d($recordData, 10);
  1263. $cbRuns = self::_GetInt2d($recordData, 12);
  1264. $text = $this->_getSplicedRecordData();
  1265. $this->_textObjects[$this->textObjRef] = array(
  1266. 'text' => substr($text["recordData"],$text["spliceOffsets"][0]+1,$cchText),
  1267. 'format' => substr($text["recordData"],$text["spliceOffsets"][1],$cbRuns),
  1268. 'alignment' => $grbitOpts,
  1269. 'rotation' => $rot
  1270. );
  1271. // echo '<b>_readTextObject()</b><br />';
  1272. // var_dump($this->_textObjects[$this->textObjRef]);
  1273. // echo '<br />';
  1274. }
  1275. /**
  1276. * Read BOF
  1277. */
  1278. private function _readBof()
  1279. {
  1280. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1281. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1282. // move stream pointer to next record
  1283. $this->_pos += 4 + $length;
  1284. // offset: 2; size: 2; type of the following data
  1285. $substreamType = self::_GetInt2d($recordData, 2);
  1286. switch ($substreamType) {
  1287. case self::XLS_WorkbookGlobals:
  1288. $version = self::_GetInt2d($recordData, 0);
  1289. if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
  1290. throw new Exception('Cannot read this Excel file. Version is too old.');
  1291. }
  1292. $this->_version = $version;
  1293. break;
  1294. case self::XLS_Worksheet:
  1295. // do not use this version information for anything
  1296. // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
  1297. break;
  1298. default:
  1299. // substream, e.g. chart
  1300. // just skip the entire substream
  1301. do {
  1302. $code = self::_GetInt2d($this->_data, $this->_pos);
  1303. $this->_readDefault();
  1304. } while ($code != self::XLS_Type_EOF && $this->_pos < $this->_dataSize);
  1305. break;
  1306. }
  1307. }
  1308. /**
  1309. * FILEPASS
  1310. *
  1311. * This record is part of the File Protection Block. It
  1312. * contains information about the read/write password of the
  1313. * file. All record contents following this record will be
  1314. * encrypted.
  1315. *
  1316. * -- "OpenOffice.org's Documentation of the Microsoft
  1317. * Excel File Format"
  1318. */
  1319. private function _readFilepass()
  1320. {
  1321. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1322. // $recordData = substr($this->_data, $this->_pos + 4, $length);
  1323. // move stream pointer to next record
  1324. $this->_pos += 4 + $length;
  1325. throw new Exception('Cannot read encrypted file');
  1326. }
  1327. /**
  1328. * CODEPAGE
  1329. *
  1330. * This record stores the text encoding used to write byte
  1331. * strings, stored as MS Windows code page identifier.
  1332. *
  1333. * -- "OpenOffice.org's Documentation of the Microsoft
  1334. * Excel File Format"
  1335. */
  1336. private function _readCodepage()
  1337. {
  1338. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1339. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1340. // move stream pointer to next record
  1341. $this->_pos += 4 + $length;
  1342. // offset: 0; size: 2; code page identifier
  1343. $codepage = self::_GetInt2d($recordData, 0);
  1344. $this->_codepage = PHPExcel_Shared_CodePage::NumberToName($codepage);
  1345. }
  1346. /**
  1347. * DATEMODE
  1348. *
  1349. * This record specifies the base date for displaying date
  1350. * values. All dates are stored as count of days past this
  1351. * base date. In BIFF2-BIFF4 this record is part of the
  1352. * Calculation Settings Block. In BIFF5-BIFF8 it is
  1353. * stored in the Workbook Globals Substream.
  1354. *
  1355. * -- "OpenOffice.org's Documentation of the Microsoft
  1356. * Excel File Format"
  1357. */
  1358. private function _readDateMode()
  1359. {
  1360. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1361. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1362. // move stream pointer to next record
  1363. $this->_pos += 4 + $length;
  1364. // offset: 0; size: 2; 0 = base 1900, 1 = base 1904
  1365. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  1366. if (ord($recordData{0}) == 1) {
  1367. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  1368. }
  1369. }
  1370. /**
  1371. * Read a FONT record
  1372. */
  1373. private function _readFont()
  1374. {
  1375. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1376. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1377. // move stream pointer to next record
  1378. $this->_pos += 4 + $length;
  1379. if (!$this->_readDataOnly) {
  1380. $objFont = new PHPExcel_Style_Font();
  1381. // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
  1382. $size = self::_GetInt2d($recordData, 0);
  1383. $objFont->setSize($size / 20);
  1384. // offset: 2; size: 2; option flags
  1385. // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
  1386. // bit: 1; mask 0x0002; italic
  1387. $isItalic = (0x0002 & self::_GetInt2d($recordData, 2)) >> 1;
  1388. if ($isItalic) $objFont->setItalic(true);
  1389. // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
  1390. // bit: 3; mask 0x0008; strike
  1391. $isStrike = (0x0008 & self::_GetInt2d($recordData, 2)) >> 3;
  1392. if ($isStrike) $objFont->setStrikethrough(true);
  1393. // offset: 4; size: 2; colour index
  1394. $colorIndex = self::_GetInt2d($recordData, 4);
  1395. $objFont->colorIndex = $colorIndex;
  1396. // offset: 6; size: 2; font weight
  1397. $weight = self::_GetInt2d($recordData, 6);
  1398. switch ($weight) {
  1399. case 0x02BC:
  1400. $objFont->setBold(true);
  1401. break;
  1402. }
  1403. // offset: 8; size: 2; escapement type
  1404. $escapement = self::_GetInt2d($recordData, 8);
  1405. switch ($escapement) {
  1406. case 0x0001:
  1407. $objFont->setSuperScript(true);
  1408. break;
  1409. case 0x0002:
  1410. $objFont->setSubScript(true);
  1411. break;
  1412. }
  1413. // offset: 10; size: 1; underline type
  1414. $underlineType = ord($recordData{10});
  1415. switch ($underlineType) {
  1416. case 0x00:
  1417. break; // no underline
  1418. case 0x01:
  1419. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  1420. break;
  1421. case 0x02:
  1422. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  1423. break;
  1424. case 0x21:
  1425. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING);
  1426. break;
  1427. case 0x22:
  1428. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING);
  1429. break;
  1430. }
  1431. // offset: 11; size: 1; font family
  1432. // offset: 12; size: 1; character set
  1433. // offset: 13; size: 1; not used
  1434. // offset: 14; size: var; font name
  1435. if ($this->_version == self::XLS_BIFF8) {
  1436. $string = self::_readUnicodeStringShort(substr($recordData, 14));
  1437. } else {
  1438. $string = $this->_readByteStringShort(substr($recordData, 14));
  1439. }
  1440. $objFont->setName($string['value']);
  1441. $this->_objFonts[] = $objFont;
  1442. }
  1443. }
  1444. /**
  1445. * FORMAT
  1446. *
  1447. * This record contains information about a number format.
  1448. * All FORMAT records occur together in a sequential list.
  1449. *
  1450. * In BIFF2-BIFF4 other records referencing a FORMAT record
  1451. * contain a zero-based index into this list. From BIFF5 on
  1452. * the FORMAT record contains the index itself that will be
  1453. * used by other records.
  1454. *
  1455. * -- "OpenOffice.org's Documentation of the Microsoft
  1456. * Excel File Format"
  1457. */
  1458. private function _readFormat()
  1459. {
  1460. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1461. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1462. // move stream pointer to next record
  1463. $this->_pos += 4 + $length;
  1464. if (!$this->_readDataOnly) {
  1465. $indexCode = self::_GetInt2d($recordData, 0);
  1466. if ($this->_version == self::XLS_BIFF8) {
  1467. $string = self::_readUnicodeStringLong(substr($recordData, 2));
  1468. } else {
  1469. // BIFF7
  1470. $string = $this->_readByteStringShort(substr($recordData, 2));
  1471. }
  1472. $formatString = $string['value'];
  1473. $this->_formats[$indexCode] = $formatString;
  1474. }
  1475. }
  1476. /**
  1477. * XF - Extended Format
  1478. *
  1479. * This record contains formatting information for cells, rows, columns or styles.
  1480. * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
  1481. * and 1 cell XF.
  1482. * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
  1483. * and XF record 15 is a cell XF
  1484. * We only read the first cell style XF and skip the remaining cell style XF records
  1485. * We read all cell XF records.
  1486. *
  1487. * -- "OpenOffice.org's Documentation of the Microsoft
  1488. * Excel File Format"
  1489. */
  1490. private function _readXf()
  1491. {
  1492. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1493. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1494. // move stream pointer to next record
  1495. $this->_pos += 4 + $length;
  1496. $objStyle = new PHPExcel_Style();
  1497. if (!$this->_readDataOnly) {
  1498. // offset: 0; size: 2; Index to FONT record
  1499. if (self::_GetInt2d($recordData, 0) < 4) {
  1500. $fontIndex = self::_GetInt2d($recordData, 0);
  1501. } else {
  1502. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1503. // check the OpenOffice documentation of the FONT record
  1504. $fontIndex = self::_GetInt2d($recordData, 0) - 1;
  1505. }
  1506. $objStyle->setFont($this->_objFonts[$fontIndex]);
  1507. // offset: 2; size: 2; Index to FORMAT record
  1508. $numberFormatIndex = self::_GetInt2d($recordData, 2);
  1509. if (isset($this->_formats[$numberFormatIndex])) {
  1510. // then we have user-defined format code
  1511. $numberformat = array('code' => $this->_formats[$numberFormatIndex]);
  1512. } elseif (($code = PHPExcel_Style_NumberFormat::builtInFormatCode($numberFormatIndex)) !== '') {
  1513. // then we have built-in format code
  1514. $numberformat = array('code' => $code);
  1515. } else {
  1516. // we set the general format code
  1517. $numberformat = array('code' => 'General');
  1518. }
  1519. $objStyle->getNumberFormat()->setFormatCode($numberformat['code']);
  1520. // offset: 4; size: 2; XF type, cell protection, and parent style XF
  1521. // bit 2-0; mask 0x0007; XF_TYPE_PROT
  1522. $xfTypeProt = self::_GetInt2d($recordData, 4);
  1523. // bit 0; mask 0x01; 1 = cell is locked
  1524. $isLocked = (0x01 & $xfTypeProt) >> 0;
  1525. $objStyle->getProtection()->setLocked($isLocked ?
  1526. PHPExcel_Style_Protection::PROTECTION_INHERIT : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1527. // bit 1; mask 0x02; 1 = Formula is hidden
  1528. $isHidden = (0x02 & $xfTypeProt) >> 1;
  1529. $objStyle->getProtection()->setHidden($isHidden ?
  1530. PHPExcel_Style_Protection::PROTECTION_PROTECTED : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1531. // bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
  1532. $isCellStyleXf = (0x04 & $xfTypeProt) >> 2;
  1533. // offset: 6; size: 1; Alignment and text break
  1534. // bit 2-0, mask 0x07; horizontal alignment
  1535. $horAlign = (0x07 & ord($recordData{6})) >> 0;
  1536. switch ($horAlign) {
  1537. case 0:
  1538. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_GENERAL);
  1539. break;
  1540. case 1:
  1541. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1542. break;
  1543. case 2:
  1544. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1545. break;
  1546. case 3:
  1547. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  1548. break;
  1549. case 5:
  1550. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  1551. break;
  1552. case 6:
  1553. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
  1554. break;
  1555. }
  1556. // bit 3, mask 0x08; wrap text
  1557. $wrapText = (0x08 & ord($recordData{6})) >> 3;
  1558. switch ($wrapText) {
  1559. case 0:
  1560. $objStyle->getAlignment()->setWrapText(false);
  1561. break;
  1562. case 1:
  1563. $objStyle->getAlignment()->setWrapText(true);
  1564. break;
  1565. }
  1566. // bit 6-4, mask 0x70; vertical alignment
  1567. $vertAlign = (0x70 & ord($recordData{6})) >> 4;
  1568. switch ($vertAlign) {
  1569. case 0:
  1570. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
  1571. break;
  1572. case 1:
  1573. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1574. break;
  1575. case 2:
  1576. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
  1577. break;
  1578. case 3:
  1579. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY);
  1580. break;
  1581. }
  1582. if ($this->_version == self::XLS_BIFF8) {
  1583. // offset: 7; size: 1; XF_ROTATION: Text rotation angle
  1584. $angle = ord($recordData{7});
  1585. $rotation = 0;
  1586. if ($angle <= 90) {
  1587. $rotation = $angle;
  1588. } else if ($angle <= 180) {
  1589. $rotation = 90 - $angle;
  1590. } else if ($angle == 255) {
  1591. $rotation = -165;
  1592. }
  1593. $objStyle->getAlignment()->setTextRotation($rotation);
  1594. // offset: 8; size: 1; Indentation, shrink to cell size, and text direction
  1595. // bit: 3-0; mask: 0x0F; indent level
  1596. $indent = (0x0F & ord($recordData{8})) >> 0;
  1597. $objStyle->getAlignment()->setIndent($indent);
  1598. // bit: 4; mask: 0x10; 1 = shrink content to fit into cell
  1599. $shrinkToFit = (0x10 & ord($recordData{8})) >> 4;
  1600. switch ($shrinkToFit) {
  1601. case 0:
  1602. $objStyle->getAlignment()->setShrinkToFit(false);
  1603. break;
  1604. case 1:
  1605. $objStyle->getAlignment()->setShrinkToFit(true);
  1606. break;
  1607. }
  1608. // offset: 9; size: 1; Flags used for attribute groups
  1609. // offset: 10; size: 4; Cell border lines and background area
  1610. // bit: 3-0; mask: 0x0000000F; left style
  1611. if ($bordersLeftStyle = self::_mapBorderStyle((0x0000000F & self::_GetInt4d($recordData, 10)) >> 0)) {
  1612. $objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
  1613. }
  1614. // bit: 7-4; mask: 0x000000F0; right style
  1615. if ($bordersRightStyle = self::_mapBorderStyle((0x000000F0 & self::_GetInt4d($recordData, 10)) >> 4)) {
  1616. $objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
  1617. }
  1618. // bit: 11-8; mask: 0x00000F00; top style
  1619. if ($bordersTopStyle = self::_mapBorderStyle((0x00000F00 & self::_GetInt4d($recordData, 10)) >> 8)) {
  1620. $objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
  1621. }
  1622. // bit: 15-12; mask: 0x0000F000; bottom style
  1623. if ($bordersBottomStyle = self::_mapBorderStyle((0x0000F000 & self::_GetInt4d($recordData, 10)) >> 12)) {
  1624. $objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
  1625. }
  1626. // bit: 22-16; mask: 0x007F0000; left color
  1627. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & self::_GetInt4d($recordData, 10)) >> 16;
  1628. // bit: 29-23; mask: 0x3F800000; right color
  1629. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & self::_GetInt4d($recordData, 10)) >> 23;
  1630. // bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
  1631. $diagonalDown = (0x40000000 & self::_GetInt4d($recordData, 10)) >> 30 ?
  1632. true : false;
  1633. // bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
  1634. $diagonalUp = (0x80000000 & self::_GetInt4d($recordData, 10)) >> 31 ?
  1635. true : false;
  1636. if ($diagonalUp == false && $diagonalDown == false) {
  1637. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_NONE);
  1638. } elseif ($diagonalUp == true && $diagonalDown == false) {
  1639. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_UP);
  1640. } elseif ($diagonalUp == false && $diagonalDown == true) {
  1641. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_DOWN);
  1642. } elseif ($diagonalUp == true && $diagonalDown == true) {
  1643. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_BOTH);
  1644. }
  1645. // offset: 14; size: 4;
  1646. // bit: 6-0; mask: 0x0000007F; top color
  1647. $objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & self::_GetInt4d($recordData, 14)) >> 0;
  1648. // bit: 13-7; mask: 0x00003F80; bottom color
  1649. $objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & self::_GetInt4d($recordData, 14)) >> 7;
  1650. // bit: 20-14; mask: 0x001FC000; diagonal color
  1651. $objStyle->getBorders()->getDiagonal()->colorIndex = (0x001FC000 & self::_GetInt4d($recordData, 14)) >> 14;
  1652. // bit: 24-21; mask: 0x01E00000; diagonal style
  1653. if ($bordersDiagonalStyle = self::_mapBorderStyle((0x01E00000 & self::_GetInt4d($recordData, 14)) >> 21)) {
  1654. $objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
  1655. }
  1656. // bit: 31-26; mask: 0xFC000000 fill pattern
  1657. if ($fillType = self::_mapFillPattern((0xFC000000 & self::_GetInt4d($recordData, 14)) >> 26)) {
  1658. $objStyle->getFill()->setFillType($fillType);
  1659. }
  1660. // offset: 18; size: 2; pattern and background colour
  1661. // bit: 6-0; mask: 0x007F; color index for pattern color
  1662. $objStyle->getFill()->startcolorIndex = (0x007F & self::_GetInt2d($recordData, 18)) >> 0;
  1663. // bit: 13-7; mask: 0x3F80; color index for pattern background
  1664. $objStyle->getFill()->endcolorIndex = (0x3F80 & self::_GetInt2d($recordData, 18)) >> 7;
  1665. } else {
  1666. // BIFF5
  1667. // offset: 7; size: 1; Text orientation and flags
  1668. $orientationAndFlags = ord($recordData{7});
  1669. // bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
  1670. $xfOrientation = (0x03 & $orientationAndFlags) >> 0;
  1671. switch ($xfOrientation) {
  1672. case 0:
  1673. $objStyle->getAlignment()->setTextRotation(0);
  1674. break;
  1675. case 1:
  1676. $objStyle->getAlignment()->setTextRotation(-165);
  1677. break;
  1678. case 2:
  1679. $objStyle->getAlignment()->setTextRotation(90);
  1680. break;
  1681. case 3:
  1682. $objStyle->getAlignment()->setTextRotation(-90);
  1683. break;
  1684. }
  1685. // offset: 8; size: 4; cell border lines and background area
  1686. $borderAndBackground = self::_GetInt4d($recordData, 8);
  1687. // bit: 6-0; mask: 0x0000007F; color index for pattern color
  1688. $objStyle->getFill()->startcolorIndex = (0x0000007F & $borderAndBackground) >> 0;
  1689. // bit: 13-7; mask: 0x00003F80; color index for pattern background
  1690. $objStyle->getFill()->endcolorIndex = (0x00003F80 & $borderAndBackground) >> 7;
  1691. // bit: 21-16; mask: 0x003F0000; fill pattern
  1692. $objStyle->getFill()->setFillType(self::_mapFillPattern((0x003F0000 & $borderAndBackground) >> 16));
  1693. // bit: 24-22; mask: 0x01C00000; bottom line style
  1694. $objStyle->getBorders()->getBottom()->setBorderStyle(self::_mapBorderStyle((0x01C00000 & $borderAndBackground) >> 22));
  1695. // bit: 31-25; mask: 0xFE000000; bottom line color
  1696. $objStyle->getBorders()->getBottom()->colorIndex = (0xFE000000 & $borderAndBackground) >> 25;
  1697. // offset: 12; size: 4; cell border lines
  1698. $borderLines = self::_GetInt4d($recordData, 12);
  1699. // bit: 2-0; mask: 0x00000007; top line style
  1700. $objStyle->getBorders()->getTop()->setBorderStyle(self::_mapBorderStyle((0x00000007 & $borderLines) >> 0));
  1701. // bit: 5-3; mask: 0x00000038; left line style
  1702. $objStyle->getBorders()->getLeft()->setBorderStyle(self::_mapBorderStyle((0x00000038 & $borderLines) >> 3));
  1703. // bit: 8-6; mask: 0x000001C0; right line style
  1704. $objStyle->getBorders()->getRight()->setBorderStyle(self::_mapBorderStyle((0x000001C0 & $borderLines) >> 6));
  1705. // bit: 15-9; mask: 0x0000FE00; top line color index
  1706. $objStyle->getBorders()->getTop()->colorIndex = (0x0000FE00 & $borderLines) >> 9;
  1707. // bit: 22-16; mask: 0x007F0000; left line color index
  1708. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $borderLines) >> 16;
  1709. // bit: 29-23; mask: 0x3F800000; right line color index
  1710. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $borderLines) >> 23;
  1711. }
  1712. // add cellStyleXf or cellXf and update mapping
  1713. if ($isCellStyleXf) {
  1714. // we only read one style XF record which is always the first
  1715. if ($this->_xfIndex == 0) {
  1716. $this->_phpExcel->addCellStyleXf($objStyle);
  1717. $this->_mapCellStyleXfIndex[$this->_xfIndex] = 0;
  1718. }
  1719. } else {
  1720. // we read all cell XF records
  1721. $this->_phpExcel->addCellXf($objStyle);
  1722. $this->_mapCellXfIndex[$this->_xfIndex] = count($this->_phpExcel->getCellXfCollection()) - 1;
  1723. }
  1724. // update XF index for when we read next record
  1725. ++$this->_xfIndex;
  1726. }
  1727. }
  1728. /**
  1729. *
  1730. */
  1731. private function _readXfExt()
  1732. {
  1733. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1734. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1735. // move stream pointer to next record
  1736. $this->_pos += 4 + $length;
  1737. if (!$this->_readDataOnly) {
  1738. // offset: 0; size: 2; 0x087D = repeated header
  1739. // offset: 2; size: 2
  1740. // offset: 4; size: 8; not used
  1741. // offset: 12; size: 2; record version
  1742. // offset: 14; size: 2; index to XF record which this record modifies
  1743. $ixfe = self::_GetInt2d($recordData, 14);
  1744. // offset: 16; size: 2; not used
  1745. // offset: 18; size: 2; number of extension properties that follow
  1746. $cexts = self::_GetInt2d($recordData, 18);
  1747. // start reading the actual extension data
  1748. $offset = 20;
  1749. while ($offset < $length) {
  1750. // extension type
  1751. $extType = self::_GetInt2d($recordData, $offset);
  1752. // extension length
  1753. $cb = self::_GetInt2d($recordData, $offset + 2);
  1754. // extension data
  1755. $extData = substr($recordData, $offset + 4, $cb);
  1756. switch ($extType) {
  1757. case 4: // fill start color
  1758. $xclfType = self::_GetInt2d($extData, 0); // color type
  1759. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1760. if ($xclfType == 2) {
  1761. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1762. // modify the relevant style property
  1763. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1764. $fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  1765. $fill->getStartColor()->setRGB($rgb);
  1766. unset($fill->startcolorIndex); // normal color index does not apply, discard
  1767. }
  1768. }
  1769. break;
  1770. case 5: // fill end color
  1771. $xclfType = self::_GetInt2d($extData, 0); // color type
  1772. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1773. if ($xclfType == 2) {
  1774. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1775. // modify the relevant style property
  1776. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1777. $fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  1778. $fill->getEndColor()->setRGB($rgb);
  1779. unset($fill->endcolorIndex); // normal color index does not apply, discard
  1780. }
  1781. }
  1782. break;
  1783. case 7: // border color top
  1784. $xclfType = self::_GetInt2d($extData, 0); // color type
  1785. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1786. if ($xclfType == 2) {
  1787. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1788. // modify the relevant style property
  1789. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1790. $top = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getTop();
  1791. $top->getColor()->setRGB($rgb);
  1792. unset($top->colorIndex); // normal color index does not apply, discard
  1793. }
  1794. }
  1795. break;
  1796. case 8: // border color bottom
  1797. $xclfType = self::_GetInt2d($extData, 0); // color type
  1798. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1799. if ($xclfType == 2) {
  1800. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1801. // modify the relevant style property
  1802. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1803. $bottom = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getBottom();
  1804. $bottom->getColor()->setRGB($rgb);
  1805. unset($bottom->colorIndex); // normal color index does not apply, discard
  1806. }
  1807. }
  1808. break;
  1809. case 9: // border color left
  1810. $xclfType = self::_GetInt2d($extData, 0); // color type
  1811. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1812. if ($xclfType == 2) {
  1813. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1814. // modify the relevant style property
  1815. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1816. $left = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getLeft();
  1817. $left->getColor()->setRGB($rgb);
  1818. unset($left->colorIndex); // normal color index does not apply, discard
  1819. }
  1820. }
  1821. break;
  1822. case 10: // border color right
  1823. $xclfType = self::_GetInt2d($extData, 0); // color type
  1824. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1825. if ($xclfType == 2) {
  1826. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1827. // modify the relevant style property
  1828. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1829. $right = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getRight();
  1830. $right->getColor()->setRGB($rgb);
  1831. unset($right->colorIndex); // normal color index does not apply, discard
  1832. }
  1833. }
  1834. break;
  1835. case 11: // border color diagonal
  1836. $xclfType = self::_GetInt2d($extData, 0); // color type
  1837. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1838. if ($xclfType == 2) {
  1839. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1840. // modify the relevant style property
  1841. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1842. $diagonal = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getDiagonal();
  1843. $diagonal->getColor()->setRGB($rgb);
  1844. unset($diagonal->colorIndex); // normal color index does not apply, discard
  1845. }
  1846. }
  1847. break;
  1848. case 13: // font color
  1849. $xclfType = self::_GetInt2d($extData, 0); // color type
  1850. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1851. if ($xclfType == 2) {
  1852. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1853. // modify the relevant style property
  1854. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1855. $font = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFont();
  1856. $font->getColor()->setRGB($rgb);
  1857. unset($font->colorIndex); // normal color index does not apply, discard
  1858. }
  1859. }
  1860. break;
  1861. }
  1862. $offset += $cb;
  1863. }
  1864. }
  1865. }
  1866. /**
  1867. * Read STYLE record
  1868. */
  1869. private function _readStyle()
  1870. {
  1871. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1872. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1873. // move stream pointer to next record
  1874. $this->_pos += 4 + $length;
  1875. if (!$this->_readDataOnly) {
  1876. // offset: 0; size: 2; index to XF record and flag for built-in style
  1877. $ixfe = self::_GetInt2d($recordData, 0);
  1878. // bit: 11-0; mask 0x0FFF; index to XF record
  1879. $xfIndex = (0x0FFF & $ixfe) >> 0;
  1880. // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
  1881. $isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15);
  1882. if ($isBuiltIn) {
  1883. // offset: 2; size: 1; identifier for built-in style
  1884. $builtInId = ord($recordData{2});
  1885. switch ($builtInId) {
  1886. case 0x00:
  1887. // currently, we are not using this for anything
  1888. break;
  1889. default:
  1890. break;
  1891. }
  1892. } else {
  1893. // user-defined; not supported by PHPExcel
  1894. }
  1895. }
  1896. }
  1897. /**
  1898. * Read PALETTE record
  1899. */
  1900. private function _readPalette()
  1901. {
  1902. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1903. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1904. // move stream pointer to next record
  1905. $this->_pos += 4 + $length;
  1906. if (!$this->_readDataOnly) {
  1907. // offset: 0; size: 2; number of following colors
  1908. $nm = self::_GetInt2d($recordData, 0);
  1909. // list of RGB colors
  1910. for ($i = 0; $i < $nm; ++$i) {
  1911. $rgb = substr($recordData, 2 + 4 * $i, 4);
  1912. $this->_palette[] = self::_readRGB($rgb);
  1913. }
  1914. }
  1915. }
  1916. /**
  1917. * SHEET
  1918. *
  1919. * This record is located in the Workbook Globals
  1920. * Substream and represents a sheet inside the workbook.
  1921. * One SHEET record is written for each sheet. It stores the
  1922. * sheet name and a stream offset to the BOF record of the
  1923. * respective Sheet Substream within the Workbook Stream.
  1924. *
  1925. * -- "OpenOffice.org's Documentation of the Microsoft
  1926. * Excel File Format"
  1927. */
  1928. private function _readSheet()
  1929. {
  1930. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1931. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1932. // move stream pointer to next record
  1933. $this->_pos += 4 + $length;
  1934. // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
  1935. $rec_offset = self::_GetInt4d($recordData, 0);
  1936. // offset: 4; size: 1; sheet state
  1937. switch (ord($recordData{4})) {
  1938. case 0x00: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE; break;
  1939. case 0x01: $sheetState = PHPExcel_Worksheet::SHEETSTATE_HIDDEN; break;
  1940. case 0x02: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VERYHIDDEN; break;
  1941. default: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE; break;
  1942. }
  1943. // offset: 5; size: 1; sheet type
  1944. $sheetType = ord($recordData{5});
  1945. // offset: 6; size: var; sheet name
  1946. if ($this->_version == self::XLS_BIFF8) {
  1947. $string = self::_readUnicodeStringShort(substr($recordData, 6));
  1948. $rec_name = $string['value'];
  1949. } elseif ($this->_version == self::XLS_BIFF7) {
  1950. $string = $this->_readByteStringShort(substr($recordData, 6));
  1951. $rec_name = $string['value'];
  1952. }
  1953. $this->_sheets[] = array(
  1954. 'name' => $rec_name,
  1955. 'offset' => $rec_offset,
  1956. 'sheetState' => $sheetState,
  1957. 'sheetType' => $sheetType,
  1958. );
  1959. }
  1960. /**
  1961. * Read EXTERNALBOOK record
  1962. */
  1963. private function _readExternalBook()
  1964. {
  1965. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1966. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1967. // move stream pointer to next record
  1968. $this->_pos += 4 + $length;
  1969. // offset within record data
  1970. $offset = 0;
  1971. // there are 4 types of records
  1972. if (strlen($recordData) > 4) {
  1973. // external reference
  1974. // offset: 0; size: 2; number of sheet names ($nm)
  1975. $nm = self::_GetInt2d($recordData, 0);
  1976. $offset += 2;
  1977. // offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
  1978. $encodedUrlString = self::_readUnicodeStringLong(substr($recordData, 2));
  1979. $offset += $encodedUrlString['size'];
  1980. // offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
  1981. $externalSheetNames = array();
  1982. for ($i = 0; $i < $nm; ++$i) {
  1983. $externalSheetNameString = self::_readUnicodeStringLong(substr($recordData, $offset));
  1984. $externalSheetNames[] = $externalSheetNameString['value'];
  1985. $offset += $externalSheetNameString['size'];
  1986. }
  1987. // store the record data
  1988. $this->_externalBooks[] = array(
  1989. 'type' => 'external',
  1990. 'encodedUrl' => $encodedUrlString['value'],
  1991. 'externalSheetNames' => $externalSheetNames,
  1992. );
  1993. } elseif (substr($recordData, 2, 2) == pack('CC', 0x01, 0x04)) {
  1994. // internal reference
  1995. // offset: 0; size: 2; number of sheet in this document
  1996. // offset: 2; size: 2; 0x01 0x04
  1997. $this->_externalBooks[] = array(
  1998. 'type' => 'internal',
  1999. );
  2000. } elseif (substr($recordData, 0, 4) == pack('vCC', 0x0001, 0x01, 0x3A)) {
  2001. // add-in function
  2002. // offset: 0; size: 2; 0x0001
  2003. $this->_externalBooks[] = array(
  2004. 'type' => 'addInFunction',
  2005. );
  2006. } elseif (substr($recordData, 0, 2) == pack('v', 0x0000)) {
  2007. // DDE links, OLE links
  2008. // offset: 0; size: 2; 0x0000
  2009. // offset: 2; size: var; encoded source document name
  2010. $this->_externalBooks[] = array(
  2011. 'type' => 'DDEorOLE',
  2012. );
  2013. }
  2014. }
  2015. /**
  2016. * Read EXTERNNAME record.
  2017. */
  2018. private function _readExternName()
  2019. {
  2020. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2021. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2022. // move stream pointer to next record
  2023. $this->_pos += 4 + $length;
  2024. // external sheet references provided for named cells
  2025. if ($this->_version == self::XLS_BIFF8) {
  2026. // offset: 0; size: 2; options
  2027. $options = self::_GetInt2d($recordData, 0);
  2028. // offset: 2; size: 2;
  2029. // offset: 4; size: 2; not used
  2030. // offset: 6; size: var
  2031. $nameString = self::_readUnicodeStringShort(substr($recordData, 6));
  2032. // offset: var; size: var; formula data
  2033. $offset = 6 + $nameString['size'];
  2034. $formula = $this->_getFormulaFromStructure(substr($recordData, $offset));
  2035. $this->_externalNames[] = array(
  2036. 'name' => $nameString['value'],
  2037. 'formula' => $formula,
  2038. );
  2039. }
  2040. }
  2041. /**
  2042. * Read EXTERNSHEET record
  2043. */
  2044. private function _readExternSheet()
  2045. {
  2046. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2047. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2048. // move stream pointer to next record
  2049. $this->_pos += 4 + $length;
  2050. // external sheet references provided for named cells
  2051. if ($this->_version == self::XLS_BIFF8) {
  2052. // offset: 0; size: 2; number of following ref structures
  2053. $nm = self::_GetInt2d($recordData, 0);
  2054. for ($i = 0; $i < $nm; ++$i) {
  2055. $this->_ref[] = array(
  2056. // offset: 2 + 6 * $i; index to EXTERNALBOOK record
  2057. 'externalBookIndex' => self::_GetInt2d($recordData, 2 + 6 * $i),
  2058. // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
  2059. 'firstSheetIndex' => self::_GetInt2d($recordData, 4 + 6 * $i),
  2060. // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
  2061. 'lastSheetIndex' => self::_GetInt2d($recordData, 6 + 6 * $i),
  2062. );
  2063. }
  2064. }
  2065. }
  2066. /**
  2067. * DEFINEDNAME
  2068. *
  2069. * This record is part of a Link Table. It contains the name
  2070. * and the token array of an internal defined name. Token
  2071. * arrays of defined names contain tokens with aberrant
  2072. * token classes.
  2073. *
  2074. * -- "OpenOffice.org's Documentation of the Microsoft
  2075. * Excel File Format"
  2076. */
  2077. private function _readDefinedName()
  2078. {
  2079. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2080. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2081. // move stream pointer to next record
  2082. $this->_pos += 4 + $length;
  2083. if ($this->_version == self::XLS_BIFF8) {
  2084. // retrieves named cells
  2085. // offset: 0; size: 2; option flags
  2086. $opts = self::_GetInt2d($recordData, 0);
  2087. // bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
  2088. $isBuiltInName = (0x0020 & $opts) >> 5;
  2089. // offset: 2; size: 1; keyboard shortcut
  2090. // offset: 3; size: 1; length of the name (character count)
  2091. $nlen = ord($recordData{3});
  2092. // offset: 4; size: 2; size of the formula data (it can happen that this is zero)
  2093. // note: there can also be additional data, this is not included in $flen
  2094. $flen = self::_GetInt2d($recordData, 4);
  2095. // offset: 8; size: 2; 0=Global name, otherwise index to sheet (1-based)
  2096. $scope = self::_GetInt2d($recordData, 8);
  2097. // offset: 14; size: var; Name (Unicode string without length field)
  2098. $string = self::_readUnicodeString(substr($recordData, 14), $nlen);
  2099. // offset: var; size: $flen; formula data
  2100. $offset = 14 + $string['size'];
  2101. $formulaStructure = pack('v', $flen) . substr($recordData, $offset);
  2102. try {
  2103. $formula = $this->_getFormulaFromStructure($formulaStructure);
  2104. } catch (Exception $e) {
  2105. $formula = '';
  2106. }
  2107. $this->_definedname[] = array(
  2108. 'isBuiltInName' => $isBuiltInName,
  2109. 'name' => $string['value'],
  2110. 'formula' => $formula,
  2111. 'scope' => $scope,
  2112. );
  2113. }
  2114. }
  2115. /**
  2116. * Read MSODRAWINGGROUP record
  2117. */
  2118. private function _readMsoDrawingGroup()
  2119. {
  2120. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2121. // get spliced record data
  2122. $splicedRecordData = $this->_getSplicedRecordData();
  2123. $recordData = $splicedRecordData['recordData'];
  2124. $this->_drawingGroupData .= $recordData;
  2125. }
  2126. /**
  2127. * SST - Shared String Table
  2128. *
  2129. * This record contains a list of all strings used anywhere
  2130. * in the workbook. Each string occurs only once. The
  2131. * workbook uses indexes into the list to reference the
  2132. * strings.
  2133. *
  2134. * -- "OpenOffice.org's Documentation of the Microsoft
  2135. * Excel File Format"
  2136. **/
  2137. private function _readSst()
  2138. {
  2139. // offset within (spliced) record data
  2140. $pos = 0;
  2141. // get spliced record data
  2142. $splicedRecordData = $this->_getSplicedRecordData();
  2143. $recordData = $splicedRecordData['recordData'];
  2144. $spliceOffsets = $splicedRecordData['spliceOffsets'];
  2145. // offset: 0; size: 4; total number of strings in the workbook
  2146. $pos += 4;
  2147. // offset: 4; size: 4; number of following strings ($nm)
  2148. $nm = self::_GetInt4d($recordData, 4);
  2149. $pos += 4;
  2150. // loop through the Unicode strings (16-bit length)
  2151. for ($i = 0; $i < $nm; ++$i) {
  2152. // number of characters in the Unicode string
  2153. $numChars = self::_GetInt2d($recordData, $pos);
  2154. $pos += 2;
  2155. // option flags
  2156. $optionFlags = ord($recordData{$pos});
  2157. ++$pos;
  2158. // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  2159. $isCompressed = (($optionFlags & 0x01) == 0) ;
  2160. // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  2161. $hasAsian = (($optionFlags & 0x04) != 0);
  2162. // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  2163. $hasRichText = (($optionFlags & 0x08) != 0);
  2164. if ($hasRichText) {
  2165. // number of Rich-Text formatting runs
  2166. $formattingRuns = self::_GetInt2d($recordData, $pos);
  2167. $pos += 2;
  2168. }
  2169. if ($hasAsian) {
  2170. // size of Asian phonetic setting
  2171. $extendedRunLength = self::_GetInt4d($recordData, $pos);
  2172. $pos += 4;
  2173. }
  2174. // expected byte length of character array if not split
  2175. $len = ($isCompressed) ? $numChars : $numChars * 2;
  2176. // look up limit position
  2177. foreach ($spliceOffsets as $spliceOffset) {
  2178. // it can happen that the string is empty, therefore we need
  2179. // <= and not just <
  2180. if ($pos <= $spliceOffset) {
  2181. $limitpos = $spliceOffset;
  2182. break;
  2183. }
  2184. }
  2185. if ($pos + $len <= $limitpos) {
  2186. // character array is not split between records
  2187. $retstr = substr($recordData, $pos, $len);
  2188. $pos += $len;
  2189. } else {
  2190. // character array is split between records
  2191. // first part of character array
  2192. $retstr = substr($recordData, $pos, $limitpos - $pos);
  2193. $bytesRead = $limitpos - $pos;
  2194. // remaining characters in Unicode string
  2195. $charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2));
  2196. $pos = $limitpos;
  2197. // keep reading the characters
  2198. while ($charsLeft > 0) {
  2199. // look up next limit position, in case the string span more than one continue record
  2200. foreach ($spliceOffsets as $spliceOffset) {
  2201. if ($pos < $spliceOffset) {
  2202. $limitpos = $spliceOffset;
  2203. break;
  2204. }
  2205. }
  2206. // repeated option flags
  2207. // OpenOffice.org documentation 5.21
  2208. $option = ord($recordData{$pos});
  2209. ++$pos;
  2210. if ($isCompressed && ($option == 0)) {
  2211. // 1st fragment compressed
  2212. // this fragment compressed
  2213. $len = min($charsLeft, $limitpos - $pos);
  2214. $retstr .= substr($recordData, $pos, $len);
  2215. $charsLeft -= $len;
  2216. $isCompressed = true;
  2217. } elseif (!$isCompressed && ($option != 0)) {
  2218. // 1st fragment uncompressed
  2219. // this fragment uncompressed
  2220. $len = min($charsLeft * 2, $limitpos - $pos);
  2221. $retstr .= substr($recordData, $pos, $len);
  2222. $charsLeft -= $len / 2;
  2223. $isCompressed = false;
  2224. } elseif (!$isCompressed && ($option == 0)) {
  2225. // 1st fragment uncompressed
  2226. // this fragment compressed
  2227. $len = min($charsLeft, $limitpos - $pos);
  2228. for ($j = 0; $j < $len; ++$j) {
  2229. $retstr .= $recordData{$pos + $j} . chr(0);
  2230. }
  2231. $charsLeft -= $len;
  2232. $isCompressed = false;
  2233. } else {
  2234. // 1st fragment compressed
  2235. // this fragment uncompressed
  2236. $newstr = '';
  2237. for ($j = 0; $j < strlen($retstr); ++$j) {
  2238. $newstr .= $retstr[$j] . chr(0);
  2239. }
  2240. $retstr = $newstr;
  2241. $len = min($charsLeft * 2, $limitpos - $pos);
  2242. $retstr .= substr($recordData, $pos, $len);
  2243. $charsLeft -= $len / 2;
  2244. $isCompressed = false;
  2245. }
  2246. $pos += $len;
  2247. }
  2248. }
  2249. // convert to UTF-8
  2250. $retstr = self::_encodeUTF16($retstr, $isCompressed);
  2251. // read additional Rich-Text information, if any
  2252. $fmtRuns = array();
  2253. if ($hasRichText) {
  2254. // list of formatting runs
  2255. for ($j = 0; $j < $formattingRuns; ++$j) {
  2256. // first formatted character; zero-based
  2257. $charPos = self::_GetInt2d($recordData, $pos + $j * 4);
  2258. // index to font record
  2259. $fontIndex = self::_GetInt2d($recordData, $pos + 2 + $j * 4);
  2260. $fmtRuns[] = array(
  2261. 'charPos' => $charPos,
  2262. 'fontIndex' => $fontIndex,
  2263. );
  2264. }
  2265. $pos += 4 * $formattingRuns;
  2266. }
  2267. // read additional Asian phonetics information, if any
  2268. if ($hasAsian) {
  2269. // For Asian phonetic settings, we skip the extended string data
  2270. $pos += $extendedRunLength;
  2271. }
  2272. // store the shared sting
  2273. $this->_sst[] = array(
  2274. 'value' => $retstr,
  2275. 'fmtRuns' => $fmtRuns,
  2276. );
  2277. }
  2278. // _getSplicedRecordData() takes care of moving current position in data stream
  2279. }
  2280. /**
  2281. * Read PRINTGRIDLINES record
  2282. */
  2283. private function _readPrintGridlines()
  2284. {
  2285. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2286. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2287. // move stream pointer to next record
  2288. $this->_pos += 4 + $length;
  2289. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2290. // offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
  2291. $printGridlines = (bool) self::_GetInt2d($recordData, 0);
  2292. $this->_phpSheet->setPrintGridlines($printGridlines);
  2293. }
  2294. }
  2295. /**
  2296. * Read DEFAULTROWHEIGHT record
  2297. */
  2298. private function _readDefaultRowHeight()
  2299. {
  2300. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2301. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2302. // move stream pointer to next record
  2303. $this->_pos += 4 + $length;
  2304. // offset: 0; size: 2; option flags
  2305. // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
  2306. $height = self::_GetInt2d($recordData, 2);
  2307. $this->_phpSheet->getDefaultRowDimension()->setRowHeight($height / 20);
  2308. }
  2309. /**
  2310. * Read SHEETPR record
  2311. */
  2312. private function _readSheetPr()
  2313. {
  2314. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2315. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2316. // move stream pointer to next record
  2317. $this->_pos += 4 + $length;
  2318. // offset: 0; size: 2
  2319. // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
  2320. $isSummaryBelow = (0x0040 & self::_GetInt2d($recordData, 0)) >> 6;
  2321. $this->_phpSheet->setShowSummaryBelow($isSummaryBelow);
  2322. // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
  2323. $isSummaryRight = (0x0080 & self::_GetInt2d($recordData, 0)) >> 7;
  2324. $this->_phpSheet->setShowSummaryRight($isSummaryRight);
  2325. // bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
  2326. // this corresponds to radio button setting in page setup dialog in Excel
  2327. $this->_isFitToPages = (bool) ((0x0100 & self::_GetInt2d($recordData, 0)) >> 8);
  2328. }
  2329. /**
  2330. * Read HORIZONTALPAGEBREAKS record
  2331. */
  2332. private function _readHorizontalPageBreaks()
  2333. {
  2334. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2335. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2336. // move stream pointer to next record
  2337. $this->_pos += 4 + $length;
  2338. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2339. // offset: 0; size: 2; number of the following row index structures
  2340. $nm = self::_GetInt2d($recordData, 0);
  2341. // offset: 2; size: 6 * $nm; list of $nm row index structures
  2342. for ($i = 0; $i < $nm; ++$i) {
  2343. $r = self::_GetInt2d($recordData, 2 + 6 * $i);
  2344. $cf = self::_GetInt2d($recordData, 2 + 6 * $i + 2);
  2345. $cl = self::_GetInt2d($recordData, 2 + 6 * $i + 4);
  2346. // not sure why two column indexes are necessary?
  2347. $this->_phpSheet->setBreakByColumnAndRow($cf, $r, PHPExcel_Worksheet::BREAK_ROW);
  2348. }
  2349. }
  2350. }
  2351. /**
  2352. * Read VERTICALPAGEBREAKS record
  2353. */
  2354. private function _readVerticalPageBreaks()
  2355. {
  2356. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2357. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2358. // move stream pointer to next record
  2359. $this->_pos += 4 + $length;
  2360. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2361. // offset: 0; size: 2; number of the following column index structures
  2362. $nm = self::_GetInt2d($recordData, 0);
  2363. // offset: 2; size: 6 * $nm; list of $nm row index structures
  2364. for ($i = 0; $i < $nm; ++$i) {
  2365. $c = self::_GetInt2d($recordData, 2 + 6 * $i);
  2366. $rf = self::_GetInt2d($recordData, 2 + 6 * $i + 2);
  2367. $rl = self::_GetInt2d($recordData, 2 + 6 * $i + 4);
  2368. // not sure why two row indexes are necessary?
  2369. $this->_phpSheet->setBreakByColumnAndRow($c, $rf, PHPExcel_Worksheet::BREAK_COLUMN);
  2370. }
  2371. }
  2372. }
  2373. /**
  2374. * Read HEADER record
  2375. */
  2376. private function _readHeader()
  2377. {
  2378. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2379. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2380. // move stream pointer to next record
  2381. $this->_pos += 4 + $length;
  2382. if (!$this->_readDataOnly) {
  2383. // offset: 0; size: var
  2384. // realized that $recordData can be empty even when record exists
  2385. if ($recordData) {
  2386. if ($this->_version == self::XLS_BIFF8) {
  2387. $string = self::_readUnicodeStringLong($recordData);
  2388. } else {
  2389. $string = $this->_readByteStringShort($recordData);
  2390. }
  2391. $this->_phpSheet->getHeaderFooter()->setOddHeader($string['value']);
  2392. $this->_phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
  2393. }
  2394. }
  2395. }
  2396. /**
  2397. * Read FOOTER record
  2398. */
  2399. private function _readFooter()
  2400. {
  2401. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2402. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2403. // move stream pointer to next record
  2404. $this->_pos += 4 + $length;
  2405. if (!$this->_readDataOnly) {
  2406. // offset: 0; size: var
  2407. // realized that $recordData can be empty even when record exists
  2408. if ($recordData) {
  2409. if ($this->_version == self::XLS_BIFF8) {
  2410. $string = self::_readUnicodeStringLong($recordData);
  2411. } else {
  2412. $string = $this->_readByteStringShort($recordData);
  2413. }
  2414. $this->_phpSheet->getHeaderFooter()->setOddFooter($string['value']);
  2415. $this->_phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
  2416. }
  2417. }
  2418. }
  2419. /**
  2420. * Read HCENTER record
  2421. */
  2422. private function _readHcenter()
  2423. {
  2424. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2425. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2426. // move stream pointer to next record
  2427. $this->_pos += 4 + $length;
  2428. if (!$this->_readDataOnly) {
  2429. // offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
  2430. $isHorizontalCentered = (bool) self::_GetInt2d($recordData, 0);
  2431. $this->_phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
  2432. }
  2433. }
  2434. /**
  2435. * Read VCENTER record
  2436. */
  2437. private function _readVcenter()
  2438. {
  2439. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2440. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2441. // move stream pointer to next record
  2442. $this->_pos += 4 + $length;
  2443. if (!$this->_readDataOnly) {
  2444. // offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
  2445. $isVerticalCentered = (bool) self::_GetInt2d($recordData, 0);
  2446. $this->_phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
  2447. }
  2448. }
  2449. /**
  2450. * Read LEFTMARGIN record
  2451. */
  2452. private function _readLeftMargin()
  2453. {
  2454. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2455. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2456. // move stream pointer to next record
  2457. $this->_pos += 4 + $length;
  2458. if (!$this->_readDataOnly) {
  2459. // offset: 0; size: 8
  2460. $this->_phpSheet->getPageMargins()->setLeft(self::_extractNumber($recordData));
  2461. }
  2462. }
  2463. /**
  2464. * Read RIGHTMARGIN record
  2465. */
  2466. private function _readRightMargin()
  2467. {
  2468. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2469. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2470. // move stream pointer to next record
  2471. $this->_pos += 4 + $length;
  2472. if (!$this->_readDataOnly) {
  2473. // offset: 0; size: 8
  2474. $this->_phpSheet->getPageMargins()->setRight(self::_extractNumber($recordData));
  2475. }
  2476. }
  2477. /**
  2478. * Read TOPMARGIN record
  2479. */
  2480. private function _readTopMargin()
  2481. {
  2482. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2483. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2484. // move stream pointer to next record
  2485. $this->_pos += 4 + $length;
  2486. if (!$this->_readDataOnly) {
  2487. // offset: 0; size: 8
  2488. $this->_phpSheet->getPageMargins()->setTop(self::_extractNumber($recordData));
  2489. }
  2490. }
  2491. /**
  2492. * Read BOTTOMMARGIN record
  2493. */
  2494. private function _readBottomMargin()
  2495. {
  2496. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2497. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2498. // move stream pointer to next record
  2499. $this->_pos += 4 + $length;
  2500. if (!$this->_readDataOnly) {
  2501. // offset: 0; size: 8
  2502. $this->_phpSheet->getPageMargins()->setBottom(self::_extractNumber($recordData));
  2503. }
  2504. }
  2505. /**
  2506. * Read PAGESETUP record
  2507. */
  2508. private function _readPageSetup()
  2509. {
  2510. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2511. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2512. // move stream pointer to next record
  2513. $this->_pos += 4 + $length;
  2514. if (!$this->_readDataOnly) {
  2515. // offset: 0; size: 2; paper size
  2516. $paperSize = self::_GetInt2d($recordData, 0);
  2517. // offset: 2; size: 2; scaling factor
  2518. $scale = self::_GetInt2d($recordData, 2);
  2519. // offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
  2520. $fitToWidth = self::_GetInt2d($recordData, 6);
  2521. // offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
  2522. $fitToHeight = self::_GetInt2d($recordData, 8);
  2523. // offset: 10; size: 2; option flags
  2524. // bit: 1; mask: 0x0002; 0=landscape, 1=portrait
  2525. $isPortrait = (0x0002 & self::_GetInt2d($recordData, 10)) >> 1;
  2526. // bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
  2527. // when this bit is set, do not use flags for those properties
  2528. $isNotInit = (0x0004 & self::_GetInt2d($recordData, 10)) >> 2;
  2529. if (!$isNotInit) {
  2530. $this->_phpSheet->getPageSetup()->setPaperSize($paperSize);
  2531. switch ($isPortrait) {
  2532. case 0: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); break;
  2533. case 1: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); break;
  2534. }
  2535. $this->_phpSheet->getPageSetup()->setScale($scale, false);
  2536. $this->_phpSheet->getPageSetup()->setFitToPage((bool) $this->_isFitToPages);
  2537. $this->_phpSheet->getPageSetup()->setFitToWidth($fitToWidth, false);
  2538. $this->_phpSheet->getPageSetup()->setFitToHeight($fitToHeight, false);
  2539. }
  2540. // offset: 16; size: 8; header margin (IEEE 754 floating-point value)
  2541. $marginHeader = self::_extractNumber(substr($recordData, 16, 8));
  2542. $this->_phpSheet->getPageMargins()->setHeader($marginHeader);
  2543. // offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
  2544. $marginFooter = self::_extractNumber(substr($recordData, 24, 8));
  2545. $this->_phpSheet->getPageMargins()->setFooter($marginFooter);
  2546. }
  2547. }
  2548. /**
  2549. * PROTECT - Sheet protection (BIFF2 through BIFF8)
  2550. * if this record is omitted, then it also means no sheet protection
  2551. */
  2552. private function _readProtect()
  2553. {
  2554. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2555. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2556. // move stream pointer to next record
  2557. $this->_pos += 4 + $length;
  2558. if ($this->_readDataOnly) {
  2559. return;
  2560. }
  2561. // offset: 0; size: 2;
  2562. // bit 0, mask 0x01; 1 = sheet is protected
  2563. $bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
  2564. $this->_phpSheet->getProtection()->setSheet((bool)$bool);
  2565. }
  2566. /**
  2567. * SCENPROTECT
  2568. */
  2569. private function _readScenProtect()
  2570. {
  2571. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2572. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2573. // move stream pointer to next record
  2574. $this->_pos += 4 + $length;
  2575. if ($this->_readDataOnly) {
  2576. return;
  2577. }
  2578. // offset: 0; size: 2;
  2579. // bit: 0, mask 0x01; 1 = scenarios are protected
  2580. $bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
  2581. $this->_phpSheet->getProtection()->setScenarios((bool)$bool);
  2582. }
  2583. /**
  2584. * OBJECTPROTECT
  2585. */
  2586. private function _readObjectProtect()
  2587. {
  2588. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2589. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2590. // move stream pointer to next record
  2591. $this->_pos += 4 + $length;
  2592. if ($this->_readDataOnly) {
  2593. return;
  2594. }
  2595. // offset: 0; size: 2;
  2596. // bit: 0, mask 0x01; 1 = objects are protected
  2597. $bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
  2598. $this->_phpSheet->getProtection()->setObjects((bool)$bool);
  2599. }
  2600. /**
  2601. * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
  2602. */
  2603. private function _readPassword()
  2604. {
  2605. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2606. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2607. // move stream pointer to next record
  2608. $this->_pos += 4 + $length;
  2609. if (!$this->_readDataOnly) {
  2610. // offset: 0; size: 2; 16-bit hash value of password
  2611. $password = strtoupper(dechex(self::_GetInt2d($recordData, 0))); // the hashed password
  2612. $this->_phpSheet->getProtection()->setPassword($password, true);
  2613. }
  2614. }
  2615. /**
  2616. * Read DEFCOLWIDTH record
  2617. */
  2618. private function _readDefColWidth()
  2619. {
  2620. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2621. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2622. // move stream pointer to next record
  2623. $this->_pos += 4 + $length;
  2624. // offset: 0; size: 2; default column width
  2625. $width = self::_GetInt2d($recordData, 0);
  2626. if ($width != 8) {
  2627. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width);
  2628. }
  2629. }
  2630. /**
  2631. * Read COLINFO record
  2632. */
  2633. private function _readColInfo()
  2634. {
  2635. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2636. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2637. // move stream pointer to next record
  2638. $this->_pos += 4 + $length;
  2639. if (!$this->_readDataOnly) {
  2640. // offset: 0; size: 2; index to first column in range
  2641. $fc = self::_GetInt2d($recordData, 0); // first column index
  2642. // offset: 2; size: 2; index to last column in range
  2643. $lc = self::_GetInt2d($recordData, 2); // first column index
  2644. // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
  2645. $width = self::_GetInt2d($recordData, 4);
  2646. // offset: 6; size: 2; index to XF record for default column formatting
  2647. $xfIndex = self::_GetInt2d($recordData, 6);
  2648. // offset: 8; size: 2; option flags
  2649. // bit: 0; mask: 0x0001; 1= columns are hidden
  2650. $isHidden = (0x0001 & self::_GetInt2d($recordData, 8)) >> 0;
  2651. // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
  2652. $level = (0x0700 & self::_GetInt2d($recordData, 8)) >> 8;
  2653. // bit: 12; mask: 0x1000; 1 = collapsed
  2654. $isCollapsed = (0x1000 & self::_GetInt2d($recordData, 8)) >> 12;
  2655. // offset: 10; size: 2; not used
  2656. for ($i = $fc; $i <= $lc; ++$i) {
  2657. if ($lc == 255 || $lc == 256) {
  2658. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width / 256);
  2659. break;
  2660. }
  2661. $this->_phpSheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
  2662. $this->_phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
  2663. $this->_phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  2664. $this->_phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
  2665. $this->_phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2666. }
  2667. }
  2668. }
  2669. /**
  2670. * ROW
  2671. *
  2672. * This record contains the properties of a single row in a
  2673. * sheet. Rows and cells in a sheet are divided into blocks
  2674. * of 32 rows.
  2675. *
  2676. * -- "OpenOffice.org's Documentation of the Microsoft
  2677. * Excel File Format"
  2678. */
  2679. private function _readRow()
  2680. {
  2681. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2682. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2683. // move stream pointer to next record
  2684. $this->_pos += 4 + $length;
  2685. if (!$this->_readDataOnly) {
  2686. // offset: 0; size: 2; index of this row
  2687. $r = self::_GetInt2d($recordData, 0);
  2688. // offset: 2; size: 2; index to column of the first cell which is described by a cell record
  2689. // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
  2690. // offset: 6; size: 2;
  2691. // bit: 14-0; mask: 0x7FFF; height of the row, in twips = 1/20 of a point
  2692. $height = (0x7FFF & self::_GetInt2d($recordData, 6)) >> 0;
  2693. // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
  2694. $useDefaultHeight = (0x8000 & self::_GetInt2d($recordData, 6)) >> 15;
  2695. if (!$useDefaultHeight) {
  2696. $this->_phpSheet->getRowDimension($r + 1)->setRowHeight($height / 20);
  2697. }
  2698. // offset: 8; size: 2; not used
  2699. // offset: 10; size: 2; not used in BIFF5-BIFF8
  2700. // offset: 12; size: 4; option flags and default row formatting
  2701. // bit: 2-0: mask: 0x00000007; outline level of the row
  2702. $level = (0x00000007 & self::_GetInt4d($recordData, 12)) >> 0;
  2703. $this->_phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
  2704. // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
  2705. $isCollapsed = (0x00000010 & self::_GetInt4d($recordData, 12)) >> 4;
  2706. $this->_phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
  2707. // bit: 5; mask: 0x00000020; 1 = row is hidden
  2708. $isHidden = (0x00000020 & self::_GetInt4d($recordData, 12)) >> 5;
  2709. $this->_phpSheet->getRowDimension($r + 1)->setVisible(!$isHidden);
  2710. // bit: 7; mask: 0x00000080; 1 = row has explicit format
  2711. $hasExplicitFormat = (0x00000080 & self::_GetInt4d($recordData, 12)) >> 7;
  2712. // bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
  2713. $xfIndex = (0x0FFF0000 & self::_GetInt4d($recordData, 12)) >> 16;
  2714. if ($hasExplicitFormat) {
  2715. $this->_phpSheet->getRowDimension($r + 1)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2716. }
  2717. }
  2718. }
  2719. /**
  2720. * Read RK record
  2721. * This record represents a cell that contains an RK value
  2722. * (encoded integer or floating-point value). If a
  2723. * floating-point value cannot be encoded to an RK value,
  2724. * a NUMBER record will be written. This record replaces the
  2725. * record INTEGER written in BIFF2.
  2726. *
  2727. * -- "OpenOffice.org's Documentation of the Microsoft
  2728. * Excel File Format"
  2729. */
  2730. private function _readRk()
  2731. {
  2732. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2733. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2734. // move stream pointer to next record
  2735. $this->_pos += 4 + $length;
  2736. // offset: 0; size: 2; index to row
  2737. $row = self::_GetInt2d($recordData, 0);
  2738. // offset: 2; size: 2; index to column
  2739. $column = self::_GetInt2d($recordData, 2);
  2740. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2741. // Read cell?
  2742. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2743. // offset: 4; size: 2; index to XF record
  2744. $xfIndex = self::_GetInt2d($recordData, 4);
  2745. // offset: 6; size: 4; RK value
  2746. $rknum = self::_GetInt4d($recordData, 6);
  2747. $numValue = self::_GetIEEE754($rknum);
  2748. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2749. if (!$this->_readDataOnly) {
  2750. // add style information
  2751. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2752. }
  2753. // add cell
  2754. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2755. }
  2756. }
  2757. /**
  2758. * Read LABELSST record
  2759. * This record represents a cell that contains a string. It
  2760. * replaces the LABEL record and RSTRING record used in
  2761. * BIFF2-BIFF5.
  2762. *
  2763. * -- "OpenOffice.org's Documentation of the Microsoft
  2764. * Excel File Format"
  2765. */
  2766. private function _readLabelSst()
  2767. {
  2768. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2769. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2770. // move stream pointer to next record
  2771. $this->_pos += 4 + $length;
  2772. // offset: 0; size: 2; index to row
  2773. $row = self::_GetInt2d($recordData, 0);
  2774. // offset: 2; size: 2; index to column
  2775. $column = self::_GetInt2d($recordData, 2);
  2776. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2777. // Read cell?
  2778. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2779. // offset: 4; size: 2; index to XF record
  2780. $xfIndex = self::_GetInt2d($recordData, 4);
  2781. // offset: 6; size: 4; index to SST record
  2782. $index = self::_GetInt4d($recordData, 6);
  2783. // add cell
  2784. if (($fmtRuns = $this->_sst[$index]['fmtRuns']) && !$this->_readDataOnly) {
  2785. // then we should treat as rich text
  2786. $richText = new PHPExcel_RichText();
  2787. $charPos = 0;
  2788. $sstCount = count($this->_sst[$index]['fmtRuns']);
  2789. for ($i = 0; $i <= $sstCount; ++$i) {
  2790. if (isset($fmtRuns[$i])) {
  2791. $text = PHPExcel_Shared_String::Substring($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos);
  2792. $charPos = $fmtRuns[$i]['charPos'];
  2793. } else {
  2794. $text = PHPExcel_Shared_String::Substring($this->_sst[$index]['value'], $charPos, PHPExcel_Shared_String::CountCharacters($this->_sst[$index]['value']));
  2795. }
  2796. if (PHPExcel_Shared_String::CountCharacters($text) > 0) {
  2797. if ($i == 0) { // first text run, no style
  2798. $richText->createText($text);
  2799. } else {
  2800. $textRun = $richText->createTextRun($text);
  2801. if (isset($fmtRuns[$i - 1])) {
  2802. if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
  2803. $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
  2804. } else {
  2805. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  2806. // check the OpenOffice documentation of the FONT record
  2807. $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
  2808. }
  2809. $textRun->setFont(clone $this->_objFonts[$fontIndex]);
  2810. }
  2811. }
  2812. }
  2813. }
  2814. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2815. $cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING);
  2816. } else {
  2817. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2818. $cell->setValueExplicit($this->_sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
  2819. }
  2820. if (!$this->_readDataOnly) {
  2821. // add style information
  2822. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2823. }
  2824. }
  2825. }
  2826. /**
  2827. * Read MULRK record
  2828. * This record represents a cell range containing RK value
  2829. * cells. All cells are located in the same row.
  2830. *
  2831. * -- "OpenOffice.org's Documentation of the Microsoft
  2832. * Excel File Format"
  2833. */
  2834. private function _readMulRk()
  2835. {
  2836. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2837. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2838. // move stream pointer to next record
  2839. $this->_pos += 4 + $length;
  2840. // offset: 0; size: 2; index to row
  2841. $row = self::_GetInt2d($recordData, 0);
  2842. // offset: 2; size: 2; index to first column
  2843. $colFirst = self::_GetInt2d($recordData, 2);
  2844. // offset: var; size: 2; index to last column
  2845. $colLast = self::_GetInt2d($recordData, $length - 2);
  2846. $columns = $colLast - $colFirst + 1;
  2847. // offset within record data
  2848. $offset = 4;
  2849. for ($i = 0; $i < $columns; ++$i) {
  2850. $columnString = PHPExcel_Cell::stringFromColumnIndex($colFirst + $i);
  2851. // Read cell?
  2852. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2853. // offset: var; size: 2; index to XF record
  2854. $xfIndex = self::_GetInt2d($recordData, $offset);
  2855. // offset: var; size: 4; RK value
  2856. $numValue = self::_GetIEEE754(self::_GetInt4d($recordData, $offset + 2));
  2857. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2858. if (!$this->_readDataOnly) {
  2859. // add style
  2860. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2861. }
  2862. // add cell value
  2863. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2864. }
  2865. $offset += 6;
  2866. }
  2867. }
  2868. /**
  2869. * Read NUMBER record
  2870. * This record represents a cell that contains a
  2871. * floating-point value.
  2872. *
  2873. * -- "OpenOffice.org's Documentation of the Microsoft
  2874. * Excel File Format"
  2875. */
  2876. private function _readNumber()
  2877. {
  2878. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2879. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2880. // move stream pointer to next record
  2881. $this->_pos += 4 + $length;
  2882. // offset: 0; size: 2; index to row
  2883. $row = self::_GetInt2d($recordData, 0);
  2884. // offset: 2; size 2; index to column
  2885. $column = self::_GetInt2d($recordData, 2);
  2886. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2887. // Read cell?
  2888. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2889. // offset 4; size: 2; index to XF record
  2890. $xfIndex = self::_GetInt2d($recordData, 4);
  2891. $numValue = self::_extractNumber(substr($recordData, 6, 8));
  2892. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2893. if (!$this->_readDataOnly) {
  2894. // add cell style
  2895. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2896. }
  2897. // add cell value
  2898. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2899. }
  2900. }
  2901. /**
  2902. * Read FORMULA record + perhaps a following STRING record if formula result is a string
  2903. * This record contains the token array and the result of a
  2904. * formula cell.
  2905. *
  2906. * -- "OpenOffice.org's Documentation of the Microsoft
  2907. * Excel File Format"
  2908. */
  2909. private function _readFormula()
  2910. {
  2911. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2912. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2913. // move stream pointer to next record
  2914. $this->_pos += 4 + $length;
  2915. // offset: 0; size: 2; row index
  2916. $row = self::_GetInt2d($recordData, 0);
  2917. // offset: 2; size: 2; col index
  2918. $column = self::_GetInt2d($recordData, 2);
  2919. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2920. // offset: 20: size: variable; formula structure
  2921. $formulaStructure = substr($recordData, 20);
  2922. // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
  2923. $options = self::_GetInt2d($recordData, 14);
  2924. // bit: 0; mask: 0x0001; 1 = recalculate always
  2925. // bit: 1; mask: 0x0002; 1 = calculate on open
  2926. // bit: 2; mask: 0x0008; 1 = part of a shared formula
  2927. $isPartOfSharedFormula = (bool) (0x0008 & $options);
  2928. // WARNING:
  2929. // We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
  2930. // the formula data may be ordinary formula data, therefore we need to check
  2931. // explicitly for the tExp token (0x01)
  2932. $isPartOfSharedFormula = $isPartOfSharedFormula && ord($formulaStructure{2}) == 0x01;
  2933. if ($isPartOfSharedFormula) {
  2934. // part of shared formula which means there will be a formula with a tExp token and nothing else
  2935. // get the base cell, grab tExp token
  2936. $baseRow = self::_GetInt2d($formulaStructure, 3);
  2937. $baseCol = self::_GetInt2d($formulaStructure, 5);
  2938. $this->_baseCell = PHPExcel_Cell::stringFromColumnIndex($baseCol). ($baseRow + 1);
  2939. }
  2940. // Read cell?
  2941. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2942. if ($isPartOfSharedFormula) {
  2943. // formula is added to this cell after the sheet has been read
  2944. $this->_sharedFormulaParts[$columnString . ($row + 1)] = $this->_baseCell;
  2945. }
  2946. // offset: 16: size: 4; not used
  2947. // offset: 4; size: 2; XF index
  2948. $xfIndex = self::_GetInt2d($recordData, 4);
  2949. // offset: 6; size: 8; result of the formula
  2950. if ( (ord($recordData{6}) == 0)
  2951. && (ord($recordData{12}) == 255)
  2952. && (ord($recordData{13}) == 255) ) {
  2953. // String formula. Result follows in appended STRING record
  2954. $dataType = PHPExcel_Cell_DataType::TYPE_STRING;
  2955. // read possible SHAREDFMLA record
  2956. $code = self::_GetInt2d($this->_data, $this->_pos);
  2957. if ($code == self::XLS_Type_SHAREDFMLA) {
  2958. $this->_readSharedFmla();
  2959. }
  2960. // read STRING record
  2961. $value = $this->_readString();
  2962. } elseif ((ord($recordData{6}) == 1)
  2963. && (ord($recordData{12}) == 255)
  2964. && (ord($recordData{13}) == 255)) {
  2965. // Boolean formula. Result is in +2; 0=false, 1=true
  2966. $dataType = PHPExcel_Cell_DataType::TYPE_BOOL;
  2967. $value = (bool) ord($recordData{8});
  2968. } elseif ((ord($recordData{6}) == 2)
  2969. && (ord($recordData{12}) == 255)
  2970. && (ord($recordData{13}) == 255)) {
  2971. // Error formula. Error code is in +2
  2972. $dataType = PHPExcel_Cell_DataType::TYPE_ERROR;
  2973. $value = self::_mapErrorCode(ord($recordData{8}));
  2974. } elseif ((ord($recordData{6}) == 3)
  2975. && (ord($recordData{12}) == 255)
  2976. && (ord($recordData{13}) == 255)) {
  2977. // Formula result is a null string
  2978. $dataType = PHPExcel_Cell_DataType::TYPE_NULL;
  2979. $value = '';
  2980. } else {
  2981. // forumla result is a number, first 14 bytes like _NUMBER record
  2982. $dataType = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  2983. $value = self::_extractNumber(substr($recordData, 6, 8));
  2984. }
  2985. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2986. if (!$this->_readDataOnly) {
  2987. // add cell style
  2988. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2989. }
  2990. // store the formula
  2991. if (!$isPartOfSharedFormula) {
  2992. // not part of shared formula
  2993. // add cell value. If we can read formula, populate with formula, otherwise just used cached value
  2994. try {
  2995. if ($this->_version != self::XLS_BIFF8) {
  2996. throw new Exception('Not BIFF8. Can only read BIFF8 formulas');
  2997. }
  2998. $formula = $this->_getFormulaFromStructure($formulaStructure); // get formula in human language
  2999. $cell->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  3000. } catch (Exception $e) {
  3001. $cell->setValueExplicit($value, $dataType);
  3002. }
  3003. } else {
  3004. if ($this->_version == self::XLS_BIFF8) {
  3005. // do nothing at this point, formula id added later in the code
  3006. } else {
  3007. $cell->setValueExplicit($value, $dataType);
  3008. }
  3009. }
  3010. // store the cached calculated value
  3011. $cell->setCalculatedValue($value);
  3012. }
  3013. }
  3014. /**
  3015. * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
  3016. * which usually contains relative references.
  3017. * These will be used to construct the formula in each shared formula part after the sheet is read.
  3018. */
  3019. private function _readSharedFmla()
  3020. {
  3021. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3022. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3023. // move stream pointer to next record
  3024. $this->_pos += 4 + $length;
  3025. // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
  3026. $cellRange = substr($recordData, 0, 6);
  3027. $cellRange = $this->_readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax
  3028. // offset: 6, size: 1; not used
  3029. // offset: 7, size: 1; number of existing FORMULA records for this shared formula
  3030. $no = ord($recordData{7});
  3031. // offset: 8, size: var; Binary token array of the shared formula
  3032. $formula = substr($recordData, 8);
  3033. // at this point we only store the shared formula for later use
  3034. $this->_sharedFormulas[$this->_baseCell] = $formula;
  3035. }
  3036. /**
  3037. * Read a STRING record from current stream position and advance the stream pointer to next record
  3038. * This record is used for storing result from FORMULA record when it is a string, and
  3039. * it occurs directly after the FORMULA record
  3040. *
  3041. * @return string The string contents as UTF-8
  3042. */
  3043. private function _readString()
  3044. {
  3045. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3046. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3047. // move stream pointer to next record
  3048. $this->_pos += 4 + $length;
  3049. if ($this->_version == self::XLS_BIFF8) {
  3050. $string = self::_readUnicodeStringLong($recordData);
  3051. $value = $string['value'];
  3052. } else {
  3053. $string = $this->_readByteStringLong($recordData);
  3054. $value = $string['value'];
  3055. }
  3056. return $value;
  3057. }
  3058. /**
  3059. * Read BOOLERR record
  3060. * This record represents a Boolean value or error value
  3061. * cell.
  3062. *
  3063. * -- "OpenOffice.org's Documentation of the Microsoft
  3064. * Excel File Format"
  3065. */
  3066. private function _readBoolErr()
  3067. {
  3068. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3069. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3070. // move stream pointer to next record
  3071. $this->_pos += 4 + $length;
  3072. // offset: 0; size: 2; row index
  3073. $row = self::_GetInt2d($recordData, 0);
  3074. // offset: 2; size: 2; column index
  3075. $column = self::_GetInt2d($recordData, 2);
  3076. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  3077. // Read cell?
  3078. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3079. // offset: 4; size: 2; index to XF record
  3080. $xfIndex = self::_GetInt2d($recordData, 4);
  3081. // offset: 6; size: 1; the boolean value or error value
  3082. $boolErr = ord($recordData{6});
  3083. // offset: 7; size: 1; 0=boolean; 1=error
  3084. $isError = ord($recordData{7});
  3085. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3086. switch ($isError) {
  3087. case 0: // boolean
  3088. $value = (bool) $boolErr;
  3089. // add cell value
  3090. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_BOOL);
  3091. break;
  3092. case 1: // error type
  3093. $value = self::_mapErrorCode($boolErr);
  3094. // add cell value
  3095. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_ERROR);
  3096. break;
  3097. }
  3098. if (!$this->_readDataOnly) {
  3099. // add cell style
  3100. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3101. }
  3102. }
  3103. }
  3104. /**
  3105. * Read MULBLANK record
  3106. * This record represents a cell range of empty cells. All
  3107. * cells are located in the same row
  3108. *
  3109. * -- "OpenOffice.org's Documentation of the Microsoft
  3110. * Excel File Format"
  3111. */
  3112. private function _readMulBlank()
  3113. {
  3114. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3115. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3116. // move stream pointer to next record
  3117. $this->_pos += 4 + $length;
  3118. // offset: 0; size: 2; index to row
  3119. $row = self::_GetInt2d($recordData, 0);
  3120. // offset: 2; size: 2; index to first column
  3121. $fc = self::_GetInt2d($recordData, 2);
  3122. // offset: 4; size: 2 x nc; list of indexes to XF records
  3123. // add style information
  3124. if (!$this->_readDataOnly) {
  3125. for ($i = 0; $i < $length / 2 - 3; ++$i) {
  3126. $columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i);
  3127. // Read cell?
  3128. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3129. $xfIndex = self::_GetInt2d($recordData, 4 + 2 * $i);
  3130. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3131. }
  3132. }
  3133. }
  3134. // offset: 6; size 2; index to last column (not needed)
  3135. }
  3136. /**
  3137. * Read LABEL record
  3138. * This record represents a cell that contains a string. In
  3139. * BIFF8 it is usually replaced by the LABELSST record.
  3140. * Excel still uses this record, if it copies unformatted
  3141. * text cells to the clipboard.
  3142. *
  3143. * -- "OpenOffice.org's Documentation of the Microsoft
  3144. * Excel File Format"
  3145. */
  3146. private function _readLabel()
  3147. {
  3148. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3149. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3150. // move stream pointer to next record
  3151. $this->_pos += 4 + $length;
  3152. // offset: 0; size: 2; index to row
  3153. $row = self::_GetInt2d($recordData, 0);
  3154. // offset: 2; size: 2; index to column
  3155. $column = self::_GetInt2d($recordData, 2);
  3156. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  3157. // Read cell?
  3158. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3159. // offset: 4; size: 2; XF index
  3160. $xfIndex = self::_GetInt2d($recordData, 4);
  3161. // add cell value
  3162. // todo: what if string is very long? continue record
  3163. if ($this->_version == self::XLS_BIFF8) {
  3164. $string = self::_readUnicodeStringLong(substr($recordData, 6));
  3165. $value = $string['value'];
  3166. } else {
  3167. $string = $this->_readByteStringLong(substr($recordData, 6));
  3168. $value = $string['value'];
  3169. }
  3170. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3171. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
  3172. if (!$this->_readDataOnly) {
  3173. // add cell style
  3174. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3175. }
  3176. }
  3177. }
  3178. /**
  3179. * Read BLANK record
  3180. */
  3181. private function _readBlank()
  3182. {
  3183. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3184. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3185. // move stream pointer to next record
  3186. $this->_pos += 4 + $length;
  3187. // offset: 0; size: 2; row index
  3188. $row = self::_GetInt2d($recordData, 0);
  3189. // offset: 2; size: 2; col index
  3190. $col = self::_GetInt2d($recordData, 2);
  3191. $columnString = PHPExcel_Cell::stringFromColumnIndex($col);
  3192. // Read cell?
  3193. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3194. // offset: 4; size: 2; XF index
  3195. $xfIndex = self::_GetInt2d($recordData, 4);
  3196. // add style information
  3197. if (!$this->_readDataOnly) {
  3198. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3199. }
  3200. }
  3201. }
  3202. /**
  3203. * Read MSODRAWING record
  3204. */
  3205. private function _readMsoDrawing()
  3206. {
  3207. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3208. // get spliced record data
  3209. $splicedRecordData = $this->_getSplicedRecordData();
  3210. $recordData = $splicedRecordData['recordData'];
  3211. $this->_drawingData .= $recordData;
  3212. }
  3213. /**
  3214. * Read OBJ record
  3215. */
  3216. private function _readObj()
  3217. {
  3218. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3219. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3220. // move stream pointer to next record
  3221. $this->_pos += 4 + $length;
  3222. if ($this->_readDataOnly || $this->_version != self::XLS_BIFF8) {
  3223. return;
  3224. }
  3225. // recordData consists of an array of subrecords looking like this:
  3226. // ft: 2 bytes; ftCmo type (0x15)
  3227. // cb: 2 bytes; size in bytes of ftCmo data
  3228. // ot: 2 bytes; Object Type
  3229. // id: 2 bytes; Object id number
  3230. // grbit: 2 bytes; Option Flags
  3231. // data: var; subrecord data
  3232. // for now, we are just interested in the second subrecord containing the object type
  3233. $ftCmoType = self::_GetInt2d($recordData, 0);
  3234. $cbCmoSize = self::_GetInt2d($recordData, 2);
  3235. $otObjType = self::_GetInt2d($recordData, 4);
  3236. $idObjID = self::_GetInt2d($recordData, 6);
  3237. $grbitOpts = self::_GetInt2d($recordData, 6);
  3238. $this->_objs[] = array(
  3239. 'ftCmoType' => $ftCmoType,
  3240. 'cbCmoSize' => $cbCmoSize,
  3241. 'otObjType' => $otObjType,
  3242. 'idObjID' => $idObjID,
  3243. 'grbitOpts' => $grbitOpts
  3244. );
  3245. $this->textObjRef = $idObjID;
  3246. // echo '<b>_readObj()</b><br />';
  3247. // var_dump(end($this->_objs));
  3248. // echo '<br />';
  3249. }
  3250. /**
  3251. * Read WINDOW2 record
  3252. */
  3253. private function _readWindow2()
  3254. {
  3255. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3256. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3257. // move stream pointer to next record
  3258. $this->_pos += 4 + $length;
  3259. // offset: 0; size: 2; option flags
  3260. $options = self::_GetInt2d($recordData, 0);
  3261. // bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
  3262. $showGridlines = (bool) ((0x0002 & $options) >> 1);
  3263. $this->_phpSheet->setShowGridlines($showGridlines);
  3264. // bit: 2; mask: 0x0004; 0 = do not show headers, 1 = show headers
  3265. $showRowColHeaders = (bool) ((0x0004 & $options) >> 2);
  3266. $this->_phpSheet->setShowRowColHeaders($showRowColHeaders);
  3267. // bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
  3268. $this->_frozen = (bool) ((0x0008 & $options) >> 3);
  3269. // bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
  3270. $this->_phpSheet->setRightToLeft((bool)((0x0040 & $options) >> 6));
  3271. // bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
  3272. $isActive = (bool) ((0x0400 & $options) >> 10);
  3273. if ($isActive) {
  3274. $this->_phpExcel->setActiveSheetIndex($this->_phpExcel->getIndex($this->_phpSheet));
  3275. }
  3276. }
  3277. /**
  3278. * Read SCL record
  3279. */
  3280. private function _readScl()
  3281. {
  3282. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3283. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3284. // move stream pointer to next record
  3285. $this->_pos += 4 + $length;
  3286. // offset: 0; size: 2; numerator of the view magnification
  3287. $numerator = self::_GetInt2d($recordData, 0);
  3288. // offset: 2; size: 2; numerator of the view magnification
  3289. $denumerator = self::_GetInt2d($recordData, 2);
  3290. // set the zoom scale (in percent)
  3291. $this->_phpSheet->getSheetView()->setZoomScale($numerator * 100 / $denumerator);
  3292. }
  3293. /**
  3294. * Read PANE record
  3295. */
  3296. private function _readPane()
  3297. {
  3298. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3299. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3300. // move stream pointer to next record
  3301. $this->_pos += 4 + $length;
  3302. if (!$this->_readDataOnly) {
  3303. // offset: 0; size: 2; position of vertical split
  3304. $px = self::_GetInt2d($recordData, 0);
  3305. // offset: 2; size: 2; position of horizontal split
  3306. $py = self::_GetInt2d($recordData, 2);
  3307. if ($this->_frozen) {
  3308. // frozen panes
  3309. $this->_phpSheet->freezePane(PHPExcel_Cell::stringFromColumnIndex($px) . ($py + 1));
  3310. } else {
  3311. // unfrozen panes; split windows; not supported by PHPExcel core
  3312. }
  3313. }
  3314. }
  3315. /**
  3316. * Read SELECTION record. There is one such record for each pane in the sheet.
  3317. */
  3318. private function _readSelection()
  3319. {
  3320. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3321. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3322. // move stream pointer to next record
  3323. $this->_pos += 4 + $length;
  3324. if (!$this->_readDataOnly) {
  3325. // offset: 0; size: 1; pane identifier
  3326. $paneId = ord($recordData{0});
  3327. // offset: 1; size: 2; index to row of the active cell
  3328. $r = self::_GetInt2d($recordData, 1);
  3329. // offset: 3; size: 2; index to column of the active cell
  3330. $c = self::_GetInt2d($recordData, 3);
  3331. // offset: 5; size: 2; index into the following cell range list to the
  3332. // entry that contains the active cell
  3333. $index = self::_GetInt2d($recordData, 5);
  3334. // offset: 7; size: var; cell range address list containing all selected cell ranges
  3335. $data = substr($recordData, 7);
  3336. $cellRangeAddressList = $this->_readBIFF5CellRangeAddressList($data); // note: also BIFF8 uses BIFF5 syntax
  3337. $selectedCells = $cellRangeAddressList['cellRangeAddresses'][0];
  3338. // first row '1' + last row '16384' indicates that full column is selected (apparently also in BIFF8!)
  3339. if (preg_match('/^([A-Z]+1\:[A-Z]+)16384$/', $selectedCells)) {
  3340. $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)16384$/', '${1}1048576', $selectedCells);
  3341. }
  3342. // first row '1' + last row '65536' indicates that full column is selected
  3343. if (preg_match('/^([A-Z]+1\:[A-Z]+)65536$/', $selectedCells)) {
  3344. $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)65536$/', '${1}1048576', $selectedCells);
  3345. }
  3346. // first column 'A' + last column 'IV' indicates that full row is selected
  3347. if (preg_match('/^(A[0-9]+\:)IV([0-9]+)$/', $selectedCells)) {
  3348. $selectedCells = preg_replace('/^(A[0-9]+\:)IV([0-9]+)$/', '${1}XFD${2}', $selectedCells);
  3349. }
  3350. $this->_phpSheet->setSelectedCells($selectedCells);
  3351. }
  3352. }
  3353. private function _includeCellRangeFiltered($cellRangeAddress)
  3354. {
  3355. $includeCellRange = true;
  3356. if (!is_null($this->getReadFilter())) {
  3357. $includeCellRange = false;
  3358. $rangeBoundaries = PHPExcel_Cell::getRangeBoundaries($cellRangeAddress);
  3359. $rangeBoundaries[1][0]++;
  3360. for ($row = $rangeBoundaries[0][1]; $row <= $rangeBoundaries[1][1]; $row++) {
  3361. for ($column = $rangeBoundaries[0][0]; $column != $rangeBoundaries[1][0]; $column++) {
  3362. if ($this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle())) {
  3363. $includeCellRange = true;
  3364. break 2;
  3365. }
  3366. }
  3367. }
  3368. }
  3369. return $includeCellRange;
  3370. }
  3371. /**
  3372. * MERGEDCELLS
  3373. *
  3374. * This record contains the addresses of merged cell ranges
  3375. * in the current sheet.
  3376. *
  3377. * -- "OpenOffice.org's Documentation of the Microsoft
  3378. * Excel File Format"
  3379. */
  3380. private function _readMergedCells()
  3381. {
  3382. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3383. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3384. // move stream pointer to next record
  3385. $this->_pos += 4 + $length;
  3386. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  3387. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData);
  3388. foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) {
  3389. if ($this->_includeCellRangeFiltered($cellRangeAddress)) {
  3390. $this->_phpSheet->mergeCells($cellRangeAddress);
  3391. }
  3392. }
  3393. }
  3394. }
  3395. /**
  3396. * Read HYPERLINK record
  3397. */
  3398. private function _readHyperLink()
  3399. {
  3400. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3401. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3402. // move stream pointer forward to next record
  3403. $this->_pos += 4 + $length;
  3404. if (!$this->_readDataOnly) {
  3405. // offset: 0; size: 8; cell range address of all cells containing this hyperlink
  3406. try {
  3407. $cellRange = $this->_readBIFF8CellRangeAddressFixed($recordData, 0, 8);
  3408. } catch (Exception $e) {
  3409. return;
  3410. }
  3411. // offset: 8, size: 16; GUID of StdLink
  3412. // offset: 24, size: 4; unknown value
  3413. // offset: 28, size: 4; option flags
  3414. // bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
  3415. $isFileLinkOrUrl = (0x00000001 & self::_GetInt2d($recordData, 28)) >> 0;
  3416. // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
  3417. $isAbsPathOrUrl = (0x00000001 & self::_GetInt2d($recordData, 28)) >> 1;
  3418. // bit: 2 (and 4); mask: 0x00000014; 0 = no description
  3419. $hasDesc = (0x00000014 & self::_GetInt2d($recordData, 28)) >> 2;
  3420. // bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
  3421. $hasText = (0x00000008 & self::_GetInt2d($recordData, 28)) >> 3;
  3422. // bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
  3423. $hasFrame = (0x00000080 & self::_GetInt2d($recordData, 28)) >> 7;
  3424. // bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
  3425. $isUNC = (0x00000100 & self::_GetInt2d($recordData, 28)) >> 8;
  3426. // offset within record data
  3427. $offset = 32;
  3428. if ($hasDesc) {
  3429. // offset: 32; size: var; character count of description text
  3430. $dl = self::_GetInt4d($recordData, 32);
  3431. // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
  3432. $desc = self::_encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false);
  3433. $offset += 4 + 2 * $dl;
  3434. }
  3435. if ($hasFrame) {
  3436. $fl = self::_GetInt4d($recordData, $offset);
  3437. $offset += 4 + 2 * $fl;
  3438. }
  3439. // detect type of hyperlink (there are 4 types)
  3440. $hyperlinkType = null;
  3441. if ($isUNC) {
  3442. $hyperlinkType = 'UNC';
  3443. } else if (!$isFileLinkOrUrl) {
  3444. $hyperlinkType = 'workbook';
  3445. } else if (ord($recordData{$offset}) == 0x03) {
  3446. $hyperlinkType = 'local';
  3447. } else if (ord($recordData{$offset}) == 0xE0) {
  3448. $hyperlinkType = 'URL';
  3449. }
  3450. switch ($hyperlinkType) {
  3451. case 'URL':
  3452. // section 5.58.2: Hyperlink containing a URL
  3453. // e.g. http://example.org/index.php
  3454. // offset: var; size: 16; GUID of URL Moniker
  3455. $offset += 16;
  3456. // offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
  3457. $us = self::_GetInt4d($recordData, $offset);
  3458. $offset += 4;
  3459. // offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
  3460. $url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
  3461. $url .= $hasText ? '#' : '';
  3462. $offset += $us;
  3463. break;
  3464. case 'local':
  3465. // section 5.58.3: Hyperlink to local file
  3466. // examples:
  3467. // mydoc.txt
  3468. // ../../somedoc.xls#Sheet!A1
  3469. // offset: var; size: 16; GUI of File Moniker
  3470. $offset += 16;
  3471. // offset: var; size: 2; directory up-level count.
  3472. $upLevelCount = self::_GetInt2d($recordData, $offset);
  3473. $offset += 2;
  3474. // offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
  3475. $sl = self::_GetInt4d($recordData, $offset);
  3476. $offset += 4;
  3477. // offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
  3478. $shortenedFilePath = substr($recordData, $offset, $sl);
  3479. $shortenedFilePath = self::_encodeUTF16($shortenedFilePath, true);
  3480. $shortenedFilePath = substr($shortenedFilePath, 0, -1); // remove trailing zero
  3481. $offset += $sl;
  3482. // offset: var; size: 24; unknown sequence
  3483. $offset += 24;
  3484. // extended file path
  3485. // offset: var; size: 4; size of the following file link field including string lenth mark
  3486. $sz = self::_GetInt4d($recordData, $offset);
  3487. $offset += 4;
  3488. // only present if $sz > 0
  3489. if ($sz > 0) {
  3490. // offset: var; size: 4; size of the character array of the extended file path and name
  3491. $xl = self::_GetInt4d($recordData, $offset);
  3492. $offset += 4;
  3493. // offset: var; size 2; unknown
  3494. $offset += 2;
  3495. // offset: var; size $xl; character array of the extended file path and name.
  3496. $extendedFilePath = substr($recordData, $offset, $xl);
  3497. $extendedFilePath = self::_encodeUTF16($extendedFilePath, false);
  3498. $offset += $xl;
  3499. }
  3500. // construct the path
  3501. $url = str_repeat('..\\', $upLevelCount);
  3502. $url .= ($sz > 0) ?
  3503. $extendedFilePath : $shortenedFilePath; // use extended path if available
  3504. $url .= $hasText ? '#' : '';
  3505. break;
  3506. case 'UNC':
  3507. // section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
  3508. // todo: implement
  3509. return;
  3510. case 'workbook':
  3511. // section 5.58.5: Hyperlink to the Current Workbook
  3512. // e.g. Sheet2!B1:C2, stored in text mark field
  3513. $url = 'sheet://';
  3514. break;
  3515. default:
  3516. return;
  3517. }
  3518. if ($hasText) {
  3519. // offset: var; size: 4; character count of text mark including trailing zero word
  3520. $tl = self::_GetInt4d($recordData, $offset);
  3521. $offset += 4;
  3522. // offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
  3523. $text = self::_encodeUTF16(substr($recordData, $offset, 2 * ($tl - 1)), false);
  3524. $url .= $text;
  3525. }
  3526. // apply the hyperlink to all the relevant cells
  3527. foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cellRange) as $coordinate) {
  3528. $this->_phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
  3529. }
  3530. }
  3531. }
  3532. /**
  3533. * Read DATAVALIDATIONS record
  3534. */
  3535. private function _readDataValidations()
  3536. {
  3537. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3538. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3539. // move stream pointer forward to next record
  3540. $this->_pos += 4 + $length;
  3541. }
  3542. /**
  3543. * Read DATAVALIDATION record
  3544. */
  3545. private function _readDataValidation()
  3546. {
  3547. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3548. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3549. // move stream pointer forward to next record
  3550. $this->_pos += 4 + $length;
  3551. if ($this->_readDataOnly) {
  3552. return;
  3553. }
  3554. // offset: 0; size: 4; Options
  3555. $options = self::_GetInt4d($recordData, 0);
  3556. // bit: 0-3; mask: 0x0000000F; type
  3557. $type = (0x0000000F & $options) >> 0;
  3558. switch ($type) {
  3559. case 0x00: $type = PHPExcel_Cell_DataValidation::TYPE_NONE; break;
  3560. case 0x01: $type = PHPExcel_Cell_DataValidation::TYPE_WHOLE; break;
  3561. case 0x02: $type = PHPExcel_Cell_DataValidation::TYPE_DECIMAL; break;
  3562. case 0x03: $type = PHPExcel_Cell_DataValidation::TYPE_LIST; break;
  3563. case 0x04: $type = PHPExcel_Cell_DataValidation::TYPE_DATE; break;
  3564. case 0x05: $type = PHPExcel_Cell_DataValidation::TYPE_TIME; break;
  3565. case 0x06: $type = PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH; break;
  3566. case 0x07: $type = PHPExcel_Cell_DataValidation::TYPE_CUSTOM; break;
  3567. }
  3568. // bit: 4-6; mask: 0x00000070; error type
  3569. $errorStyle = (0x00000070 & $options) >> 4;
  3570. switch ($errorStyle) {
  3571. case 0x00: $errorStyle = PHPExcel_Cell_DataValidation::STYLE_STOP; break;
  3572. case 0x01: $errorStyle = PHPExcel_Cell_DataValidation::STYLE_WARNING; break;
  3573. case 0x02: $errorStyle = PHPExcel_Cell_DataValidation::STYLE_INFORMATION; break;
  3574. }
  3575. // bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list)
  3576. // I have only seen cases where this is 1
  3577. $explicitFormula = (0x00000080 & $options) >> 7;
  3578. // bit: 8; mask: 0x00000100; 1= empty cells allowed
  3579. $allowBlank = (0x00000100 & $options) >> 8;
  3580. // bit: 9; mask: 0x00000200; 1= suppress drop down arrow in list type validity
  3581. $suppressDropDown = (0x00000200 & $options) >> 9;
  3582. // bit: 18; mask: 0x00040000; 1= show prompt box if cell selected
  3583. $showInputMessage = (0x00040000 & $options) >> 18;
  3584. // bit: 19; mask: 0x00080000; 1= show error box if invalid values entered
  3585. $showErrorMessage = (0x00080000 & $options) >> 19;
  3586. // bit: 20-23; mask: 0x00F00000; condition operator
  3587. $operator = (0x00F00000 & $options) >> 20;
  3588. switch ($operator) {
  3589. case 0x00: $operator = PHPExcel_Cell_DataValidation::OPERATOR_BETWEEN ; break;
  3590. case 0x01: $operator = PHPExcel_Cell_DataValidation::OPERATOR_NOTBETWEEN ; break;
  3591. case 0x02: $operator = PHPExcel_Cell_DataValidation::OPERATOR_EQUAL ; break;
  3592. case 0x03: $operator = PHPExcel_Cell_DataValidation::OPERATOR_NOTEQUAL ; break;
  3593. case 0x04: $operator = PHPExcel_Cell_DataValidation::OPERATOR_GREATERTHAN ; break;
  3594. case 0x05: $operator = PHPExcel_Cell_DataValidation::OPERATOR_LESSTHAN ; break;
  3595. case 0x06: $operator = PHPExcel_Cell_DataValidation::OPERATOR_GREATERTHANOREQUAL; break;
  3596. case 0x07: $operator = PHPExcel_Cell_DataValidation::OPERATOR_LESSTHANOREQUAL ; break;
  3597. }
  3598. // offset: 4; size: var; title of the prompt box
  3599. $offset = 4;
  3600. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3601. $promptTitle = $string['value'] !== chr(0) ?
  3602. $string['value'] : '';
  3603. $offset += $string['size'];
  3604. // offset: var; size: var; title of the error box
  3605. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3606. $errorTitle = $string['value'] !== chr(0) ?
  3607. $string['value'] : '';
  3608. $offset += $string['size'];
  3609. // offset: var; size: var; text of the prompt box
  3610. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3611. $prompt = $string['value'] !== chr(0) ?
  3612. $string['value'] : '';
  3613. $offset += $string['size'];
  3614. // offset: var; size: var; text of the error box
  3615. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3616. $error = $string['value'] !== chr(0) ?
  3617. $string['value'] : '';
  3618. $offset += $string['size'];
  3619. // offset: var; size: 2; size of the formula data for the first condition
  3620. $sz1 = self::_GetInt2d($recordData, $offset);
  3621. $offset += 2;
  3622. // offset: var; size: 2; not used
  3623. $offset += 2;
  3624. // offset: var; size: $sz1; formula data for first condition (without size field)
  3625. $formula1 = substr($recordData, $offset, $sz1);
  3626. $formula1 = pack('v', $sz1) . $formula1; // prepend the length
  3627. try {
  3628. $formula1 = $this->_getFormulaFromStructure($formula1);
  3629. // in list type validity, null characters are used as item separators
  3630. if ($type == PHPExcel_Cell_DataValidation::TYPE_LIST) {
  3631. $formula1 = str_replace(chr(0), ',', $formula1);
  3632. }
  3633. } catch (Exception $e) {
  3634. return;
  3635. }
  3636. $offset += $sz1;
  3637. // offset: var; size: 2; size of the formula data for the first condition
  3638. $sz2 = self::_GetInt2d($recordData, $offset);
  3639. $offset += 2;
  3640. // offset: var; size: 2; not used
  3641. $offset += 2;
  3642. // offset: var; size: $sz2; formula data for second condition (without size field)
  3643. $formula2 = substr($recordData, $offset, $sz2);
  3644. $formula2 = pack('v', $sz2) . $formula2; // prepend the length
  3645. try {
  3646. $formula2 = $this->_getFormulaFromStructure($formula2);
  3647. } catch (Exception $e) {
  3648. return;
  3649. }
  3650. $offset += $sz2;
  3651. // offset: var; size: var; cell range address list with
  3652. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList(substr($recordData, $offset));
  3653. $cellRangeAddresses = $cellRangeAddressList['cellRangeAddresses'];
  3654. foreach ($cellRangeAddresses as $cellRange) {
  3655. $stRange = $this->_phpSheet->shrinkRangeToFit($cellRange);
  3656. $stRange = PHPExcel_Cell::extractAllCellReferencesInRange($stRange);
  3657. foreach ($stRange as $coordinate) {
  3658. $objValidation = $this->_phpSheet->getCell($coordinate)->getDataValidation();
  3659. $objValidation->setType($type);
  3660. $objValidation->setErrorStyle($errorStyle);
  3661. $objValidation->setAllowBlank((bool)$allowBlank);
  3662. $objValidation->setShowInputMessage((bool)$showInputMessage);
  3663. $objValidation->setShowErrorMessage((bool)$showErrorMessage);
  3664. $objValidation->setShowDropDown(!$suppressDropDown);
  3665. $objValidation->setOperator($operator);
  3666. $objValidation->setErrorTitle($errorTitle);
  3667. $objValidation->setError($error);
  3668. $objValidation->setPromptTitle($promptTitle);
  3669. $objValidation->setPrompt($prompt);
  3670. $objValidation->setFormula1($formula1);
  3671. $objValidation->setFormula2($formula2);
  3672. }
  3673. }
  3674. }
  3675. /**
  3676. * Read SHEETLAYOUT record. Stores sheet tab color information.
  3677. */
  3678. private function _readSheetLayout()
  3679. {
  3680. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3681. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3682. // move stream pointer to next record
  3683. $this->_pos += 4 + $length;
  3684. // local pointer in record data
  3685. $offset = 0;
  3686. if (!$this->_readDataOnly) {
  3687. // offset: 0; size: 2; repeated record identifier 0x0862
  3688. // offset: 2; size: 10; not used
  3689. // offset: 12; size: 4; size of record data
  3690. // Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
  3691. $sz = self::_GetInt4d($recordData, 12);
  3692. switch ($sz) {
  3693. case 0x14:
  3694. // offset: 16; size: 2; color index for sheet tab
  3695. $colorIndex = self::_GetInt2d($recordData, 16);
  3696. $color = self::_readColor($colorIndex,$this->_palette,$this->_version);
  3697. $this->_phpSheet->getTabColor()->setRGB($color['rgb']);
  3698. break;
  3699. case 0x28:
  3700. // TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
  3701. return;
  3702. break;
  3703. }
  3704. }
  3705. }
  3706. /**
  3707. * Read SHEETPROTECTION record (FEATHEADR)
  3708. */
  3709. private function _readSheetProtection()
  3710. {
  3711. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3712. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3713. // move stream pointer to next record
  3714. $this->_pos += 4 + $length;
  3715. if ($this->_readDataOnly) {
  3716. return;
  3717. }
  3718. // offset: 0; size: 2; repeated record header
  3719. // offset: 2; size: 2; FRT cell reference flag (=0 currently)
  3720. // offset: 4; size: 8; Currently not used and set to 0
  3721. // offset: 12; size: 2; Shared feature type index (2=Enhanced Protetion, 4=SmartTag)
  3722. $isf = self::_GetInt2d($recordData, 12);
  3723. if ($isf != 2) {
  3724. return;
  3725. }
  3726. // offset: 14; size: 1; =1 since this is a feat header
  3727. // offset: 15; size: 4; size of rgbHdrSData
  3728. // rgbHdrSData, assume "Enhanced Protection"
  3729. // offset: 19; size: 2; option flags
  3730. $options = self::_GetInt2d($recordData, 19);
  3731. // bit: 0; mask 0x0001; 1 = user may edit objects, 0 = users must not edit objects
  3732. $bool = (0x0001 & $options) >> 0;
  3733. $this->_phpSheet->getProtection()->setObjects(!$bool);
  3734. // bit: 1; mask 0x0002; edit scenarios
  3735. $bool = (0x0002 & $options) >> 1;
  3736. $this->_phpSheet->getProtection()->setScenarios(!$bool);
  3737. // bit: 2; mask 0x0004; format cells
  3738. $bool = (0x0004 & $options) >> 2;
  3739. $this->_phpSheet->getProtection()->setFormatCells(!$bool);
  3740. // bit: 3; mask 0x0008; format columns
  3741. $bool = (0x0008 & $options) >> 3;
  3742. $this->_phpSheet->getProtection()->setFormatColumns(!$bool);
  3743. // bit: 4; mask 0x0010; format rows
  3744. $bool = (0x0010 & $options) >> 4;
  3745. $this->_phpSheet->getProtection()->setFormatRows(!$bool);
  3746. // bit: 5; mask 0x0020; insert columns
  3747. $bool = (0x0020 & $options) >> 5;
  3748. $this->_phpSheet->getProtection()->setInsertColumns(!$bool);
  3749. // bit: 6; mask 0x0040; insert rows
  3750. $bool = (0x0040 & $options) >> 6;
  3751. $this->_phpSheet->getProtection()->setInsertRows(!$bool);
  3752. // bit: 7; mask 0x0080; insert hyperlinks
  3753. $bool = (0x0080 & $options) >> 7;
  3754. $this->_phpSheet->getProtection()->setInsertHyperlinks(!$bool);
  3755. // bit: 8; mask 0x0100; delete columns
  3756. $bool = (0x0100 & $options) >> 8;
  3757. $this->_phpSheet->getProtection()->setDeleteColumns(!$bool);
  3758. // bit: 9; mask 0x0200; delete rows
  3759. $bool = (0x0200 & $options) >> 9;
  3760. $this->_phpSheet->getProtection()->setDeleteRows(!$bool);
  3761. // bit: 10; mask 0x0400; select locked cells
  3762. $bool = (0x0400 & $options) >> 10;
  3763. $this->_phpSheet->getProtection()->setSelectLockedCells(!$bool);
  3764. // bit: 11; mask 0x0800; sort cell range
  3765. $bool = (0x0800 & $options) >> 11;
  3766. $this->_phpSheet->getProtection()->setSort(!$bool);
  3767. // bit: 12; mask 0x1000; auto filter
  3768. $bool = (0x1000 & $options) >> 12;
  3769. $this->_phpSheet->getProtection()->setAutoFilter(!$bool);
  3770. // bit: 13; mask 0x2000; pivot tables
  3771. $bool = (0x2000 & $options) >> 13;
  3772. $this->_phpSheet->getProtection()->setPivotTables(!$bool);
  3773. // bit: 14; mask 0x4000; select unlocked cells
  3774. $bool = (0x4000 & $options) >> 14;
  3775. $this->_phpSheet->getProtection()->setSelectUnlockedCells(!$bool);
  3776. // offset: 21; size: 2; not used
  3777. }
  3778. /**
  3779. * Read RANGEPROTECTION record
  3780. * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
  3781. * where it is referred to as FEAT record
  3782. */
  3783. private function _readRangeProtection()
  3784. {
  3785. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3786. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3787. // move stream pointer to next record
  3788. $this->_pos += 4 + $length;
  3789. // local pointer in record data
  3790. $offset = 0;
  3791. if (!$this->_readDataOnly) {
  3792. $offset += 12;
  3793. // offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
  3794. $isf = self::_GetInt2d($recordData, 12);
  3795. if ($isf != 2) {
  3796. // we only read FEAT records of type 2
  3797. return;
  3798. }
  3799. $offset += 2;
  3800. $offset += 5;
  3801. // offset: 19; size: 2; count of ref ranges this feature is on
  3802. $cref = self::_GetInt2d($recordData, 19);
  3803. $offset += 2;
  3804. $offset += 6;
  3805. // offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
  3806. $cellRanges = array();
  3807. for ($i = 0; $i < $cref; ++$i) {
  3808. try {
  3809. $cellRange = $this->_readBIFF8CellRangeAddressFixed(substr($recordData, 27 + 8 * $i, 8));
  3810. } catch (Exception $e) {
  3811. return;
  3812. }
  3813. $cellRanges[] = $cellRange;
  3814. $offset += 8;
  3815. }
  3816. // offset: var; size: var; variable length of feature specific data
  3817. $rgbFeat = substr($recordData, $offset);
  3818. $offset += 4;
  3819. // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
  3820. $wPassword = self::_GetInt4d($recordData, $offset);
  3821. $offset += 4;
  3822. // Apply range protection to sheet
  3823. if ($cellRanges) {
  3824. $this->_phpSheet->protectCells(implode(' ', $cellRanges), strtoupper(dechex($wPassword)), true);
  3825. }
  3826. }
  3827. }
  3828. /**
  3829. * Read IMDATA record
  3830. */
  3831. private function _readImData()
  3832. {
  3833. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3834. // get spliced record data
  3835. $splicedRecordData = $this->_getSplicedRecordData();
  3836. $recordData = $splicedRecordData['recordData'];
  3837. // UNDER CONSTRUCTION
  3838. // offset: 0; size: 2; image format
  3839. $cf = self::_GetInt2d($recordData, 0);
  3840. // offset: 2; size: 2; environment from which the file was written
  3841. $env = self::_GetInt2d($recordData, 2);
  3842. // offset: 4; size: 4; length of the image data
  3843. $lcb = self::_GetInt4d($recordData, 4);
  3844. // offset: 8; size: var; image data
  3845. $iData = substr($recordData, 8);
  3846. switch ($cf) {
  3847. case 0x09: // Windows bitmap format
  3848. // BITMAPCOREINFO
  3849. // 1. BITMAPCOREHEADER
  3850. // offset: 0; size: 4; bcSize, Specifies the number of bytes required by the structure
  3851. $bcSize = self::_GetInt4d($iData, 0);
  3852. // var_dump($bcSize);
  3853. // offset: 4; size: 2; bcWidth, specifies the width of the bitmap, in pixels
  3854. $bcWidth = self::_GetInt2d($iData, 4);
  3855. // var_dump($bcWidth);
  3856. // offset: 6; size: 2; bcHeight, specifies the height of the bitmap, in pixels.
  3857. $bcHeight = self::_GetInt2d($iData, 6);
  3858. // var_dump($bcHeight);
  3859. $ih = imagecreatetruecolor($bcWidth, $bcHeight);
  3860. // offset: 8; size: 2; bcPlanes, specifies the number of planes for the target device. This value must be 1
  3861. // offset: 10; size: 2; bcBitCount specifies the number of bits-per-pixel. This value must be 1, 4, 8, or 24
  3862. $bcBitCount = self::_GetInt2d($iData, 10);
  3863. // var_dump($bcBitCount);
  3864. $rgbString = substr($iData, 12);
  3865. $rgbTriples = array();
  3866. while (strlen($rgbString) > 0) {
  3867. $rgbTriples[] = unpack('Cb/Cg/Cr', $rgbString);
  3868. $rgbString = substr($rgbString, 3);
  3869. }
  3870. $x = 0;
  3871. $y = 0;
  3872. foreach ($rgbTriples as $i => $rgbTriple) {
  3873. $color = imagecolorallocate($ih, $rgbTriple['r'], $rgbTriple['g'], $rgbTriple['b']);
  3874. imagesetpixel($ih, $x, $bcHeight - 1 - $y, $color);
  3875. $x = ($x + 1) % $bcWidth;
  3876. $y = $y + floor(($x + 1) / $bcWidth);
  3877. }
  3878. //imagepng($ih, 'image.png');
  3879. $drawing = new PHPExcel_Worksheet_Drawing();
  3880. $drawing->setPath($filename);
  3881. $drawing->setWorksheet($this->_phpSheet);
  3882. break;
  3883. case 0x02: // Windows metafile or Macintosh PICT format
  3884. case 0x0e: // native format
  3885. default;
  3886. break;
  3887. }
  3888. // _getSplicedRecordData() takes care of moving current position in data stream
  3889. }
  3890. /**
  3891. * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
  3892. * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
  3893. * In this case, we must treat the CONTINUE record as a MSODRAWING record
  3894. */
  3895. private function _readContinue()
  3896. {
  3897. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3898. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3899. // check if we are reading drawing data
  3900. // this is in case a free CONTINUE record occurs in other circumstances we are unaware of
  3901. if ($this->_drawingData == '') {
  3902. // move stream pointer to next record
  3903. $this->_pos += 4 + $length;
  3904. return;
  3905. }
  3906. // check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
  3907. if ($length < 4) {
  3908. // move stream pointer to next record
  3909. $this->_pos += 4 + $length;
  3910. return;
  3911. }
  3912. // dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
  3913. // look inside CONTINUE record to see if it looks like a part of an Escher stream
  3914. // we know that Escher stream may be split at least at
  3915. // 0xF003 MsofbtSpgrContainer
  3916. // 0xF004 MsofbtSpContainer
  3917. // 0xF00D MsofbtClientTextbox
  3918. $validSplitPoints = array(0xF003, 0xF004, 0xF00D); // add identifiers if we find more
  3919. $splitPoint = self::_GetInt2d($recordData, 2);
  3920. if (in_array($splitPoint, $validSplitPoints)) {
  3921. // get spliced record data (and move pointer to next record)
  3922. $splicedRecordData = $this->_getSplicedRecordData();
  3923. $this->_drawingData .= $splicedRecordData['recordData'];
  3924. return;
  3925. }
  3926. // move stream pointer to next record
  3927. $this->_pos += 4 + $length;
  3928. }
  3929. /**
  3930. * Reads a record from current position in data stream and continues reading data as long as CONTINUE
  3931. * records are found. Splices the record data pieces and returns the combined string as if record data
  3932. * is in one piece.
  3933. * Moves to next current position in data stream to start of next record different from a CONtINUE record
  3934. *
  3935. * @return array
  3936. */
  3937. private function _getSplicedRecordData()
  3938. {
  3939. $data = '';
  3940. $spliceOffsets = array();
  3941. $i = 0;
  3942. $spliceOffsets[0] = 0;
  3943. do {
  3944. ++$i;
  3945. // offset: 0; size: 2; identifier
  3946. $identifier = self::_GetInt2d($this->_data, $this->_pos);
  3947. // offset: 2; size: 2; length
  3948. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3949. $data .= substr($this->_data, $this->_pos + 4, $length);
  3950. $spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
  3951. $this->_pos += 4 + $length;
  3952. $nextIdentifier = self::_GetInt2d($this->_data, $this->_pos);
  3953. }
  3954. while ($nextIdentifier == self::XLS_Type_CONTINUE);
  3955. $splicedData = array(
  3956. 'recordData' => $data,
  3957. 'spliceOffsets' => $spliceOffsets,
  3958. );
  3959. return $splicedData;
  3960. }
  3961. /**
  3962. * Convert formula structure into human readable Excel formula like 'A3+A5*5'
  3963. *
  3964. * @param string $formulaStructure The complete binary data for the formula
  3965. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3966. * @return string Human readable formula
  3967. */
  3968. private function _getFormulaFromStructure($formulaStructure, $baseCell = 'A1')
  3969. {
  3970. // offset: 0; size: 2; size of the following formula data
  3971. $sz = self::_GetInt2d($formulaStructure, 0);
  3972. // offset: 2; size: sz
  3973. $formulaData = substr($formulaStructure, 2, $sz);
  3974. // for debug: dump the formula data
  3975. //echo '<xmp>';
  3976. //echo 'size: ' . $sz . "\n";
  3977. //echo 'the entire formula data: ';
  3978. //Debug::dump($formulaData);
  3979. //echo "\n----\n";
  3980. // offset: 2 + sz; size: variable (optional)
  3981. if (strlen($formulaStructure) > 2 + $sz) {
  3982. $additionalData = substr($formulaStructure, 2 + $sz);
  3983. // for debug: dump the additional data
  3984. //echo 'the entire additional data: ';
  3985. //Debug::dump($additionalData);
  3986. //echo "\n----\n";
  3987. } else {
  3988. $additionalData = '';
  3989. }
  3990. return $this->_getFormulaFromData($formulaData, $additionalData, $baseCell);
  3991. }
  3992. /**
  3993. * Take formula data and additional data for formula and return human readable formula
  3994. *
  3995. * @param string $formulaData The binary data for the formula itself
  3996. * @param string $additionalData Additional binary data going with the formula
  3997. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3998. * @return string Human readable formula
  3999. */
  4000. private function _getFormulaFromData($formulaData, $additionalData = '', $baseCell = 'A1')
  4001. {
  4002. // start parsing the formula data
  4003. $tokens = array();
  4004. while (strlen($formulaData) > 0 and $token = $this->_getNextToken($formulaData, $baseCell)) {
  4005. $tokens[] = $token;
  4006. $formulaData = substr($formulaData, $token['size']);
  4007. // for debug: dump the token
  4008. //var_dump($token);
  4009. }
  4010. $formulaString = $this->_createFormulaFromTokens($tokens, $additionalData);
  4011. return $formulaString;
  4012. }
  4013. /**
  4014. * Take array of tokens together with additional data for formula and return human readable formula
  4015. *
  4016. * @param array $tokens
  4017. * @param array $additionalData Additional binary data going with the formula
  4018. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4019. * @return string Human readable formula
  4020. */
  4021. private function _createFormulaFromTokens($tokens, $additionalData)
  4022. {
  4023. // empty formula?
  4024. if (count($tokens) == 0) {
  4025. return '';
  4026. }
  4027. $formulaStrings = array();
  4028. foreach ($tokens as $token) {
  4029. // initialize spaces
  4030. $space0 = isset($space0) ? $space0 : ''; // spaces before next token, not tParen
  4031. $space1 = isset($space1) ? $space1 : ''; // carriage returns before next token, not tParen
  4032. $space2 = isset($space2) ? $space2 : ''; // spaces before opening parenthesis
  4033. $space3 = isset($space3) ? $space3 : ''; // carriage returns before opening parenthesis
  4034. $space4 = isset($space4) ? $space4 : ''; // spaces before closing parenthesis
  4035. $space5 = isset($space5) ? $space5 : ''; // carriage returns before closing parenthesis
  4036. switch ($token['name']) {
  4037. case 'tAdd': // addition
  4038. case 'tConcat': // addition
  4039. case 'tDiv': // division
  4040. case 'tEQ': // equality
  4041. case 'tGE': // greater than or equal
  4042. case 'tGT': // greater than
  4043. case 'tIsect': // intersection
  4044. case 'tLE': // less than or equal
  4045. case 'tList': // less than or equal
  4046. case 'tLT': // less than
  4047. case 'tMul': // multiplication
  4048. case 'tNE': // multiplication
  4049. case 'tPower': // power
  4050. case 'tRange': // range
  4051. case 'tSub': // subtraction
  4052. $op2 = array_pop($formulaStrings);
  4053. $op1 = array_pop($formulaStrings);
  4054. $formulaStrings[] = "$op1$space1$space0{$token['data']}$op2";
  4055. unset($space0, $space1);
  4056. break;
  4057. case 'tUplus': // unary plus
  4058. case 'tUminus': // unary minus
  4059. $op = array_pop($formulaStrings);
  4060. $formulaStrings[] = "$space1$space0{$token['data']}$op";
  4061. unset($space0, $space1);
  4062. break;
  4063. case 'tPercent': // percent sign
  4064. $op = array_pop($formulaStrings);
  4065. $formulaStrings[] = "$op$space1$space0{$token['data']}";
  4066. unset($space0, $space1);
  4067. break;
  4068. case 'tAttrVolatile': // indicates volatile function
  4069. case 'tAttrIf':
  4070. case 'tAttrSkip':
  4071. case 'tAttrChoose':
  4072. // token is only important for Excel formula evaluator
  4073. // do nothing
  4074. break;
  4075. case 'tAttrSpace': // space / carriage return
  4076. // space will be used when next token arrives, do not alter formulaString stack
  4077. switch ($token['data']['spacetype']) {
  4078. case 'type0':
  4079. $space0 = str_repeat(' ', $token['data']['spacecount']);
  4080. break;
  4081. case 'type1':
  4082. $space1 = str_repeat("\n", $token['data']['spacecount']);
  4083. break;
  4084. case 'type2':
  4085. $space2 = str_repeat(' ', $token['data']['spacecount']);
  4086. break;
  4087. case 'type3':
  4088. $space3 = str_repeat("\n", $token['data']['spacecount']);
  4089. break;
  4090. case 'type4':
  4091. $space4 = str_repeat(' ', $token['data']['spacecount']);
  4092. break;
  4093. case 'type5':
  4094. $space5 = str_repeat("\n", $token['data']['spacecount']);
  4095. break;
  4096. }
  4097. break;
  4098. case 'tAttrSum': // SUM function with one parameter
  4099. $op = array_pop($formulaStrings);
  4100. $formulaStrings[] = "{$space1}{$space0}SUM($op)";
  4101. unset($space0, $space1);
  4102. break;
  4103. case 'tFunc': // function with fixed number of arguments
  4104. case 'tFuncV': // function with variable number of arguments
  4105. if ($token['data']['function'] != '') {
  4106. // normal function
  4107. $ops = array(); // array of operators
  4108. for ($i = 0; $i < $token['data']['args']; ++$i) {
  4109. $ops[] = array_pop($formulaStrings);
  4110. }
  4111. $ops = array_reverse($ops);
  4112. $formulaStrings[] = "$space1$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
  4113. unset($space0, $space1);
  4114. } else {
  4115. // add-in function
  4116. $ops = array(); // array of operators
  4117. for ($i = 0; $i < $token['data']['args'] - 1; ++$i) {
  4118. $ops[] = array_pop($formulaStrings);
  4119. }
  4120. $ops = array_reverse($ops);
  4121. $function = array_pop($formulaStrings);
  4122. $formulaStrings[] = "$space1$space0$function(" . implode(',', $ops) . ")";
  4123. unset($space0, $space1);
  4124. }
  4125. break;
  4126. case 'tParen': // parenthesis
  4127. $expression = array_pop($formulaStrings);
  4128. $formulaStrings[] = "$space3$space2($expression$space5$space4)";
  4129. unset($space2, $space3, $space4, $space5);
  4130. break;
  4131. case 'tArray': // array constant
  4132. $constantArray = self::_readBIFF8ConstantArray($additionalData);
  4133. $formulaStrings[] = $space1 . $space0 . $constantArray['value'];
  4134. $additionalData = substr($additionalData, $constantArray['size']); // bite of chunk of additional data
  4135. unset($space0, $space1);
  4136. break;
  4137. case 'tMemArea':
  4138. // bite off chunk of additional data
  4139. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($additionalData);
  4140. $additionalData = substr($additionalData, $cellRangeAddressList['size']);
  4141. $formulaStrings[] = "$space1$space0{$token['data']}";
  4142. unset($space0, $space1);
  4143. break;
  4144. case 'tArea': // cell range address
  4145. case 'tBool': // boolean
  4146. case 'tErr': // error code
  4147. case 'tInt': // integer
  4148. case 'tMemErr':
  4149. case 'tMemFunc':
  4150. case 'tMissArg':
  4151. case 'tName':
  4152. case 'tNameX':
  4153. case 'tNum': // number
  4154. case 'tRef': // single cell reference
  4155. case 'tRef3d': // 3d cell reference
  4156. case 'tArea3d': // 3d cell range reference
  4157. case 'tRefN':
  4158. case 'tAreaN':
  4159. case 'tStr': // string
  4160. $formulaStrings[] = "$space1$space0{$token['data']}";
  4161. unset($space0, $space1);
  4162. break;
  4163. }
  4164. }
  4165. $formulaString = $formulaStrings[0];
  4166. // for debug: dump the human readable formula
  4167. //echo '----' . "\n";
  4168. //echo 'Formula: ' . $formulaString;
  4169. return $formulaString;
  4170. }
  4171. /**
  4172. * Fetch next token from binary formula data
  4173. *
  4174. * @param string Formula data
  4175. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4176. * @return array
  4177. * @throws Exception
  4178. */
  4179. private function _getNextToken($formulaData, $baseCell = 'A1')
  4180. {
  4181. // offset: 0; size: 1; token id
  4182. $id = ord($formulaData[0]); // token id
  4183. $name = false; // initialize token name
  4184. switch ($id) {
  4185. case 0x03: $name = 'tAdd'; $size = 1; $data = '+'; break;
  4186. case 0x04: $name = 'tSub'; $size = 1; $data = '-'; break;
  4187. case 0x05: $name = 'tMul'; $size = 1; $data = '*'; break;
  4188. case 0x06: $name = 'tDiv'; $size = 1; $data = '/'; break;
  4189. case 0x07: $name = 'tPower'; $size = 1; $data = '^'; break;
  4190. case 0x08: $name = 'tConcat'; $size = 1; $data = '&'; break;
  4191. case 0x09: $name = 'tLT'; $size = 1; $data = '<'; break;
  4192. case 0x0A: $name = 'tLE'; $size = 1; $data = '<='; break;
  4193. case 0x0B: $name = 'tEQ'; $size = 1; $data = '='; break;
  4194. case 0x0C: $name = 'tGE'; $size = 1; $data = '>='; break;
  4195. case 0x0D: $name = 'tGT'; $size = 1; $data = '>'; break;
  4196. case 0x0E: $name = 'tNE'; $size = 1; $data = '<>'; break;
  4197. case 0x0F: $name = 'tIsect'; $size = 1; $data = ' '; break;
  4198. case 0x10: $name = 'tList'; $size = 1; $data = ','; break;
  4199. case 0x11: $name = 'tRange'; $size = 1; $data = ':'; break;
  4200. case 0x12: $name = 'tUplus'; $size = 1; $data = '+'; break;
  4201. case 0x13: $name = 'tUminus'; $size = 1; $data = '-'; break;
  4202. case 0x14: $name = 'tPercent'; $size = 1; $data = '%'; break;
  4203. case 0x15: // parenthesis
  4204. $name = 'tParen';
  4205. $size = 1;
  4206. $data = null;
  4207. break;
  4208. case 0x16: // missing argument
  4209. $name = 'tMissArg';
  4210. $size = 1;
  4211. $data = '';
  4212. break;
  4213. case 0x17: // string
  4214. $name = 'tStr';
  4215. // offset: 1; size: var; Unicode string, 8-bit string length
  4216. $string = self::_readUnicodeStringShort(substr($formulaData, 1));
  4217. $size = 1 + $string['size'];
  4218. $data = self::_UTF8toExcelDoubleQuoted($string['value']);
  4219. break;
  4220. case 0x19: // Special attribute
  4221. // offset: 1; size: 1; attribute type flags:
  4222. switch (ord($formulaData[1])) {
  4223. case 0x01:
  4224. $name = 'tAttrVolatile';
  4225. $size = 4;
  4226. $data = null;
  4227. break;
  4228. case 0x02:
  4229. $name = 'tAttrIf';
  4230. $size = 4;
  4231. $data = null;
  4232. break;
  4233. case 0x04:
  4234. $name = 'tAttrChoose';
  4235. // offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
  4236. $nc = self::_GetInt2d($formulaData, 2);
  4237. // offset: 4; size: 2 * $nc
  4238. // offset: 4 + 2 * $nc; size: 2
  4239. $size = 2 * $nc + 6;
  4240. $data = null;
  4241. break;
  4242. case 0x08:
  4243. $name = 'tAttrSkip';
  4244. $size = 4;
  4245. $data = null;
  4246. break;
  4247. case 0x10:
  4248. $name = 'tAttrSum';
  4249. $size = 4;
  4250. $data = null;
  4251. break;
  4252. case 0x40:
  4253. case 0x41:
  4254. $name = 'tAttrSpace';
  4255. $size = 4;
  4256. // offset: 2; size: 2; space type and position
  4257. switch (ord($formulaData[2])) {
  4258. case 0x00:
  4259. $spacetype = 'type0';
  4260. break;
  4261. case 0x01:
  4262. $spacetype = 'type1';
  4263. break;
  4264. case 0x02:
  4265. $spacetype = 'type2';
  4266. break;
  4267. case 0x03:
  4268. $spacetype = 'type3';
  4269. break;
  4270. case 0x04:
  4271. $spacetype = 'type4';
  4272. break;
  4273. case 0x05:
  4274. $spacetype = 'type5';
  4275. break;
  4276. default:
  4277. throw new Exception('Unrecognized space type in tAttrSpace token');
  4278. break;
  4279. }
  4280. // offset: 3; size: 1; number of inserted spaces/carriage returns
  4281. $spacecount = ord($formulaData[3]);
  4282. $data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
  4283. break;
  4284. default:
  4285. throw new Exception('Unrecognized attribute flag in tAttr token');
  4286. break;
  4287. }
  4288. break;
  4289. case 0x1C: // error code
  4290. // offset: 1; size: 1; error code
  4291. $name = 'tErr';
  4292. $size = 2;
  4293. $data = self::_mapErrorCode(ord($formulaData[1]));
  4294. break;
  4295. case 0x1D: // boolean
  4296. // offset: 1; size: 1; 0 = false, 1 = true;
  4297. $name = 'tBool';
  4298. $size = 2;
  4299. $data = ord($formulaData[1]) ? 'TRUE' : 'FALSE';
  4300. break;
  4301. case 0x1E: // integer
  4302. // offset: 1; size: 2; unsigned 16-bit integer
  4303. $name = 'tInt';
  4304. $size = 3;
  4305. $data = self::_GetInt2d($formulaData, 1);
  4306. break;
  4307. case 0x1F: // number
  4308. // offset: 1; size: 8;
  4309. $name = 'tNum';
  4310. $size = 9;
  4311. $data = self::_extractNumber(substr($formulaData, 1));
  4312. $data = str_replace(',', '.', (string)$data); // in case non-English locale
  4313. break;
  4314. case 0x20: // array constant
  4315. case 0x40:
  4316. case 0x60:
  4317. // offset: 1; size: 7; not used
  4318. $name = 'tArray';
  4319. $size = 8;
  4320. $data = null;
  4321. break;
  4322. case 0x21: // function with fixed number of arguments
  4323. case 0x41:
  4324. case 0x61:
  4325. $name = 'tFunc';
  4326. $size = 3;
  4327. // offset: 1; size: 2; index to built-in sheet function
  4328. switch (self::_GetInt2d($formulaData, 1)) {
  4329. case 2: $function = 'ISNA'; $args = 1; break;
  4330. case 3: $function = 'ISERROR'; $args = 1; break;
  4331. case 10: $function = 'NA'; $args = 0; break;
  4332. case 15: $function = 'SIN'; $args = 1; break;
  4333. case 16: $function = 'COS'; $args = 1; break;
  4334. case 17: $function = 'TAN'; $args = 1; break;
  4335. case 18: $function = 'ATAN'; $args = 1; break;
  4336. case 19: $function = 'PI'; $args = 0; break;
  4337. case 20: $function = 'SQRT'; $args = 1; break;
  4338. case 21: $function = 'EXP'; $args = 1; break;
  4339. case 22: $function = 'LN'; $args = 1; break;
  4340. case 23: $function = 'LOG10'; $args = 1; break;
  4341. case 24: $function = 'ABS'; $args = 1; break;
  4342. case 25: $function = 'INT'; $args = 1; break;
  4343. case 26: $function = 'SIGN'; $args = 1; break;
  4344. case 27: $function = 'ROUND'; $args = 2; break;
  4345. case 30: $function = 'REPT'; $args = 2; break;
  4346. case 31: $function = 'MID'; $args = 3; break;
  4347. case 32: $function = 'LEN'; $args = 1; break;
  4348. case 33: $function = 'VALUE'; $args = 1; break;
  4349. case 34: $function = 'TRUE'; $args = 0; break;
  4350. case 35: $function = 'FALSE'; $args = 0; break;
  4351. case 38: $function = 'NOT'; $args = 1; break;
  4352. case 39: $function = 'MOD'; $args = 2; break;
  4353. case 40: $function = 'DCOUNT'; $args = 3; break;
  4354. case 41: $function = 'DSUM'; $args = 3; break;
  4355. case 42: $function = 'DAVERAGE'; $args = 3; break;
  4356. case 43: $function = 'DMIN'; $args = 3; break;
  4357. case 44: $function = 'DMAX'; $args = 3; break;
  4358. case 45: $function = 'DSTDEV'; $args = 3; break;
  4359. case 48: $function = 'TEXT'; $args = 2; break;
  4360. case 61: $function = 'MIRR'; $args = 3; break;
  4361. case 63: $function = 'RAND'; $args = 0; break;
  4362. case 65: $function = 'DATE'; $args = 3; break;
  4363. case 66: $function = 'TIME'; $args = 3; break;
  4364. case 67: $function = 'DAY'; $args = 1; break;
  4365. case 68: $function = 'MONTH'; $args = 1; break;
  4366. case 69: $function = 'YEAR'; $args = 1; break;
  4367. case 71: $function = 'HOUR'; $args = 1; break;
  4368. case 72: $function = 'MINUTE'; $args = 1; break;
  4369. case 73: $function = 'SECOND'; $args = 1; break;
  4370. case 74: $function = 'NOW'; $args = 0; break;
  4371. case 75: $function = 'AREAS'; $args = 1; break;
  4372. case 76: $function = 'ROWS'; $args = 1; break;
  4373. case 77: $function = 'COLUMNS'; $args = 1; break;
  4374. case 83: $function = 'TRANSPOSE'; $args = 1; break;
  4375. case 86: $function = 'TYPE'; $args = 1; break;
  4376. case 97: $function = 'ATAN2'; $args = 2; break;
  4377. case 98: $function = 'ASIN'; $args = 1; break;
  4378. case 99: $function = 'ACOS'; $args = 1; break;
  4379. case 105: $function = 'ISREF'; $args = 1; break;
  4380. case 111: $function = 'CHAR'; $args = 1; break;
  4381. case 112: $function = 'LOWER'; $args = 1; break;
  4382. case 113: $function = 'UPPER'; $args = 1; break;
  4383. case 114: $function = 'PROPER'; $args = 1; break;
  4384. case 117: $function = 'EXACT'; $args = 2; break;
  4385. case 118: $function = 'TRIM'; $args = 1; break;
  4386. case 119: $function = 'REPLACE'; $args = 4; break;
  4387. case 121: $function = 'CODE'; $args = 1; break;
  4388. case 126: $function = 'ISERR'; $args = 1; break;
  4389. case 127: $function = 'ISTEXT'; $args = 1; break;
  4390. case 128: $function = 'ISNUMBER'; $args = 1; break;
  4391. case 129: $function = 'ISBLANK'; $args = 1; break;
  4392. case 130: $function = 'T'; $args = 1; break;
  4393. case 131: $function = 'N'; $args = 1; break;
  4394. case 140: $function = 'DATEVALUE'; $args = 1; break;
  4395. case 141: $function = 'TIMEVALUE'; $args = 1; break;
  4396. case 142: $function = 'SLN'; $args = 3; break;
  4397. case 143: $function = 'SYD'; $args = 4; break;
  4398. case 162: $function = 'CLEAN'; $args = 1; break;
  4399. case 163: $function = 'MDETERM'; $args = 1; break;
  4400. case 164: $function = 'MINVERSE'; $args = 1; break;
  4401. case 165: $function = 'MMULT'; $args = 2; break;
  4402. case 184: $function = 'FACT'; $args = 1; break;
  4403. case 189: $function = 'DPRODUCT'; $args = 3; break;
  4404. case 190: $function = 'ISNONTEXT'; $args = 1; break;
  4405. case 195: $function = 'DSTDEVP'; $args = 3; break;
  4406. case 196: $function = 'DVARP'; $args = 3; break;
  4407. case 198: $function = 'ISLOGICAL'; $args = 1; break;
  4408. case 199: $function = 'DCOUNTA'; $args = 3; break;
  4409. case 207: $function = 'REPLACEB'; $args = 4; break;
  4410. case 210: $function = 'MIDB'; $args = 3; break;
  4411. case 211: $function = 'LENB'; $args = 1; break;
  4412. case 212: $function = 'ROUNDUP'; $args = 2; break;
  4413. case 213: $function = 'ROUNDDOWN'; $args = 2; break;
  4414. case 214: $function = 'ASC'; $args = 1; break;
  4415. case 215: $function = 'DBCS'; $args = 1; break;
  4416. case 221: $function = 'TODAY'; $args = 0; break;
  4417. case 229: $function = 'SINH'; $args = 1; break;
  4418. case 230: $function = 'COSH'; $args = 1; break;
  4419. case 231: $function = 'TANH'; $args = 1; break;
  4420. case 232: $function = 'ASINH'; $args = 1; break;
  4421. case 233: $function = 'ACOSH'; $args = 1; break;
  4422. case 234: $function = 'ATANH'; $args = 1; break;
  4423. case 235: $function = 'DGET'; $args = 3; break;
  4424. case 244: $function = 'INFO'; $args = 1; break;
  4425. case 252: $function = 'FREQUENCY'; $args = 2; break;
  4426. case 261: $function = 'ERROR.TYPE'; $args = 1; break;
  4427. case 271: $function = 'GAMMALN'; $args = 1; break;
  4428. case 273: $function = 'BINOMDIST'; $args = 4; break;
  4429. case 274: $function = 'CHIDIST'; $args = 2; break;
  4430. case 275: $function = 'CHIINV'; $args = 2; break;
  4431. case 276: $function = 'COMBIN'; $args = 2; break;
  4432. case 277: $function = 'CONFIDENCE'; $args = 3; break;
  4433. case 278: $function = 'CRITBINOM'; $args = 3; break;
  4434. case 279: $function = 'EVEN'; $args = 1; break;
  4435. case 280: $function = 'EXPONDIST'; $args = 3; break;
  4436. case 281: $function = 'FDIST'; $args = 3; break;
  4437. case 282: $function = 'FINV'; $args = 3; break;
  4438. case 283: $function = 'FISHER'; $args = 1; break;
  4439. case 284: $function = 'FISHERINV'; $args = 1; break;
  4440. case 285: $function = 'FLOOR'; $args = 2; break;
  4441. case 286: $function = 'GAMMADIST'; $args = 4; break;
  4442. case 287: $function = 'GAMMAINV'; $args = 3; break;
  4443. case 288: $function = 'CEILING'; $args = 2; break;
  4444. case 289: $function = 'HYPGEOMDIST'; $args = 4; break;
  4445. case 290: $function = 'LOGNORMDIST'; $args = 3; break;
  4446. case 291: $function = 'LOGINV'; $args = 3; break;
  4447. case 292: $function = 'NEGBINOMDIST'; $args = 3; break;
  4448. case 293: $function = 'NORMDIST'; $args = 4; break;
  4449. case 294: $function = 'NORMSDIST'; $args = 1; break;
  4450. case 295: $function = 'NORMINV'; $args = 3; break;
  4451. case 296: $function = 'NORMSINV'; $args = 1; break;
  4452. case 297: $function = 'STANDARDIZE'; $args = 3; break;
  4453. case 298: $function = 'ODD'; $args = 1; break;
  4454. case 299: $function = 'PERMUT'; $args = 2; break;
  4455. case 300: $function = 'POISSON'; $args = 3; break;
  4456. case 301: $function = 'TDIST'; $args = 3; break;
  4457. case 302: $function = 'WEIBULL'; $args = 4; break;
  4458. case 303: $function = 'SUMXMY2'; $args = 2; break;
  4459. case 304: $function = 'SUMX2MY2'; $args = 2; break;
  4460. case 305: $function = 'SUMX2PY2'; $args = 2; break;
  4461. case 306: $function = 'CHITEST'; $args = 2; break;
  4462. case 307: $function = 'CORREL'; $args = 2; break;
  4463. case 308: $function = 'COVAR'; $args = 2; break;
  4464. case 309: $function = 'FORECAST'; $args = 3; break;
  4465. case 310: $function = 'FTEST'; $args = 2; break;
  4466. case 311: $function = 'INTERCEPT'; $args = 2; break;
  4467. case 312: $function = 'PEARSON'; $args = 2; break;
  4468. case 313: $function = 'RSQ'; $args = 2; break;
  4469. case 314: $function = 'STEYX'; $args = 2; break;
  4470. case 315: $function = 'SLOPE'; $args = 2; break;
  4471. case 316: $function = 'TTEST'; $args = 4; break;
  4472. case 325: $function = 'LARGE'; $args = 2; break;
  4473. case 326: $function = 'SMALL'; $args = 2; break;
  4474. case 327: $function = 'QUARTILE'; $args = 2; break;
  4475. case 328: $function = 'PERCENTILE'; $args = 2; break;
  4476. case 331: $function = 'TRIMMEAN'; $args = 2; break;
  4477. case 332: $function = 'TINV'; $args = 2; break;
  4478. case 337: $function = 'POWER'; $args = 2; break;
  4479. case 342: $function = 'RADIANS'; $args = 1; break;
  4480. case 343: $function = 'DEGREES'; $args = 1; break;
  4481. case 346: $function = 'COUNTIF'; $args = 2; break;
  4482. case 347: $function = 'COUNTBLANK'; $args = 1; break;
  4483. case 350: $function = 'ISPMT'; $args = 4; break;
  4484. case 351: $function = 'DATEDIF'; $args = 3; break;
  4485. case 352: $function = 'DATESTRING'; $args = 1; break;
  4486. case 353: $function = 'NUMBERSTRING'; $args = 2; break;
  4487. case 360: $function = 'PHONETIC'; $args = 1; break;
  4488. case 368: $function = 'BAHTTEXT'; $args = 1; break;
  4489. default:
  4490. throw new Exception('Unrecognized function in formula');
  4491. break;
  4492. }
  4493. $data = array('function' => $function, 'args' => $args);
  4494. break;
  4495. case 0x22: // function with variable number of arguments
  4496. case 0x42:
  4497. case 0x62:
  4498. $name = 'tFuncV';
  4499. $size = 4;
  4500. // offset: 1; size: 1; number of arguments
  4501. $args = ord($formulaData[1]);
  4502. // offset: 2: size: 2; index to built-in sheet function
  4503. $index = self::_GetInt2d($formulaData, 2);
  4504. switch ($index) {
  4505. case 0: $function = 'COUNT'; break;
  4506. case 1: $function = 'IF'; break;
  4507. case 4: $function = 'SUM'; break;
  4508. case 5: $function = 'AVERAGE'; break;
  4509. case 6: $function = 'MIN'; break;
  4510. case 7: $function = 'MAX'; break;
  4511. case 8: $function = 'ROW'; break;
  4512. case 9: $function = 'COLUMN'; break;
  4513. case 11: $function = 'NPV'; break;
  4514. case 12: $function = 'STDEV'; break;
  4515. case 13: $function = 'DOLLAR'; break;
  4516. case 14: $function = 'FIXED'; break;
  4517. case 28: $function = 'LOOKUP'; break;
  4518. case 29: $function = 'INDEX'; break;
  4519. case 36: $function = 'AND'; break;
  4520. case 37: $function = 'OR'; break;
  4521. case 46: $function = 'VAR'; break;
  4522. case 49: $function = 'LINEST'; break;
  4523. case 50: $function = 'TREND'; break;
  4524. case 51: $function = 'LOGEST'; break;
  4525. case 52: $function = 'GROWTH'; break;
  4526. case 56: $function = 'PV'; break;
  4527. case 57: $function = 'FV'; break;
  4528. case 58: $function = 'NPER'; break;
  4529. case 59: $function = 'PMT'; break;
  4530. case 60: $function = 'RATE'; break;
  4531. case 62: $function = 'IRR'; break;
  4532. case 64: $function = 'MATCH'; break;
  4533. case 70: $function = 'WEEKDAY'; break;
  4534. case 78: $function = 'OFFSET'; break;
  4535. case 82: $function = 'SEARCH'; break;
  4536. case 100: $function = 'CHOOSE'; break;
  4537. case 101: $function = 'HLOOKUP'; break;
  4538. case 102: $function = 'VLOOKUP'; break;
  4539. case 109: $function = 'LOG'; break;
  4540. case 115: $function = 'LEFT'; break;
  4541. case 116: $function = 'RIGHT'; break;
  4542. case 120: $function = 'SUBSTITUTE'; break;
  4543. case 124: $function = 'FIND'; break;
  4544. case 125: $function = 'CELL'; break;
  4545. case 144: $function = 'DDB'; break;
  4546. case 148: $function = 'INDIRECT'; break;
  4547. case 167: $function = 'IPMT'; break;
  4548. case 168: $function = 'PPMT'; break;
  4549. case 169: $function = 'COUNTA'; break;
  4550. case 183: $function = 'PRODUCT'; break;
  4551. case 193: $function = 'STDEVP'; break;
  4552. case 194: $function = 'VARP'; break;
  4553. case 197: $function = 'TRUNC'; break;
  4554. case 204: $function = 'USDOLLAR'; break;
  4555. case 205: $function = 'FINDB'; break;
  4556. case 206: $function = 'SEARCHB'; break;
  4557. case 208: $function = 'LEFTB'; break;
  4558. case 209: $function = 'RIGHTB'; break;
  4559. case 216: $function = 'RANK'; break;
  4560. case 219: $function = 'ADDRESS'; break;
  4561. case 220: $function = 'DAYS360'; break;
  4562. case 222: $function = 'VDB'; break;
  4563. case 227: $function = 'MEDIAN'; break;
  4564. case 228: $function = 'SUMPRODUCT'; break;
  4565. case 247: $function = 'DB'; break;
  4566. case 255: $function = ''; break;
  4567. case 269: $function = 'AVEDEV'; break;
  4568. case 270: $function = 'BETADIST'; break;
  4569. case 272: $function = 'BETAINV'; break;
  4570. case 317: $function = 'PROB'; break;
  4571. case 318: $function = 'DEVSQ'; break;
  4572. case 319: $function = 'GEOMEAN'; break;
  4573. case 320: $function = 'HARMEAN'; break;
  4574. case 321: $function = 'SUMSQ'; break;
  4575. case 322: $function = 'KURT'; break;
  4576. case 323: $function = 'SKEW'; break;
  4577. case 324: $function = 'ZTEST'; break;
  4578. case 329: $function = 'PERCENTRANK'; break;
  4579. case 330: $function = 'MODE'; break;
  4580. case 336: $function = 'CONCATENATE'; break;
  4581. case 344: $function = 'SUBTOTAL'; break;
  4582. case 345: $function = 'SUMIF'; break;
  4583. case 354: $function = 'ROMAN'; break;
  4584. case 358: $function = 'GETPIVOTDATA'; break;
  4585. case 359: $function = 'HYPERLINK'; break;
  4586. case 361: $function = 'AVERAGEA'; break;
  4587. case 362: $function = 'MAXA'; break;
  4588. case 363: $function = 'MINA'; break;
  4589. case 364: $function = 'STDEVPA'; break;
  4590. case 365: $function = 'VARPA'; break;
  4591. case 366: $function = 'STDEVA'; break;
  4592. case 367: $function = 'VARA'; break;
  4593. default:
  4594. throw new Exception('Unrecognized function in formula');
  4595. break;
  4596. }
  4597. $data = array('function' => $function, 'args' => $args);
  4598. break;
  4599. case 0x23: // index to defined name
  4600. case 0x43:
  4601. case 0x63:
  4602. $name = 'tName';
  4603. $size = 5;
  4604. // offset: 1; size: 2; one-based index to definedname record
  4605. $definedNameIndex = self::_GetInt2d($formulaData, 1) - 1;
  4606. // offset: 2; size: 2; not used
  4607. $data = $this->_definedname[$definedNameIndex]['name'];
  4608. break;
  4609. case 0x24: // single cell reference e.g. A5
  4610. case 0x44:
  4611. case 0x64:
  4612. $name = 'tRef';
  4613. $size = 5;
  4614. $data = $this->_readBIFF8CellAddress(substr($formulaData, 1, 4));
  4615. break;
  4616. case 0x25: // cell range reference to cells in the same sheet (2d)
  4617. case 0x45:
  4618. case 0x65:
  4619. $name = 'tArea';
  4620. $size = 9;
  4621. $data = $this->_readBIFF8CellRangeAddress(substr($formulaData, 1, 8));
  4622. break;
  4623. case 0x26: // Constant reference sub-expression
  4624. case 0x46:
  4625. case 0x66:
  4626. $name = 'tMemArea';
  4627. // offset: 1; size: 4; not used
  4628. // offset: 5; size: 2; size of the following subexpression
  4629. $subSize = self::_GetInt2d($formulaData, 5);
  4630. $size = 7 + $subSize;
  4631. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  4632. break;
  4633. case 0x27: // Deleted constant reference sub-expression
  4634. case 0x47:
  4635. case 0x67:
  4636. $name = 'tMemErr';
  4637. // offset: 1; size: 4; not used
  4638. // offset: 5; size: 2; size of the following subexpression
  4639. $subSize = self::_GetInt2d($formulaData, 5);
  4640. $size = 7 + $subSize;
  4641. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  4642. break;
  4643. case 0x29: // Variable reference sub-expression
  4644. case 0x49:
  4645. case 0x69:
  4646. $name = 'tMemFunc';
  4647. // offset: 1; size: 2; size of the following sub-expression
  4648. $subSize = self::_GetInt2d($formulaData, 1);
  4649. $size = 3 + $subSize;
  4650. $data = $this->_getFormulaFromData(substr($formulaData, 3, $subSize));
  4651. break;
  4652. case 0x2C: // Relative 2d cell reference reference, used in shared formulas and some other places
  4653. case 0x4C:
  4654. case 0x6C:
  4655. $name = 'tRefN';
  4656. $size = 5;
  4657. $data = $this->_readBIFF8CellAddressB(substr($formulaData, 1, 4), $baseCell);
  4658. break;
  4659. case 0x2D: // Relative 2d range reference
  4660. case 0x4D:
  4661. case 0x6D:
  4662. $name = 'tAreaN';
  4663. $size = 9;
  4664. $data = $this->_readBIFF8CellRangeAddressB(substr($formulaData, 1, 8), $baseCell);
  4665. break;
  4666. case 0x39: // External name
  4667. case 0x59:
  4668. case 0x79:
  4669. $name = 'tNameX';
  4670. $size = 7;
  4671. // offset: 1; size: 2; index to REF entry in EXTERNSHEET record
  4672. // offset: 3; size: 2; one-based index to DEFINEDNAME or EXTERNNAME record
  4673. $index = self::_GetInt2d($formulaData, 3);
  4674. // assume index is to EXTERNNAME record
  4675. $data = $this->_externalNames[$index - 1]['name'];
  4676. // offset: 5; size: 2; not used
  4677. break;
  4678. case 0x3A: // 3d reference to cell
  4679. case 0x5A:
  4680. case 0x7A:
  4681. $name = 'tRef3d';
  4682. $size = 7;
  4683. try {
  4684. // offset: 1; size: 2; index to REF entry
  4685. $sheetRange = $this->_readSheetRangeByRefIndex(self::_GetInt2d($formulaData, 1));
  4686. // offset: 3; size: 4; cell address
  4687. $cellAddress = $this->_readBIFF8CellAddress(substr($formulaData, 3, 4));
  4688. $data = "$sheetRange!$cellAddress";
  4689. } catch (Exception $e) {
  4690. // deleted sheet reference
  4691. $data = '#REF!';
  4692. }
  4693. break;
  4694. case 0x3B: // 3d reference to cell range
  4695. case 0x5B:
  4696. case 0x7B:
  4697. $name = 'tArea3d';
  4698. $size = 11;
  4699. try {
  4700. // offset: 1; size: 2; index to REF entry
  4701. $sheetRange = $this->_readSheetRangeByRefIndex(self::_GetInt2d($formulaData, 1));
  4702. // offset: 3; size: 8; cell address
  4703. $cellRangeAddress = $this->_readBIFF8CellRangeAddress(substr($formulaData, 3, 8));
  4704. $data = "$sheetRange!$cellRangeAddress";
  4705. } catch (Exception $e) {
  4706. // deleted sheet reference
  4707. $data = '#REF!';
  4708. }
  4709. break;
  4710. // Unknown cases // don't know how to deal with
  4711. default:
  4712. throw new Exception('Unrecognized token ' . sprintf('%02X', $id) . ' in formula');
  4713. break;
  4714. }
  4715. return array(
  4716. 'id' => $id,
  4717. 'name' => $name,
  4718. 'size' => $size,
  4719. 'data' => $data,
  4720. );
  4721. }
  4722. /**
  4723. * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
  4724. * section 3.3.4
  4725. *
  4726. * @param string $cellAddressStructure
  4727. * @return string
  4728. */
  4729. private function _readBIFF8CellAddress($cellAddressStructure)
  4730. {
  4731. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  4732. $row = self::_GetInt2d($cellAddressStructure, 0) + 1;
  4733. // offset: 2; size: 2; index to column or column offset + relative flags
  4734. // bit: 7-0; mask 0x00FF; column index
  4735. $column = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($cellAddressStructure, 2));
  4736. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4737. if (!(0x4000 & self::_GetInt2d($cellAddressStructure, 2))) {
  4738. $column = '$' . $column;
  4739. }
  4740. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4741. if (!(0x8000 & self::_GetInt2d($cellAddressStructure, 2))) {
  4742. $row = '$' . $row;
  4743. }
  4744. return $column . $row;
  4745. }
  4746. /**
  4747. * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  4748. * to indicate offsets from a base cell
  4749. * section 3.3.4
  4750. *
  4751. * @param string $cellAddressStructure
  4752. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4753. * @return string
  4754. */
  4755. private function _readBIFF8CellAddressB($cellAddressStructure, $baseCell = 'A1')
  4756. {
  4757. list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
  4758. $baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
  4759. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  4760. $rowIndex = self::_GetInt2d($cellAddressStructure, 0);
  4761. $row = self::_GetInt2d($cellAddressStructure, 0) + 1;
  4762. // offset: 2; size: 2; index to column or column offset + relative flags
  4763. // bit: 7-0; mask 0x00FF; column index
  4764. $colIndex = 0x00FF & self::_GetInt2d($cellAddressStructure, 2);
  4765. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4766. if (!(0x4000 & self::_GetInt2d($cellAddressStructure, 2))) {
  4767. $column = PHPExcel_Cell::stringFromColumnIndex($colIndex);
  4768. $column = '$' . $column;
  4769. } else {
  4770. $colIndex = ($colIndex <= 127) ? $colIndex : $colIndex - 256;
  4771. $column = PHPExcel_Cell::stringFromColumnIndex($baseCol + $colIndex);
  4772. }
  4773. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4774. if (!(0x8000 & self::_GetInt2d($cellAddressStructure, 2))) {
  4775. $row = '$' . $row;
  4776. } else {
  4777. $rowIndex = ($rowIndex <= 32767) ? $rowIndex : $rowIndex - 65536;
  4778. $row = $baseRow + $rowIndex;
  4779. }
  4780. return $column . $row;
  4781. }
  4782. /**
  4783. * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
  4784. * always fixed range
  4785. * section 2.5.14
  4786. *
  4787. * @param string $subData
  4788. * @return string
  4789. * @throws Exception
  4790. */
  4791. private function _readBIFF5CellRangeAddressFixed($subData)
  4792. {
  4793. // offset: 0; size: 2; index to first row
  4794. $fr = self::_GetInt2d($subData, 0) + 1;
  4795. // offset: 2; size: 2; index to last row
  4796. $lr = self::_GetInt2d($subData, 2) + 1;
  4797. // offset: 4; size: 1; index to first column
  4798. $fc = ord($subData{4});
  4799. // offset: 5; size: 1; index to last column
  4800. $lc = ord($subData{5});
  4801. // check values
  4802. if ($fr > $lr || $fc > $lc) {
  4803. throw new Exception('Not a cell range address');
  4804. }
  4805. // column index to letter
  4806. $fc = PHPExcel_Cell::stringFromColumnIndex($fc);
  4807. $lc = PHPExcel_Cell::stringFromColumnIndex($lc);
  4808. if ($fr == $lr and $fc == $lc) {
  4809. return "$fc$fr";
  4810. }
  4811. return "$fc$fr:$lc$lr";
  4812. }
  4813. /**
  4814. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
  4815. * always fixed range
  4816. * section 2.5.14
  4817. *
  4818. * @param string $subData
  4819. * @return string
  4820. * @throws Exception
  4821. */
  4822. private function _readBIFF8CellRangeAddressFixed($subData)
  4823. {
  4824. // offset: 0; size: 2; index to first row
  4825. $fr = self::_GetInt2d($subData, 0) + 1;
  4826. // offset: 2; size: 2; index to last row
  4827. $lr = self::_GetInt2d($subData, 2) + 1;
  4828. // offset: 4; size: 2; index to first column
  4829. $fc = self::_GetInt2d($subData, 4);
  4830. // offset: 6; size: 2; index to last column
  4831. $lc = self::_GetInt2d($subData, 6);
  4832. // check values
  4833. if ($fr > $lr || $fc > $lc) {
  4834. throw new Exception('Not a cell range address');
  4835. }
  4836. // column index to letter
  4837. $fc = PHPExcel_Cell::stringFromColumnIndex($fc);
  4838. $lc = PHPExcel_Cell::stringFromColumnIndex($lc);
  4839. if ($fr == $lr and $fc == $lc) {
  4840. return "$fc$fr";
  4841. }
  4842. return "$fc$fr:$lc$lr";
  4843. }
  4844. /**
  4845. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
  4846. * there are flags indicating whether column/row index is relative
  4847. * section 3.3.4
  4848. *
  4849. * @param string $subData
  4850. * @return string
  4851. */
  4852. private function _readBIFF8CellRangeAddress($subData)
  4853. {
  4854. // todo: if cell range is just a single cell, should this funciton
  4855. // not just return e.g. 'A1' and not 'A1:A1' ?
  4856. // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
  4857. $fr = self::_GetInt2d($subData, 0) + 1;
  4858. // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
  4859. $lr = self::_GetInt2d($subData, 2) + 1;
  4860. // offset: 4; size: 2; index to first column or column offset + relative flags
  4861. // bit: 7-0; mask 0x00FF; column index
  4862. $fc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($subData, 4));
  4863. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4864. if (!(0x4000 & self::_GetInt2d($subData, 4))) {
  4865. $fc = '$' . $fc;
  4866. }
  4867. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4868. if (!(0x8000 & self::_GetInt2d($subData, 4))) {
  4869. $fr = '$' . $fr;
  4870. }
  4871. // offset: 6; size: 2; index to last column or column offset + relative flags
  4872. // bit: 7-0; mask 0x00FF; column index
  4873. $lc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($subData, 6));
  4874. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4875. if (!(0x4000 & self::_GetInt2d($subData, 6))) {
  4876. $lc = '$' . $lc;
  4877. }
  4878. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4879. if (!(0x8000 & self::_GetInt2d($subData, 6))) {
  4880. $lr = '$' . $lr;
  4881. }
  4882. return "$fc$fr:$lc$lr";
  4883. }
  4884. /**
  4885. * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  4886. * to indicate offsets from a base cell
  4887. * section 3.3.4
  4888. *
  4889. * @param string $subData
  4890. * @param string $baseCell Base cell
  4891. * @return string Cell range address
  4892. */
  4893. private function _readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
  4894. {
  4895. list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
  4896. $baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
  4897. // TODO: if cell range is just a single cell, should this funciton
  4898. // not just return e.g. 'A1' and not 'A1:A1' ?
  4899. // offset: 0; size: 2; first row
  4900. $frIndex = self::_GetInt2d($subData, 0); // adjust below
  4901. // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
  4902. $lrIndex = self::_GetInt2d($subData, 2); // adjust below
  4903. // offset: 4; size: 2; first column with relative/absolute flags
  4904. // bit: 7-0; mask 0x00FF; column index
  4905. $fcIndex = 0x00FF & self::_GetInt2d($subData, 4);
  4906. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4907. if (!(0x4000 & self::_GetInt2d($subData, 4))) {
  4908. // absolute column index
  4909. $fc = PHPExcel_Cell::stringFromColumnIndex($fcIndex);
  4910. $fc = '$' . $fc;
  4911. } else {
  4912. // column offset
  4913. $fcIndex = ($fcIndex <= 127) ? $fcIndex : $fcIndex - 256;
  4914. $fc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $fcIndex);
  4915. }
  4916. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4917. if (!(0x8000 & self::_GetInt2d($subData, 4))) {
  4918. // absolute row index
  4919. $fr = $frIndex + 1;
  4920. $fr = '$' . $fr;
  4921. } else {
  4922. // row offset
  4923. $frIndex = ($frIndex <= 32767) ? $frIndex : $frIndex - 65536;
  4924. $fr = $baseRow + $frIndex;
  4925. }
  4926. // offset: 6; size: 2; last column with relative/absolute flags
  4927. // bit: 7-0; mask 0x00FF; column index
  4928. $lcIndex = 0x00FF & self::_GetInt2d($subData, 6);
  4929. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  4930. $lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
  4931. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4932. if (!(0x4000 & self::_GetInt2d($subData, 6))) {
  4933. // absolute column index
  4934. $lc = PHPExcel_Cell::stringFromColumnIndex($lcIndex);
  4935. $lc = '$' . $lc;
  4936. } else {
  4937. // column offset
  4938. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  4939. $lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
  4940. }
  4941. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4942. if (!(0x8000 & self::_GetInt2d($subData, 6))) {
  4943. // absolute row index
  4944. $lr = $lrIndex + 1;
  4945. $lr = '$' . $lr;
  4946. } else {
  4947. // row offset
  4948. $lrIndex = ($lrIndex <= 32767) ? $lrIndex : $lrIndex - 65536;
  4949. $lr = $baseRow + $lrIndex;
  4950. }
  4951. return "$fc$fr:$lc$lr";
  4952. }
  4953. /**
  4954. * Read BIFF8 cell range address list
  4955. * section 2.5.15
  4956. *
  4957. * @param string $subData
  4958. * @return array
  4959. */
  4960. private function _readBIFF8CellRangeAddressList($subData)
  4961. {
  4962. $cellRangeAddresses = array();
  4963. // offset: 0; size: 2; number of the following cell range addresses
  4964. $nm = self::_GetInt2d($subData, 0);
  4965. $offset = 2;
  4966. // offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
  4967. for ($i = 0; $i < $nm; ++$i) {
  4968. $cellRangeAddresses[] = $this->_readBIFF8CellRangeAddressFixed(substr($subData, $offset, 8));
  4969. $offset += 8;
  4970. }
  4971. return array(
  4972. 'size' => 2 + 8 * $nm,
  4973. 'cellRangeAddresses' => $cellRangeAddresses,
  4974. );
  4975. }
  4976. /**
  4977. * Read BIFF5 cell range address list
  4978. * section 2.5.15
  4979. *
  4980. * @param string $subData
  4981. * @return array
  4982. */
  4983. private function _readBIFF5CellRangeAddressList($subData)
  4984. {
  4985. $cellRangeAddresses = array();
  4986. // offset: 0; size: 2; number of the following cell range addresses
  4987. $nm = self::_GetInt2d($subData, 0);
  4988. $offset = 2;
  4989. // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
  4990. for ($i = 0; $i < $nm; ++$i) {
  4991. $cellRangeAddresses[] = $this->_readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
  4992. $offset += 6;
  4993. }
  4994. return array(
  4995. 'size' => 2 + 6 * $nm,
  4996. 'cellRangeAddresses' => $cellRangeAddresses,
  4997. );
  4998. }
  4999. /**
  5000. * Get a sheet range like Sheet1:Sheet3 from REF index
  5001. * Note: If there is only one sheet in the range, one gets e.g Sheet1
  5002. * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
  5003. * in which case an exception is thrown
  5004. *
  5005. * @param int $index
  5006. * @return string|false
  5007. * @throws Exception
  5008. */
  5009. private function _readSheetRangeByRefIndex($index)
  5010. {
  5011. if (isset($this->_ref[$index])) {
  5012. $type = $this->_externalBooks[$this->_ref[$index]['externalBookIndex']]['type'];
  5013. switch ($type) {
  5014. case 'internal':
  5015. // check if we have a deleted 3d reference
  5016. if ($this->_ref[$index]['firstSheetIndex'] == 0xFFFF or $this->_ref[$index]['lastSheetIndex'] == 0xFFFF) {
  5017. throw new Exception('Deleted sheet reference');
  5018. }
  5019. // we have normal sheet range (collapsed or uncollapsed)
  5020. $firstSheetName = $this->_sheets[$this->_ref[$index]['firstSheetIndex']]['name'];
  5021. $lastSheetName = $this->_sheets[$this->_ref[$index]['lastSheetIndex']]['name'];
  5022. if ($firstSheetName == $lastSheetName) {
  5023. // collapsed sheet range
  5024. $sheetRange = $firstSheetName;
  5025. } else {
  5026. $sheetRange = "$firstSheetName:$lastSheetName";
  5027. }
  5028. // escape the single-quotes
  5029. $sheetRange = str_replace("'", "''", $sheetRange);
  5030. // if there are special characters, we need to enclose the range in single-quotes
  5031. // todo: check if we have identified the whole set of special characters
  5032. // it seems that the following characters are not accepted for sheet names
  5033. // and we may assume that they are not present: []*/:\?
  5034. if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/", $sheetRange)) {
  5035. $sheetRange = "'$sheetRange'";
  5036. }
  5037. return $sheetRange;
  5038. break;
  5039. default:
  5040. // TODO: external sheet support
  5041. throw new Exception('Excel5 reader only supports internal sheets in fomulas');
  5042. break;
  5043. }
  5044. }
  5045. return false;
  5046. }
  5047. /**
  5048. * read BIFF8 constant value array from array data
  5049. * returns e.g. array('value' => '{1,2;3,4}', 'size' => 40}
  5050. * section 2.5.8
  5051. *
  5052. * @param string $arrayData
  5053. * @return array
  5054. */
  5055. private static function _readBIFF8ConstantArray($arrayData)
  5056. {
  5057. // offset: 0; size: 1; number of columns decreased by 1
  5058. $nc = ord($arrayData[0]);
  5059. // offset: 1; size: 2; number of rows decreased by 1
  5060. $nr = self::_GetInt2d($arrayData, 1);
  5061. $size = 3; // initialize
  5062. $arrayData = substr($arrayData, 3);
  5063. // offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
  5064. $matrixChunks = array();
  5065. for ($r = 1; $r <= $nr + 1; ++$r) {
  5066. $items = array();
  5067. for ($c = 1; $c <= $nc + 1; ++$c) {
  5068. $constant = self::_readBIFF8Constant($arrayData);
  5069. $items[] = $constant['value'];
  5070. $arrayData = substr($arrayData, $constant['size']);
  5071. $size += $constant['size'];
  5072. }
  5073. $matrixChunks[] = implode(',', $items); // looks like e.g. '1,"hello"'
  5074. }
  5075. $matrix = '{' . implode(';', $matrixChunks) . '}';
  5076. return array(
  5077. 'value' => $matrix,
  5078. 'size' => $size,
  5079. );
  5080. }
  5081. /**
  5082. * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
  5083. * section 2.5.7
  5084. * returns e.g. array('value' => '5', 'size' => 9)
  5085. *
  5086. * @param string $valueData
  5087. * @return array
  5088. */
  5089. private static function _readBIFF8Constant($valueData)
  5090. {
  5091. // offset: 0; size: 1; identifier for type of constant
  5092. $identifier = ord($valueData[0]);
  5093. switch ($identifier) {
  5094. case 0x00: // empty constant (what is this?)
  5095. $value = '';
  5096. $size = 9;
  5097. break;
  5098. case 0x01: // number
  5099. // offset: 1; size: 8; IEEE 754 floating-point value
  5100. $value = self::_extractNumber(substr($valueData, 1, 8));
  5101. $size = 9;
  5102. break;
  5103. case 0x02: // string value
  5104. // offset: 1; size: var; Unicode string, 16-bit string length
  5105. $string = self::_readUnicodeStringLong(substr($valueData, 1));
  5106. $value = '"' . $string['value'] . '"';
  5107. $size = 1 + $string['size'];
  5108. break;
  5109. case 0x04: // boolean
  5110. // offset: 1; size: 1; 0 = FALSE, 1 = TRUE
  5111. if (ord($valueData[1])) {
  5112. $value = 'TRUE';
  5113. } else {
  5114. $value = 'FALSE';
  5115. }
  5116. $size = 9;
  5117. break;
  5118. case 0x10: // error code
  5119. // offset: 1; size: 1; error code
  5120. $value = self::_mapErrorCode(ord($valueData[1]));
  5121. $size = 9;
  5122. break;
  5123. }
  5124. return array(
  5125. 'value' => $value,
  5126. 'size' => $size,
  5127. );
  5128. }
  5129. /**
  5130. * Extract RGB color
  5131. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4
  5132. *
  5133. * @param string $rgb Encoded RGB value (4 bytes)
  5134. * @return array
  5135. */
  5136. private static function _readRGB($rgb)
  5137. {
  5138. // offset: 0; size 1; Red component
  5139. $r = ord($rgb{0});
  5140. // offset: 1; size: 1; Green component
  5141. $g = ord($rgb{1});
  5142. // offset: 2; size: 1; Blue component
  5143. $b = ord($rgb{2});
  5144. // HEX notation, e.g. 'FF00FC'
  5145. $rgb = sprintf('%02X%02X%02X', $r, $g, $b);
  5146. return array('rgb' => $rgb);
  5147. }
  5148. /**
  5149. * Read byte string (8-bit string length)
  5150. * OpenOffice documentation: 2.5.2
  5151. *
  5152. * @param string $subData
  5153. * @return array
  5154. */
  5155. private function _readByteStringShort($subData)
  5156. {
  5157. // offset: 0; size: 1; length of the string (character count)
  5158. $ln = ord($subData[0]);
  5159. // offset: 1: size: var; character array (8-bit characters)
  5160. $value = $this->_decodeCodepage(substr($subData, 1, $ln));
  5161. return array(
  5162. 'value' => $value,
  5163. 'size' => 1 + $ln, // size in bytes of data structure
  5164. );
  5165. }
  5166. /**
  5167. * Read byte string (16-bit string length)
  5168. * OpenOffice documentation: 2.5.2
  5169. *
  5170. * @param string $subData
  5171. * @return array
  5172. */
  5173. private function _readByteStringLong($subData)
  5174. {
  5175. // offset: 0; size: 2; length of the string (character count)
  5176. $ln = self::_GetInt2d($subData, 0);
  5177. // offset: 2: size: var; character array (8-bit characters)
  5178. $value = $this->_decodeCodepage(substr($subData, 2));
  5179. //return $string;
  5180. return array(
  5181. 'value' => $value,
  5182. 'size' => 2 + $ln, // size in bytes of data structure
  5183. );
  5184. }
  5185. /**
  5186. * Extracts an Excel Unicode short string (8-bit string length)
  5187. * OpenOffice documentation: 2.5.3
  5188. * function will automatically find out where the Unicode string ends.
  5189. *
  5190. * @param string $subData
  5191. * @return array
  5192. */
  5193. private static function _readUnicodeStringShort($subData)
  5194. {
  5195. $value = '';
  5196. // offset: 0: size: 1; length of the string (character count)
  5197. $characterCount = ord($subData[0]);
  5198. $string = self::_readUnicodeString(substr($subData, 1), $characterCount);
  5199. // add 1 for the string length
  5200. $string['size'] += 1;
  5201. return $string;
  5202. }
  5203. /**
  5204. * Extracts an Excel Unicode long string (16-bit string length)
  5205. * OpenOffice documentation: 2.5.3
  5206. * this function is under construction, needs to support rich text, and Asian phonetic settings
  5207. *
  5208. * @param string $subData
  5209. * @return array
  5210. */
  5211. private static function _readUnicodeStringLong($subData)
  5212. {
  5213. $value = '';
  5214. // offset: 0: size: 2; length of the string (character count)
  5215. $characterCount = self::_GetInt2d($subData, 0);
  5216. $string = self::_readUnicodeString(substr($subData, 2), $characterCount);
  5217. // add 2 for the string length
  5218. $string['size'] += 2;
  5219. return $string;
  5220. }
  5221. /**
  5222. * Read Unicode string with no string length field, but with known character count
  5223. * this function is under construction, needs to support rich text, and Asian phonetic settings
  5224. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
  5225. *
  5226. * @param string $subData
  5227. * @param int $characterCount
  5228. * @return array
  5229. */
  5230. private static function _readUnicodeString($subData, $characterCount)
  5231. {
  5232. $value = '';
  5233. // offset: 0: size: 1; option flags
  5234. // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  5235. $isCompressed = !((0x01 & ord($subData[0])) >> 0);
  5236. // bit: 2; mask: 0x04; Asian phonetic settings
  5237. $hasAsian = (0x04) & ord($subData[0]) >> 2;
  5238. // bit: 3; mask: 0x08; Rich-Text settings
  5239. $hasRichText = (0x08) & ord($subData[0]) >> 3;
  5240. // offset: 1: size: var; character array
  5241. // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  5242. // needs to be fixed
  5243. $value = self::_encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
  5244. return array(
  5245. 'value' => $value,
  5246. 'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount, // the size in bytes including the option flags
  5247. );
  5248. }
  5249. /**
  5250. * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
  5251. * Example: hello"world --> "hello""world"
  5252. *
  5253. * @param string $value UTF-8 encoded string
  5254. * @return string
  5255. */
  5256. private static function _UTF8toExcelDoubleQuoted($value)
  5257. {
  5258. return '"' . str_replace('"', '""', $value) . '"';
  5259. }
  5260. /**
  5261. * Reads first 8 bytes of a string and return IEEE 754 float
  5262. *
  5263. * @param string $data Binary string that is at least 8 bytes long
  5264. * @return float
  5265. */
  5266. private static function _extractNumber($data)
  5267. {
  5268. $rknumhigh = self::_GetInt4d($data, 4);
  5269. $rknumlow = self::_GetInt4d($data, 0);
  5270. $sign = ($rknumhigh & 0x80000000) >> 31;
  5271. $exp = (($rknumhigh & 0x7ff00000) >> 20) - 1023;
  5272. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  5273. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  5274. $mantissalow2 = ($rknumlow & 0x7fffffff);
  5275. $value = $mantissa / pow( 2 , (20 - $exp));
  5276. if ($mantissalow1 != 0) {
  5277. $value += 1 / pow (2 , (21 - $exp));
  5278. }
  5279. $value += $mantissalow2 / pow (2 , (52 - $exp));
  5280. if ($sign) {
  5281. $value = -1 * $value;
  5282. }
  5283. return $value;
  5284. }
  5285. private static function _GetIEEE754($rknum)
  5286. {
  5287. if (($rknum & 0x02) != 0) {
  5288. $value = $rknum >> 2;
  5289. }
  5290. else {
  5291. // changes by mmp, info on IEEE754 encoding from
  5292. // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  5293. // The RK format calls for using only the most significant 30 bits
  5294. // of the 64 bit floating point value. The other 34 bits are assumed
  5295. // to be 0 so we use the upper 30 bits of $rknum as follows...
  5296. $sign = ($rknum & 0x80000000) >> 31;
  5297. $exp = ($rknum & 0x7ff00000) >> 20;
  5298. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  5299. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  5300. if ($sign) {
  5301. $value = -1 * $value;
  5302. }
  5303. //end of changes by mmp
  5304. }
  5305. if (($rknum & 0x01) != 0) {
  5306. $value /= 100;
  5307. }
  5308. return $value;
  5309. }
  5310. /**
  5311. * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
  5312. *
  5313. * @param string $string
  5314. * @param bool $compressed
  5315. * @return string
  5316. */
  5317. private static function _encodeUTF16($string, $compressed = '')
  5318. {
  5319. if ($compressed) {
  5320. $string = self::_uncompressByteString($string);
  5321. }
  5322. return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', 'UTF-16LE');
  5323. }
  5324. /**
  5325. * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
  5326. *
  5327. * @param string $string
  5328. * @return string
  5329. */
  5330. private static function _uncompressByteString($string)
  5331. {
  5332. $uncompressedString = '';
  5333. $strLen = strlen($string);
  5334. for ($i = 0; $i < $strLen; ++$i) {
  5335. $uncompressedString .= $string[$i] . "\0";
  5336. }
  5337. return $uncompressedString;
  5338. }
  5339. /**
  5340. * Convert string to UTF-8. Only used for BIFF5.
  5341. *
  5342. * @param string $string
  5343. * @return string
  5344. */
  5345. private function _decodeCodepage($string)
  5346. {
  5347. return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', $this->_codepage);
  5348. }
  5349. /**
  5350. * Read 16-bit unsigned integer
  5351. *
  5352. * @param string $data
  5353. * @param int $pos
  5354. * @return int
  5355. */
  5356. public static function _GetInt2d($data, $pos)
  5357. {
  5358. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
  5359. }
  5360. /**
  5361. * Read 32-bit signed integer
  5362. *
  5363. * @param string $data
  5364. * @param int $pos
  5365. * @return int
  5366. */
  5367. public static function _GetInt4d($data, $pos)
  5368. {
  5369. // FIX: represent numbers correctly on 64-bit system
  5370. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  5371. // Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  5372. $_or_24 = ord($data[$pos + 3]);
  5373. if ($_or_24 >= 128) {
  5374. // negative number
  5375. $_ord_24 = -abs((256 - $_or_24) << 24);
  5376. } else {
  5377. $_ord_24 = ($_or_24 & 127) << 24;
  5378. }
  5379. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
  5380. }
  5381. /**
  5382. * Read color
  5383. *
  5384. * @param int $color Indexed color
  5385. * @param array $palette Color palette
  5386. * @return array RGB color value, example: array('rgb' => 'FF0000')
  5387. */
  5388. private static function _readColor($color,$palette,$version)
  5389. {
  5390. if ($color <= 0x07 || $color >= 0x40) {
  5391. // special built-in color
  5392. return self::_mapBuiltInColor($color);
  5393. } elseif (isset($palette) && isset($palette[$color - 8])) {
  5394. // palette color, color index 0x08 maps to pallete index 0
  5395. return $palette[$color - 8];
  5396. } else {
  5397. // default color table
  5398. if ($version == self::XLS_BIFF8) {
  5399. return self::_mapColor($color);
  5400. } else {
  5401. // BIFF5
  5402. return self::_mapColorBIFF5($color);
  5403. }
  5404. }
  5405. return $color;
  5406. }
  5407. /**
  5408. * Map border style
  5409. * OpenOffice documentation: 2.5.11
  5410. *
  5411. * @param int $index
  5412. * @return string
  5413. */
  5414. private static function _mapBorderStyle($index)
  5415. {
  5416. switch ($index) {
  5417. case 0x00: return PHPExcel_Style_Border::BORDER_NONE;
  5418. case 0x01: return PHPExcel_Style_Border::BORDER_THIN;
  5419. case 0x02: return PHPExcel_Style_Border::BORDER_MEDIUM;
  5420. case 0x03: return PHPExcel_Style_Border::BORDER_DASHED;
  5421. case 0x04: return PHPExcel_Style_Border::BORDER_DOTTED;
  5422. case 0x05: return PHPExcel_Style_Border::BORDER_THICK;
  5423. case 0x06: return PHPExcel_Style_Border::BORDER_DOUBLE;
  5424. case 0x07: return PHPExcel_Style_Border::BORDER_HAIR;
  5425. case 0x08: return PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
  5426. case 0x09: return PHPExcel_Style_Border::BORDER_DASHDOT;
  5427. case 0x0A: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
  5428. case 0x0B: return PHPExcel_Style_Border::BORDER_DASHDOTDOT;
  5429. case 0x0C: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  5430. case 0x0D: return PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
  5431. default: return PHPExcel_Style_Border::BORDER_NONE;
  5432. }
  5433. }
  5434. /**
  5435. * Get fill pattern from index
  5436. * OpenOffice documentation: 2.5.12
  5437. *
  5438. * @param int $index
  5439. * @return string
  5440. */
  5441. private static function _mapFillPattern($index)
  5442. {
  5443. switch ($index) {
  5444. case 0x00: return PHPExcel_Style_Fill::FILL_NONE;
  5445. case 0x01: return PHPExcel_Style_Fill::FILL_SOLID;
  5446. case 0x02: return PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
  5447. case 0x03: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
  5448. case 0x04: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
  5449. case 0x05: return PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
  5450. case 0x06: return PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
  5451. case 0x07: return PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
  5452. case 0x08: return PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
  5453. case 0x09: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
  5454. case 0x0A: return PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
  5455. case 0x0B: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
  5456. case 0x0C: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
  5457. case 0x0D: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
  5458. case 0x0E: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
  5459. case 0x0F: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
  5460. case 0x10: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
  5461. case 0x11: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
  5462. case 0x12: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
  5463. default: return PHPExcel_Style_Fill::FILL_NONE;
  5464. }
  5465. }
  5466. /**
  5467. * Map error code, e.g. '#N/A'
  5468. *
  5469. * @param int $subData
  5470. * @return string
  5471. */
  5472. private static function _mapErrorCode($subData)
  5473. {
  5474. switch ($subData) {
  5475. case 0x00: return '#NULL!'; break;
  5476. case 0x07: return '#DIV/0!'; break;
  5477. case 0x0F: return '#VALUE!'; break;
  5478. case 0x17: return '#REF!'; break;
  5479. case 0x1D: return '#NAME?'; break;
  5480. case 0x24: return '#NUM!'; break;
  5481. case 0x2A: return '#N/A'; break;
  5482. default: return false;
  5483. }
  5484. }
  5485. /**
  5486. * Map built-in color to RGB value
  5487. *
  5488. * @param int $color Indexed color
  5489. * @return array
  5490. */
  5491. private static function _mapBuiltInColor($color)
  5492. {
  5493. switch ($color) {
  5494. case 0x00: return array('rgb' => '000000');
  5495. case 0x01: return array('rgb' => 'FFFFFF');
  5496. case 0x02: return array('rgb' => 'FF0000');
  5497. case 0x03: return array('rgb' => '00FF00');
  5498. case 0x04: return array('rgb' => '0000FF');
  5499. case 0x05: return array('rgb' => 'FFFF00');
  5500. case 0x06: return array('rgb' => 'FF00FF');
  5501. case 0x07: return array('rgb' => '00FFFF');
  5502. case 0x40: return array('rgb' => '000000'); // system window text color
  5503. case 0x41: return array('rgb' => 'FFFFFF'); // system window background color
  5504. default: return array('rgb' => '000000');
  5505. }
  5506. }
  5507. /**
  5508. * Map color array from BIFF5 built-in color index
  5509. *
  5510. * @param int $subData
  5511. * @return array
  5512. */
  5513. private static function _mapColorBIFF5($subData)
  5514. {
  5515. switch ($subData) {
  5516. case 0x08: return array('rgb' => '000000');
  5517. case 0x09: return array('rgb' => 'FFFFFF');
  5518. case 0x0A: return array('rgb' => 'FF0000');
  5519. case 0x0B: return array('rgb' => '00FF00');
  5520. case 0x0C: return array('rgb' => '0000FF');
  5521. case 0x0D: return array('rgb' => 'FFFF00');
  5522. case 0x0E: return array('rgb' => 'FF00FF');
  5523. case 0x0F: return array('rgb' => '00FFFF');
  5524. case 0x10: return array('rgb' => '800000');
  5525. case 0x11: return array('rgb' => '008000');
  5526. case 0x12: return array('rgb' => '000080');
  5527. case 0x13: return array('rgb' => '808000');
  5528. case 0x14: return array('rgb' => '800080');
  5529. case 0x15: return array('rgb' => '008080');
  5530. case 0x16: return array('rgb' => 'C0C0C0');
  5531. case 0x17: return array('rgb' => '808080');
  5532. case 0x18: return array('rgb' => '8080FF');
  5533. case 0x19: return array('rgb' => '802060');
  5534. case 0x1A: return array('rgb' => 'FFFFC0');
  5535. case 0x1B: return array('rgb' => 'A0E0F0');
  5536. case 0x1C: return array('rgb' => '600080');
  5537. case 0x1D: return array('rgb' => 'FF8080');
  5538. case 0x1E: return array('rgb' => '0080C0');
  5539. case 0x1F: return array('rgb' => 'C0C0FF');
  5540. case 0x20: return array('rgb' => '000080');
  5541. case 0x21: return array('rgb' => 'FF00FF');
  5542. case 0x22: return array('rgb' => 'FFFF00');
  5543. case 0x23: return array('rgb' => '00FFFF');
  5544. case 0x24: return array('rgb' => '800080');
  5545. case 0x25: return array('rgb' => '800000');
  5546. case 0x26: return array('rgb' => '008080');
  5547. case 0x27: return array('rgb' => '0000FF');
  5548. case 0x28: return array('rgb' => '00CFFF');
  5549. case 0x29: return array('rgb' => '69FFFF');
  5550. case 0x2A: return array('rgb' => 'E0FFE0');
  5551. case 0x2B: return array('rgb' => 'FFFF80');
  5552. case 0x2C: return array('rgb' => 'A6CAF0');
  5553. case 0x2D: return array('rgb' => 'DD9CB3');
  5554. case 0x2E: return array('rgb' => 'B38FEE');
  5555. case 0x2F: return array('rgb' => 'E3E3E3');
  5556. case 0x30: return array('rgb' => '2A6FF9');
  5557. case 0x31: return array('rgb' => '3FB8CD');
  5558. case 0x32: return array('rgb' => '488436');
  5559. case 0x33: return array('rgb' => '958C41');
  5560. case 0x34: return array('rgb' => '8E5E42');
  5561. case 0x35: return array('rgb' => 'A0627A');
  5562. case 0x36: return array('rgb' => '624FAC');
  5563. case 0x37: return array('rgb' => '969696');
  5564. case 0x38: return array('rgb' => '1D2FBE');
  5565. case 0x39: return array('rgb' => '286676');
  5566. case 0x3A: return array('rgb' => '004500');
  5567. case 0x3B: return array('rgb' => '453E01');
  5568. case 0x3C: return array('rgb' => '6A2813');
  5569. case 0x3D: return array('rgb' => '85396A');
  5570. case 0x3E: return array('rgb' => '4A3285');
  5571. case 0x3F: return array('rgb' => '424242');
  5572. default: return array('rgb' => '000000');
  5573. }
  5574. }
  5575. /**
  5576. * Map color array from BIFF8 built-in color index
  5577. *
  5578. * @param int $subData
  5579. * @return array
  5580. */
  5581. private static function _mapColor($subData)
  5582. {
  5583. switch ($subData) {
  5584. case 0x08: return array('rgb' => '000000');
  5585. case 0x09: return array('rgb' => 'FFFFFF');
  5586. case 0x0A: return array('rgb' => 'FF0000');
  5587. case 0x0B: return array('rgb' => '00FF00');
  5588. case 0x0C: return array('rgb' => '0000FF');
  5589. case 0x0D: return array('rgb' => 'FFFF00');
  5590. case 0x0E: return array('rgb' => 'FF00FF');
  5591. case 0x0F: return array('rgb' => '00FFFF');
  5592. case 0x10: return array('rgb' => '800000');
  5593. case 0x11: return array('rgb' => '008000');
  5594. case 0x12: return array('rgb' => '000080');
  5595. case 0x13: return array('rgb' => '808000');
  5596. case 0x14: return array('rgb' => '800080');
  5597. case 0x15: return array('rgb' => '008080');
  5598. case 0x16: return array('rgb' => 'C0C0C0');
  5599. case 0x17: return array('rgb' => '808080');
  5600. case 0x18: return array('rgb' => '9999FF');
  5601. case 0x19: return array('rgb' => '993366');
  5602. case 0x1A: return array('rgb' => 'FFFFCC');
  5603. case 0x1B: return array('rgb' => 'CCFFFF');
  5604. case 0x1C: return array('rgb' => '660066');
  5605. case 0x1D: return array('rgb' => 'FF8080');
  5606. case 0x1E: return array('rgb' => '0066CC');
  5607. case 0x1F: return array('rgb' => 'CCCCFF');
  5608. case 0x20: return array('rgb' => '000080');
  5609. case 0x21: return array('rgb' => 'FF00FF');
  5610. case 0x22: return array('rgb' => 'FFFF00');
  5611. case 0x23: return array('rgb' => '00FFFF');
  5612. case 0x24: return array('rgb' => '800080');
  5613. case 0x25: return array('rgb' => '800000');
  5614. case 0x26: return array('rgb' => '008080');
  5615. case 0x27: return array('rgb' => '0000FF');
  5616. case 0x28: return array('rgb' => '00CCFF');
  5617. case 0x29: return array('rgb' => 'CCFFFF');
  5618. case 0x2A: return array('rgb' => 'CCFFCC');
  5619. case 0x2B: return array('rgb' => 'FFFF99');
  5620. case 0x2C: return array('rgb' => '99CCFF');
  5621. case 0x2D: return array('rgb' => 'FF99CC');
  5622. case 0x2E: return array('rgb' => 'CC99FF');
  5623. case 0x2F: return array('rgb' => 'FFCC99');
  5624. case 0x30: return array('rgb' => '3366FF');
  5625. case 0x31: return array('rgb' => '33CCCC');
  5626. case 0x32: return array('rgb' => '99CC00');
  5627. case 0x33: return array('rgb' => 'FFCC00');
  5628. case 0x34: return array('rgb' => 'FF9900');
  5629. case 0x35: return array('rgb' => 'FF6600');
  5630. case 0x36: return array('rgb' => '666699');
  5631. case 0x37: return array('rgb' => '969696');
  5632. case 0x38: return array('rgb' => '003366');
  5633. case 0x39: return array('rgb' => '339966');
  5634. case 0x3A: return array('rgb' => '003300');
  5635. case 0x3B: return array('rgb' => '333300');
  5636. case 0x3C: return array('rgb' => '993300');
  5637. case 0x3D: return array('rgb' => '993366');
  5638. case 0x3E: return array('rgb' => '333399');
  5639. case 0x3F: return array('rgb' => '333333');
  5640. default: return array('rgb' => '000000');
  5641. }
  5642. }
  5643. private function _parseRichText($is = '') {
  5644. $value = new PHPExcel_RichText();
  5645. $value->createText($is);
  5646. return $value;
  5647. }
  5648. }