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

/lib/PHPExcel/Classes/PHPExcel/Reader/Excel5.php

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