PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 2ms

/heart/protected/vendor/phpexcel/Classes/PHPExcel/Reader/Excel5.php

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