PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/tablepress/libraries/excel-reader.class.php

https://github.com/instantiator/c100-site-wordpress
PHP | 1645 lines | 1524 code | 53 blank | 68 comment | 109 complexity | e519dfe7678aafcbf2e665d78f3c0ae2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * PHP Excel (97/2003) Reader Class
  4. *
  5. * @package TablePress
  6. * @subpackage Libraries
  7. * @author Matt Kruse, Matt Roxburgh, Vadim Tkachenko, Tobias B채thge
  8. * @since 1.1.0
  9. */
  10. // Prohibit direct script loading
  11. defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
  12. /**
  13. * A class for reading Microsoft Excel (97/2003) Spreadsheets.
  14. *
  15. * Version 2.21
  16. *
  17. * Enhanced and maintained by Matt Kruse < http://mattkruse.com >
  18. * Maintained at http://code.google.com/p/php-excel-reader/
  19. * Licensed under MIT license
  20. *
  21. * Format parsing and MUCH more contributed by:
  22. * Matt Roxburgh < http://www.roxburgh.me.uk >
  23. *
  24. * Cleanup and changes for TablePress by Tobias B채thge
  25. * --------------------------------------------------------------------------
  26. */
  27. define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
  28. define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
  29. define('ROOT_START_BLOCK_POS', 0x30);
  30. define('BIG_BLOCK_SIZE', 0x200);
  31. define('SMALL_BLOCK_SIZE', 0x40);
  32. define('EXTENSION_BLOCK_POS', 0x44);
  33. define('NUM_EXTENSION_BLOCK_POS', 0x48);
  34. define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
  35. define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
  36. define('SMALL_BLOCK_THRESHOLD', 0x1000);
  37. // property storage offsets
  38. define('SIZE_OF_NAME_POS', 0x40);
  39. define('TYPE_POS', 0x42);
  40. define('START_BLOCK_POS', 0x74);
  41. define('SIZE_POS', 0x78);
  42. define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
  43. class OLERead {
  44. var $data = '';
  45. function OLERead(){ }
  46. function read($data){
  47. $this->data = $data;
  48. if (!$this->data) {
  49. $this->error = 1;
  50. return false;
  51. }
  52. if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
  53. $this->error = 2;
  54. return false;
  55. }
  56. $this->numBigBlockDepotBlocks = $this->_GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  57. $this->sbdStartBlock = $this->_GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
  58. $this->rootStartBlock = $this->_GetInt4d($this->data, ROOT_START_BLOCK_POS);
  59. $this->extensionBlock = $this->_GetInt4d($this->data, EXTENSION_BLOCK_POS);
  60. $this->numExtensionBlocks = $this->_GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);
  61. $bigBlockDepotBlocks = array();
  62. $pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
  63. $bbdBlocks = $this->numBigBlockDepotBlocks;
  64. if ($this->numExtensionBlocks != 0)
  65. $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
  66. for ($i = 0; $i < $bbdBlocks; $i++) {
  67. $bigBlockDepotBlocks[$i] = $this->_GetInt4d($this->data, $pos);
  68. $pos += 4;
  69. }
  70. for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
  71. $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
  72. $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);
  73. for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
  74. $bigBlockDepotBlocks[$i] = $this->_GetInt4d($this->data, $pos);
  75. $pos += 4;
  76. }
  77. $bbdBlocks += $blocksToRead;
  78. if ($bbdBlocks < $this->numBigBlockDepotBlocks)
  79. $this->extensionBlock = $this->_GetInt4d($this->data, $pos);
  80. }
  81. // readBigBlockDepot
  82. $pos = 0;
  83. $index = 0;
  84. $this->bigBlockChain = array();
  85. for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
  86. $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
  87. //echo "pos = $pos";
  88. for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
  89. $this->bigBlockChain[$index] = $this->_GetInt4d($this->data, $pos);
  90. $pos += 4 ;
  91. $index++;
  92. }
  93. }
  94. // readSmallBlockDepot();
  95. $pos = 0;
  96. $index = 0;
  97. $sbdBlock = $this->sbdStartBlock;
  98. $this->smallBlockChain = array();
  99. while ($sbdBlock != -2) {
  100. $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;
  101. for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
  102. $this->smallBlockChain[$index] = $this->_GetInt4d($this->data, $pos);
  103. $pos += 4;
  104. $index++;
  105. }
  106. $sbdBlock = $this->bigBlockChain[$sbdBlock];
  107. }
  108. // readData(rootStartBlock)
  109. $block = $this->rootStartBlock;
  110. $pos = 0;
  111. $this->entry = $this->__readData($block);
  112. $this->__readPropertySets();
  113. }
  114. function __readData($bl) {
  115. $block = $bl;
  116. $pos = 0;
  117. $data = '';
  118. while ($block != -2) {
  119. $pos = ($block + 1) * BIG_BLOCK_SIZE;
  120. $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
  121. $block = $this->bigBlockChain[$block];
  122. }
  123. return $data;
  124. }
  125. function __readPropertySets(){
  126. $offset = 0;
  127. while ($offset < strlen($this->entry)) {
  128. $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);
  129. $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8);
  130. $type = ord($d[TYPE_POS]);
  131. $startBlock = $this->_GetInt4d($d, START_BLOCK_POS);
  132. $size = $this->_GetInt4d($d, SIZE_POS);
  133. $name = '';
  134. for ($i = 0; $i < $nameSize ; $i++) {
  135. $name .= $d[$i];
  136. }
  137. $name = str_replace("\x00", "", $name);
  138. $this->props[] = array (
  139. 'name' => $name,
  140. 'type' => $type,
  141. 'startBlock' => $startBlock,
  142. 'size' => $size);
  143. if ((strtolower($name) == "workbook") || ( strtolower($name) == "book"))
  144. $this->wrkbook = count($this->props) - 1;
  145. if ($name == "Root Entry")
  146. $this->rootentry = count($this->props) - 1;
  147. $offset += PROPERTY_STORAGE_BLOCK_SIZE;
  148. }
  149. }
  150. function getWorkBook(){
  151. if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
  152. $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);
  153. $streamData = '';
  154. $block = $this->props[$this->wrkbook]['startBlock'];
  155. $pos = 0;
  156. while ($block != -2) {
  157. $pos = $block * SMALL_BLOCK_SIZE;
  158. $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);
  159. $block = $this->smallBlockChain[$block];
  160. }
  161. return $streamData;
  162. } else {
  163. $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
  164. if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
  165. $numBlocks++;
  166. }
  167. if ($numBlocks == 0)
  168. return '';
  169. $streamData = '';
  170. $block = $this->props[$this->wrkbook]['startBlock'];
  171. $pos = 0;
  172. while ($block != -2) {
  173. $pos = ($block + 1) * BIG_BLOCK_SIZE;
  174. $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
  175. $block = $this->bigBlockChain[$block];
  176. }
  177. return $streamData;
  178. }
  179. }
  180. function _GetInt4d($data, $pos) {
  181. $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  182. if ($value>=4294967294)
  183. $value=-2;
  184. return $value;
  185. }
  186. }
  187. define('SPREADSHEET_EXCEL_READER_BIFF8', 0x600);
  188. define('SPREADSHEET_EXCEL_READER_BIFF7', 0x500);
  189. define('SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS', 0x5);
  190. define('SPREADSHEET_EXCEL_READER_WORKSHEET', 0x10);
  191. define('SPREADSHEET_EXCEL_READER_TYPE_BOF', 0x809);
  192. define('SPREADSHEET_EXCEL_READER_TYPE_EOF', 0x0a);
  193. define('SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET', 0x85);
  194. define('SPREADSHEET_EXCEL_READER_TYPE_DIMENSION', 0x200);
  195. define('SPREADSHEET_EXCEL_READER_TYPE_ROW', 0x208);
  196. define('SPREADSHEET_EXCEL_READER_TYPE_DBCELL', 0xd7);
  197. define('SPREADSHEET_EXCEL_READER_TYPE_FILEPASS', 0x2f);
  198. define('SPREADSHEET_EXCEL_READER_TYPE_NOTE', 0x1c);
  199. define('SPREADSHEET_EXCEL_READER_TYPE_TXO', 0x1b6);
  200. define('SPREADSHEET_EXCEL_READER_TYPE_RK', 0x7e);
  201. define('SPREADSHEET_EXCEL_READER_TYPE_RK2', 0x27e);
  202. define('SPREADSHEET_EXCEL_READER_TYPE_MULRK', 0xbd);
  203. define('SPREADSHEET_EXCEL_READER_TYPE_MULBLANK', 0xbe);
  204. define('SPREADSHEET_EXCEL_READER_TYPE_INDEX', 0x20b);
  205. define('SPREADSHEET_EXCEL_READER_TYPE_SST', 0xfc);
  206. define('SPREADSHEET_EXCEL_READER_TYPE_EXTSST', 0xff);
  207. define('SPREADSHEET_EXCEL_READER_TYPE_CONTINUE', 0x3c);
  208. define('SPREADSHEET_EXCEL_READER_TYPE_LABEL', 0x204);
  209. define('SPREADSHEET_EXCEL_READER_TYPE_LABELSST', 0xfd);
  210. define('SPREADSHEET_EXCEL_READER_TYPE_NUMBER', 0x203);
  211. define('SPREADSHEET_EXCEL_READER_TYPE_NAME', 0x18);
  212. define('SPREADSHEET_EXCEL_READER_TYPE_ARRAY', 0x221);
  213. define('SPREADSHEET_EXCEL_READER_TYPE_STRING', 0x207);
  214. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA', 0x406);
  215. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA2', 0x6);
  216. define('SPREADSHEET_EXCEL_READER_TYPE_FORMAT', 0x41e);
  217. define('SPREADSHEET_EXCEL_READER_TYPE_XF', 0xe0);
  218. define('SPREADSHEET_EXCEL_READER_TYPE_BOOLERR', 0x205);
  219. define('SPREADSHEET_EXCEL_READER_TYPE_FONT', 0x0031);
  220. define('SPREADSHEET_EXCEL_READER_TYPE_PALETTE', 0x0092);
  221. define('SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN', 0xffff);
  222. define('SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22);
  223. define('SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS', 0xE5);
  224. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS', 25569);
  225. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107);
  226. define('SPREADSHEET_EXCEL_READER_MSINADAY', 86400);
  227. define('SPREADSHEET_EXCEL_READER_TYPE_HYPER', 0x01b8);
  228. define('SPREADSHEET_EXCEL_READER_TYPE_COLINFO', 0x7d);
  229. define('SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH', 0x55);
  230. define('SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH', 0x99);
  231. define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', "%s");
  232. /*
  233. * Main Class
  234. */
  235. class Spreadsheet_Excel_Reader {
  236. // MK: Added to make data retrieval easier
  237. var $colnames = array();
  238. var $colindexes = array();
  239. var $standardColWidth = 0;
  240. var $defaultColWidth = 0;
  241. function myHex($d) {
  242. if ($d < 16)
  243. return "0" . dechex($d);
  244. return dechex($d);
  245. }
  246. function dumpHexData($data, $pos, $length) {
  247. $info = "";
  248. for ($i = 0; $i <= $length; $i++) {
  249. $info .= ($i==0?"":" ") . $this->myHex(ord($data[$pos + $i])) . (ord($data[$pos + $i])>31? "[" . $data[$pos + $i] . "]":'');
  250. }
  251. return $info;
  252. }
  253. function getCol($col) {
  254. if (is_string($col)) {
  255. $col = strtolower($col);
  256. if (array_key_exists($col,$this->colnames))
  257. $col = $this->colnames[$col];
  258. }
  259. return $col;
  260. }
  261. // PUBLIC API FUNCTIONS
  262. // --------------------
  263. function val($row,$col,$sheet=0) {
  264. $col = $this->getCol($col);
  265. if (array_key_exists($row,$this->sheets[$sheet]['cells']) && array_key_exists($col,$this->sheets[$sheet]['cells'][$row]))
  266. return $this->sheets[$sheet]['cells'][$row][$col];
  267. return "";
  268. }
  269. function value($row,$col,$sheet=0) {
  270. return $this->val($row,$col,$sheet);
  271. }
  272. function info($row,$col,$type='',$sheet=0) {
  273. $col = $this->getCol($col);
  274. if (array_key_exists('cellsInfo',$this->sheets[$sheet])
  275. && array_key_exists($row,$this->sheets[$sheet]['cellsInfo'])
  276. && array_key_exists($col,$this->sheets[$sheet]['cellsInfo'][$row])
  277. && array_key_exists($type,$this->sheets[$sheet]['cellsInfo'][$row][$col])) {
  278. return $this->sheets[$sheet]['cellsInfo'][$row][$col][$type];
  279. }
  280. return "";
  281. }
  282. function type($row,$col,$sheet=0) {
  283. return $this->info($row,$col,'type',$sheet);
  284. }
  285. function raw($row,$col,$sheet=0) {
  286. return $this->info($row,$col,'raw',$sheet);
  287. }
  288. function rowspan($row,$col,$sheet=0) {
  289. $val = $this->info($row,$col,'rowspan',$sheet);
  290. if ($val=="")
  291. return 1;
  292. return $val;
  293. }
  294. function colspan($row,$col,$sheet=0) {
  295. $val = $this->info($row,$col,'colspan',$sheet);
  296. if ($val=="")
  297. return 1;
  298. return $val;
  299. }
  300. function hyperlink($row,$col,$sheet=0) {
  301. $link = $this->sheets[$sheet]['cellsInfo'][$row][$col]['hyperlink'];
  302. if ($link)
  303. return $link['link'];
  304. return '';
  305. }
  306. function rowcount($sheet=0) {
  307. return $this->sheets[$sheet]['numRows'];
  308. }
  309. function colcount($sheet=0) {
  310. return $this->sheets[$sheet]['numCols'];
  311. }
  312. function colwidth($col,$sheet=0) {
  313. // Col width is actually the width of the number 0. So we have to estimate and come close
  314. return $this->colInfo[$sheet][$col]['width']/9142*200;
  315. }
  316. function colhidden($col,$sheet=0) {
  317. return !!$this->colInfo[$sheet][$col]['hidden'];
  318. }
  319. function rowheight($row,$sheet=0) {
  320. return $this->rowInfo[$sheet][$row]['height'];
  321. }
  322. function rowhidden($row,$sheet=0) {
  323. return !!$this->rowInfo[$sheet][$row]['hidden'];
  324. }
  325. // GET THE CSS FOR FORMATTING
  326. // ==========================
  327. function style($row,$col,$sheet=0,$properties='') {
  328. $css = "";
  329. $font=$this->font($row,$col,$sheet);
  330. if ($font!="")
  331. $css .= "font-family:$font;";
  332. $align=$this->align($row,$col,$sheet);
  333. if ($align!="")
  334. $css .= "text-align:$align;";
  335. $height=$this->height($row,$col,$sheet);
  336. if ($height!="")
  337. $css .= "font-size:$height"."px;";
  338. $bgcolor=$this->bgColor($row,$col,$sheet);
  339. if ($bgcolor!="") {
  340. $bgcolor = $this->colors[$bgcolor];
  341. $css .= "background-color:$bgcolor;";
  342. }
  343. $color=$this->color($row,$col,$sheet);
  344. if ($color!="")
  345. $css .= "color:$color;";
  346. $bold=$this->bold($row,$col,$sheet);
  347. if ($bold)
  348. $css .= "font-weight:bold;";
  349. $italic=$this->italic($row,$col,$sheet);
  350. if ($italic)
  351. $css .= "font-style:italic;";
  352. $underline=$this->underline($row,$col,$sheet);
  353. if ($underline)
  354. $css .= "text-decoration:underline;";
  355. // Borders
  356. $bLeft = $this->borderLeft($row,$col,$sheet);
  357. $bRight = $this->borderRight($row,$col,$sheet);
  358. $bTop = $this->borderTop($row,$col,$sheet);
  359. $bBottom = $this->borderBottom($row,$col,$sheet);
  360. $bLeftCol = $this->borderLeftColor($row,$col,$sheet);
  361. $bRightCol = $this->borderRightColor($row,$col,$sheet);
  362. $bTopCol = $this->borderTopColor($row,$col,$sheet);
  363. $bBottomCol = $this->borderBottomColor($row,$col,$sheet);
  364. // Try to output the minimal required style
  365. if ($bLeft!="" && $bLeft==$bRight && $bRight==$bTop && $bTop==$bBottom) {
  366. $css .= "border:" . $this->lineStylesCss[$bLeft] .";";
  367. } else {
  368. if ($bLeft!="")
  369. $css .= "border-left:" . $this->lineStylesCss[$bLeft] .";";
  370. if ($bRight!="")
  371. $css .= "border-right:" . $this->lineStylesCss[$bRight] .";";
  372. if ($bTop!="")
  373. $css .= "border-top:" . $this->lineStylesCss[$bTop] .";";
  374. if ($bBottom!="")
  375. $css .= "border-bottom:" . $this->lineStylesCss[$bBottom] .";";
  376. }
  377. // Only output border colors if there is an actual border specified
  378. if ($bLeft!="" && $bLeftCol!="")
  379. $css .= "border-left-color:" . $bLeftCol .";";
  380. if ($bRight!="" && $bRightCol!="")
  381. $css .= "border-right-color:" . $bRightCol .";";
  382. if ($bTop!="" && $bTopCol!="")
  383. $css .= "border-top-color:" . $bTopCol . ";";
  384. if ($bBottom!="" && $bBottomCol!="")
  385. $css .= "border-bottom-color:" . $bBottomCol .";";
  386. return $css;
  387. }
  388. // FORMAT PROPERTIES
  389. // =================
  390. function format($row,$col,$sheet=0) {
  391. return $this->info($row,$col,'format',$sheet);
  392. }
  393. function formatIndex($row,$col,$sheet=0) {
  394. return $this->info($row,$col,'formatIndex',$sheet);
  395. }
  396. function formatColor($row,$col,$sheet=0) {
  397. return $this->info($row,$col,'formatColor',$sheet);
  398. }
  399. // CELL (XF) PROPERTIES
  400. // ====================
  401. function xfRecord($row,$col,$sheet=0) {
  402. $xfIndex = $this->info($row,$col,'xfIndex',$sheet);
  403. if ($xfIndex!="")
  404. return $this->xfRecords[$xfIndex];
  405. return null;
  406. }
  407. function xfProperty($row,$col,$sheet,$prop) {
  408. $xfRecord = $this->xfRecord($row,$col,$sheet);
  409. if ($xfRecord!=null)
  410. return $xfRecord[$prop];
  411. return "";
  412. }
  413. function align($row,$col,$sheet=0) {
  414. return $this->xfProperty($row,$col,$sheet,'align');
  415. }
  416. function bgColor($row,$col,$sheet=0) {
  417. return $this->xfProperty($row,$col,$sheet,'bgColor');
  418. }
  419. function borderLeft($row,$col,$sheet=0) {
  420. return $this->xfProperty($row,$col,$sheet,'borderLeft');
  421. }
  422. function borderRight($row,$col,$sheet=0) {
  423. return $this->xfProperty($row,$col,$sheet,'borderRight');
  424. }
  425. function borderTop($row,$col,$sheet=0) {
  426. return $this->xfProperty($row,$col,$sheet,'borderTop');
  427. }
  428. function borderBottom($row,$col,$sheet=0) {
  429. return $this->xfProperty($row,$col,$sheet,'borderBottom');
  430. }
  431. function borderLeftColor($row,$col,$sheet=0) {
  432. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderLeftColor')];
  433. }
  434. function borderRightColor($row,$col,$sheet=0) {
  435. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderRightColor')];
  436. }
  437. function borderTopColor($row,$col,$sheet=0) {
  438. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderTopColor')];
  439. }
  440. function borderBottomColor($row,$col,$sheet=0) {
  441. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderBottomColor')];
  442. }
  443. // FONT PROPERTIES
  444. // ===============
  445. function fontRecord($row,$col,$sheet=0) {
  446. $xfRecord = $this->xfRecord($row,$col,$sheet);
  447. if ($xfRecord!=null) {
  448. $font = $xfRecord['fontIndex'];
  449. if ($font!=null)
  450. return $this->fontRecords[$font];
  451. }
  452. return null;
  453. }
  454. function fontProperty($row,$col,$sheet=0,$prop) {
  455. $font = $this->fontRecord($row,$col,$sheet);
  456. if ($font!=null)
  457. return $font[$prop];
  458. return false;
  459. }
  460. function fontIndex($row,$col,$sheet=0) {
  461. return $this->xfProperty($row,$col,$sheet,'fontIndex');
  462. }
  463. function color($row,$col,$sheet=0) {
  464. $formatColor = $this->formatColor($row,$col,$sheet);
  465. if ($formatColor!="")
  466. return $formatColor;
  467. $ci = $this->fontProperty($row,$col,$sheet,'color');
  468. return $this->rawColor($ci);
  469. }
  470. function rawColor($ci) {
  471. if (($ci <> 0x7FFF) && ($ci <> ''))
  472. return $this->colors[$ci];
  473. return "";
  474. }
  475. function bold($row,$col,$sheet=0) {
  476. return $this->fontProperty($row,$col,$sheet,'bold');
  477. }
  478. function italic($row,$col,$sheet=0) {
  479. return $this->fontProperty($row,$col,$sheet,'italic');
  480. }
  481. function underline($row,$col,$sheet=0) {
  482. return $this->fontProperty($row,$col,$sheet,'under');
  483. }
  484. function height($row,$col,$sheet=0) {
  485. return $this->fontProperty($row,$col,$sheet,'height');
  486. }
  487. function font($row,$col,$sheet=0) {
  488. return $this->fontProperty($row,$col,$sheet,'font');
  489. }
  490. // DUMP AN HTML TABLE OF THE ENTIRE XLS DATA
  491. // =========================================
  492. function dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel') {
  493. $out = "<table class=\"$table_class\" cellspacing=0>";
  494. if ($col_letters) {
  495. $out .= "<thead>\n\t<tr>";
  496. if ($row_numbers)
  497. $out .= "\n\t\t<th>&nbsp</th>";
  498. for($i=1;$i<=$this->colcount($sheet);$i++) {
  499. $style = "width:" . ($this->colwidth($i,$sheet)*1) . "px;";
  500. if ($this->colhidden($i,$sheet))
  501. $style .= "display:none;";
  502. $out .= "\n\t\t<th style=\"$style\">" . strtoupper($this->colindexes[$i]) . "</th>";
  503. }
  504. $out .= "</tr></thead>\n";
  505. }
  506. $out .= "<tbody>\n";
  507. for($row=1;$row<=$this->rowcount($sheet);$row++) {
  508. $rowheight = $this->rowheight($row,$sheet);
  509. $style = "height:" . ($rowheight*(4/3)) . "px;";
  510. if ($this->rowhidden($row,$sheet))
  511. $style .= "display:none;";
  512. $out .= "\n\t<tr style=\"$style\">";
  513. if ($row_numbers)
  514. $out .= "\n\t\t<th>$row</th>";
  515. for($col=1;$col<=$this->colcount($sheet);$col++) {
  516. // Account for Rowspans/Colspans
  517. $rowspan = $this->rowspan($row,$col,$sheet);
  518. $colspan = $this->colspan($row,$col,$sheet);
  519. for($i=0;$i<$rowspan;$i++) {
  520. for($j=0;$j<$colspan;$j++) {
  521. if ($i>0 || $j>0)
  522. $this->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
  523. }
  524. }
  525. if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
  526. $style = $this->style($row,$col,$sheet);
  527. if ($this->colhidden($col,$sheet))
  528. $style .= "display:none;";
  529. $out .= "\n\t\t<td style=\"$style\"" . ($colspan > 1?" colspan=$colspan":"") . ($rowspan > 1?" rowspan=$rowspan":"") . ">";
  530. $val = $this->val($row,$col,$sheet);
  531. if ($val=='')
  532. $val="&nbsp;";
  533. else {
  534. $val = htmlentities($val);
  535. $link = $this->hyperlink($row,$col,$sheet);
  536. if ($link!='')
  537. $val = "<a href=\"$link\">$val</a>";
  538. }
  539. $out .= "<nobr>".nl2br($val)."</nobr>";
  540. $out .= "</td>";
  541. }
  542. }
  543. $out .= "</tr>\n";
  544. }
  545. $out .= "</tbody></table>";
  546. return $out;
  547. }
  548. // --------------
  549. // END PUBLIC API
  550. var $boundsheets = array();
  551. var $formatRecords = array();
  552. var $fontRecords = array();
  553. var $xfRecords = array();
  554. var $colInfo = array();
  555. var $rowInfo = array();
  556. var $sst = array();
  557. var $sheets = array();
  558. var $data;
  559. var $_ole;
  560. var $_defaultEncoding = "UTF-8";
  561. var $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT;
  562. var $_columnsFormat = array();
  563. var $_rowoffset = 1;
  564. var $_coloffset = 1;
  565. /**
  566. * List of default date formats used by Excel
  567. */
  568. var $dateFormats = array (
  569. 0xe => "m/d/Y",
  570. 0xf => "M-d-Y",
  571. 0x10 => "d-M",
  572. 0x11 => "M-Y",
  573. 0x12 => "h:i a",
  574. 0x13 => "h:i:s a",
  575. 0x14 => "H:i",
  576. 0x15 => "H:i:s",
  577. 0x16 => "d/m/Y H:i",
  578. 0x2d => "i:s",
  579. 0x2e => "H:i:s",
  580. 0x2f => "i:s.S"
  581. );
  582. /**
  583. * Default number formats used by Excel
  584. */
  585. var $numberFormats = array(
  586. 0x1 => "0",
  587. 0x2 => "0.00",
  588. 0x3 => "#,##0",
  589. 0x4 => "#,##0.00",
  590. 0x5 => "\$#,##0;(\$#,##0)",
  591. 0x6 => "\$#,##0;[Red](\$#,##0)",
  592. 0x7 => "\$#,##0.00;(\$#,##0.00)",
  593. 0x8 => "\$#,##0.00;[Red](\$#,##0.00)",
  594. 0x9 => "0%",
  595. 0xa => "0.00%",
  596. 0xb => "0.00E+00",
  597. 0x25 => "#,##0;(#,##0)",
  598. 0x26 => "#,##0;[Red](#,##0)",
  599. 0x27 => "#,##0.00;(#,##0.00)",
  600. 0x28 => "#,##0.00;[Red](#,##0.00)",
  601. 0x29 => "#,##0;(#,##0)", // Not exactly
  602. 0x2a => "\$#,##0;(\$#,##0)", // Not exactly
  603. 0x2b => "#,##0.00;(#,##0.00)", // Not exactly
  604. 0x2c => "\$#,##0.00;(\$#,##0.00)", // Not exactly
  605. 0x30 => "##0.0E+0"
  606. );
  607. var $colors = Array(
  608. 0x00 => "#000000",
  609. 0x01 => "#FFFFFF",
  610. 0x02 => "#FF0000",
  611. 0x03 => "#00FF00",
  612. 0x04 => "#0000FF",
  613. 0x05 => "#FFFF00",
  614. 0x06 => "#FF00FF",
  615. 0x07 => "#00FFFF",
  616. 0x08 => "#000000",
  617. 0x09 => "#FFFFFF",
  618. 0x0A => "#FF0000",
  619. 0x0B => "#00FF00",
  620. 0x0C => "#0000FF",
  621. 0x0D => "#FFFF00",
  622. 0x0E => "#FF00FF",
  623. 0x0F => "#00FFFF",
  624. 0x10 => "#800000",
  625. 0x11 => "#008000",
  626. 0x12 => "#000080",
  627. 0x13 => "#808000",
  628. 0x14 => "#800080",
  629. 0x15 => "#008080",
  630. 0x16 => "#C0C0C0",
  631. 0x17 => "#808080",
  632. 0x18 => "#9999FF",
  633. 0x19 => "#993366",
  634. 0x1A => "#FFFFCC",
  635. 0x1B => "#CCFFFF",
  636. 0x1C => "#660066",
  637. 0x1D => "#FF8080",
  638. 0x1E => "#0066CC",
  639. 0x1F => "#CCCCFF",
  640. 0x20 => "#000080",
  641. 0x21 => "#FF00FF",
  642. 0x22 => "#FFFF00",
  643. 0x23 => "#00FFFF",
  644. 0x24 => "#800080",
  645. 0x25 => "#800000",
  646. 0x26 => "#008080",
  647. 0x27 => "#0000FF",
  648. 0x28 => "#00CCFF",
  649. 0x29 => "#CCFFFF",
  650. 0x2A => "#CCFFCC",
  651. 0x2B => "#FFFF99",
  652. 0x2C => "#99CCFF",
  653. 0x2D => "#FF99CC",
  654. 0x2E => "#CC99FF",
  655. 0x2F => "#FFCC99",
  656. 0x30 => "#3366FF",
  657. 0x31 => "#33CCCC",
  658. 0x32 => "#99CC00",
  659. 0x33 => "#FFCC00",
  660. 0x34 => "#FF9900",
  661. 0x35 => "#FF6600",
  662. 0x36 => "#666699",
  663. 0x37 => "#969696",
  664. 0x38 => "#003366",
  665. 0x39 => "#339966",
  666. 0x3A => "#003300",
  667. 0x3B => "#333300",
  668. 0x3C => "#993300",
  669. 0x3D => "#993366",
  670. 0x3E => "#333399",
  671. 0x3F => "#333333",
  672. 0x40 => "#000000",
  673. 0x41 => "#FFFFFF",
  674. 0x43 => "#000000",
  675. 0x4D => "#000000",
  676. 0x4E => "#FFFFFF",
  677. 0x4F => "#000000",
  678. 0x50 => "#FFFFFF",
  679. 0x51 => "#000000",
  680. 0x7FFF => "#000000"
  681. );
  682. var $lineStyles = array(
  683. 0x00 => "",
  684. 0x01 => "Thin",
  685. 0x02 => "Medium",
  686. 0x03 => "Dashed",
  687. 0x04 => "Dotted",
  688. 0x05 => "Thick",
  689. 0x06 => "Double",
  690. 0x07 => "Hair",
  691. 0x08 => "Medium dashed",
  692. 0x09 => "Thin dash-dotted",
  693. 0x0A => "Medium dash-dotted",
  694. 0x0B => "Thin dash-dot-dotted",
  695. 0x0C => "Medium dash-dot-dotted",
  696. 0x0D => "Slanted medium dash-dotted"
  697. );
  698. var $lineStylesCss = array(
  699. "Thin" => "1px solid",
  700. "Medium" => "2px solid",
  701. "Dashed" => "1px dashed",
  702. "Dotted" => "1px dotted",
  703. "Thick" => "3px solid",
  704. "Double" => "double",
  705. "Hair" => "1px solid",
  706. "Medium dashed" => "2px dashed",
  707. "Thin dash-dotted" => "1px dashed",
  708. "Medium dash-dotted" => "2px dashed",
  709. "Thin dash-dot-dotted" => "1px dashed",
  710. "Medium dash-dot-dotted" => "2px dashed",
  711. "Slanted medium dash-dotte" => "2px dashed"
  712. );
  713. function read16bitstring($data, $start) {
  714. $len = 0;
  715. while (ord($data[$start + $len]) + ord($data[$start + $len + 1]) > 0) {
  716. $len++;
  717. }
  718. return substr($data, $start, $len);
  719. }
  720. // ADDED by Matt Kruse for better formatting
  721. function _format_value($format,$num,$f) {
  722. // 49==TEXT format
  723. // http://code.google.com/p/php-excel-reader/issues/detail?id=7
  724. if ( (!$f && $format=="%s") || ($f==49) || ($format=="GENERAL") )
  725. return array('string'=>$num, 'formatColor'=>null);
  726. // Custom pattern can be POSITIVE;NEGATIVE;ZERO
  727. // The "text" option as 4th parameter is not handled
  728. $parts = explode(";",$format);
  729. $pattern = $parts[0];
  730. // Negative pattern
  731. if (count($parts)>2 && $num==0)
  732. $pattern = $parts[2];
  733. // Zero pattern
  734. if (count($parts)>1 && $num<0) {
  735. $pattern = $parts[1];
  736. $num = abs($num);
  737. }
  738. $color = "";
  739. $matches = array();
  740. $color_regex = "/^\[(BLACK|BLUE|CYAN|GREEN|MAGENTA|RED|WHITE|YELLOW)\]/i";
  741. if (preg_match($color_regex,$pattern,$matches)) {
  742. $color = strtolower($matches[1]);
  743. $pattern = preg_replace($color_regex,"",$pattern);
  744. }
  745. // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  746. $pattern = preg_replace("/_./","",$pattern);
  747. // Some non-number characters are escaped with \, which we don't need
  748. $pattern = preg_replace("/\\\/","",$pattern);
  749. // Some non-number strings are quoted, so we'll get rid of the quotes
  750. $pattern = preg_replace("/\"/","",$pattern);
  751. // TEMPORARY - Convert # to 0
  752. $pattern = preg_replace("/\#/","0",$pattern);
  753. // Find out if we need comma formatting
  754. $has_commas = preg_match("/,/",$pattern);
  755. if ($has_commas)
  756. $pattern = preg_replace("/,/","",$pattern);
  757. // Handle Percentages
  758. if (preg_match("/\d(\%)([^\%]|$)/",$pattern,$matches)) {
  759. $num = $num * 100;
  760. $pattern = preg_replace("/(\d)(\%)([^\%]|$)/","$1%$3",$pattern);
  761. }
  762. // Handle the number itself
  763. $number_regex = "/(\d+)(\.?)(\d*)/";
  764. if (preg_match($number_regex,$pattern,$matches)) {
  765. $left = $matches[1];
  766. $dec = $matches[2];
  767. $right = $matches[3];
  768. if ($has_commas) {
  769. $formatted = number_format($num,strlen($right));
  770. } else {
  771. $sprintf_pattern = "%1.".strlen($right)."f";
  772. $formatted = sprintf($sprintf_pattern, $num);
  773. }
  774. $pattern = preg_replace($number_regex, $formatted, $pattern);
  775. }
  776. return array(
  777. 'string'=>$pattern,
  778. 'formatColor'=>$color
  779. );
  780. }
  781. /**
  782. * Constructor
  783. *
  784. * Some basic initialisation
  785. */
  786. function Spreadsheet_Excel_Reader($data='',$store_extended_info=false,$outputEncoding='') {
  787. $this->_ole = new OLERead();
  788. $this->setUTFEncoder('iconv');
  789. if ($outputEncoding != '')
  790. $this->setOutputEncoding($outputEncoding);
  791. for ($i=1; $i<245; $i++) {
  792. $name = strtolower(( (($i-1)/26>=1)?chr(($i-1)/26+64):'') . chr(($i-1)%26+65));
  793. $this->colnames[$name] = $i;
  794. $this->colindexes[$i] = $name;
  795. }
  796. $this->store_extended_info = $store_extended_info;
  797. if ($data!="")
  798. $this->read($data);
  799. }
  800. /**
  801. * Set the encoding method
  802. */
  803. function setOutputEncoding($encoding) {
  804. $this->_defaultEncoding = $encoding;
  805. }
  806. /**
  807. * $encoder = 'iconv' or 'mb'
  808. * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
  809. * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
  810. */
  811. function setUTFEncoder($encoder = 'iconv') {
  812. $this->_encoderFunction = '';
  813. if ($encoder == 'iconv')
  814. $this->_encoderFunction = function_exists('iconv') ? 'iconv' : '';
  815. elseif ($encoder == 'mb')
  816. $this->_encoderFunction = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '';
  817. }
  818. function setRowColOffset($iOffset) {
  819. $this->_rowoffset = $iOffset;
  820. $this->_coloffset = $iOffset;
  821. }
  822. /**
  823. * Set the default number format
  824. */
  825. function setDefaultFormat($sFormat) {
  826. $this->_defaultFormat = $sFormat;
  827. }
  828. /**
  829. * Force a column to use a certain format
  830. */
  831. function setColumnFormat($column, $sFormat) {
  832. $this->_columnsFormat[$column] = $sFormat;
  833. }
  834. /**
  835. * Read the spreadsheet file using OLE, then parse
  836. */
  837. function read($data) {
  838. $res = $this->_ole->read($data);
  839. // oops, something goes wrong (Darko Miljanovic)
  840. if($res === false) {
  841. // check error code
  842. if($this->_ole->error == 1)
  843. die( 'Data is not readable' );
  844. elseif($this->_ole->error == 2)
  845. die( 'OLE error' );
  846. // check other error codes here (eg bad fileformat, etc...)
  847. }
  848. $this->data = $this->_ole->getWorkBook();
  849. $this->_parse();
  850. }
  851. /**
  852. * Parse a workbook
  853. *
  854. * @access private
  855. * @return bool
  856. */
  857. function _parse() {
  858. $pos = 0;
  859. $data = $this->data;
  860. $code = $this->v($data,$pos);
  861. $length = $this->v($data,$pos+2);
  862. $version = $this->v($data,$pos+4);
  863. $substreamType = $this->v($data,$pos+6);
  864. $this->version = $version;
  865. if (($version != SPREADSHEET_EXCEL_READER_BIFF8) &&
  866. ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  867. return false;
  868. }
  869. if ($substreamType != SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS)
  870. return false;
  871. $pos += $length + 4;
  872. $code = $this->v($data,$pos);
  873. $length = $this->v($data,$pos+2);
  874. while ($code != SPREADSHEET_EXCEL_READER_TYPE_EOF) {
  875. switch ($code) {
  876. case SPREADSHEET_EXCEL_READER_TYPE_SST:
  877. $spos = $pos + 4;
  878. $limitpos = $spos + $length;
  879. $uniqueStrings = $this->_GetInt4d($data, $spos+4);
  880. $spos += 8;
  881. for ($i = 0; $i < $uniqueStrings; $i++) {
  882. // Read in the number of characters
  883. if ($spos == $limitpos) {
  884. $opcode = $this->v($data,$spos);
  885. $conlength = $this->v($data,$spos+2);
  886. if ($opcode != 0x3c)
  887. return -1;
  888. $spos += 4;
  889. $limitpos = $spos + $conlength;
  890. }
  891. $numChars = ord($data[$spos]) | (ord($data[$spos+1]) << 8);
  892. $spos += 2;
  893. $optionFlags = ord($data[$spos]);
  894. $spos++;
  895. $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  896. $extendedString = ( ($optionFlags & 0x04) != 0);
  897. // See if string contains formatting information
  898. $richString = ( ($optionFlags & 0x08) != 0);
  899. if ($richString) {
  900. // Read in the crun
  901. $formattingRuns = $this->v($data,$spos);
  902. $spos += 2;
  903. }
  904. if ($extendedString) {
  905. // Read in cchExtRst
  906. $extendedRunLength = $this->_GetInt4d($data, $spos);
  907. $spos += 4;
  908. }
  909. $len = ($asciiEncoding)? $numChars : $numChars*2;
  910. if ($spos + $len < $limitpos) {
  911. $retstr = substr($data, $spos, $len);
  912. $spos += $len;
  913. }
  914. else{
  915. // found countinue
  916. $retstr = substr($data, $spos, $limitpos - $spos);
  917. $bytesRead = $limitpos - $spos;
  918. $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  919. $spos = $limitpos;
  920. while ($charsLeft > 0){
  921. $opcode = $this->v($data,$spos);
  922. $conlength = $this->v($data,$spos+2);
  923. if ($opcode != 0x3c)
  924. return -1;
  925. $spos += 4;
  926. $limitpos = $spos + $conlength;
  927. $option = ord($data[$spos]);
  928. $spos += 1;
  929. if ($asciiEncoding && ($option == 0)) {
  930. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  931. $retstr .= substr($data, $spos, $len);
  932. $charsLeft -= $len;
  933. $asciiEncoding = true;
  934. }
  935. elseif (!$asciiEncoding && ($option != 0)) {
  936. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  937. $retstr .= substr($data, $spos, $len);
  938. $charsLeft -= $len/2;
  939. $asciiEncoding = false;
  940. }
  941. elseif (!$asciiEncoding && ($option == 0)) {
  942. // Bummer - the string starts off as Unicode, but after the
  943. // continuation it is in straightforward ASCII encoding
  944. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  945. for ($j = 0; $j < $len; $j++) {
  946. $retstr .= $data[$spos + $j].chr(0);
  947. }
  948. $charsLeft -= $len;
  949. $asciiEncoding = false;
  950. }
  951. else{
  952. $newstr = '';
  953. for ($j = 0; $j < strlen($retstr); $j++) {
  954. $newstr = $retstr[$j].chr(0);
  955. }
  956. $retstr = $newstr;
  957. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  958. $retstr .= substr($data, $spos, $len);
  959. $charsLeft -= $len/2;
  960. $asciiEncoding = false;
  961. }
  962. $spos += $len;
  963. }
  964. }
  965. $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);
  966. if ($richString)
  967. $spos += 4 * $formattingRuns;
  968. // For extended strings, skip over the extended string data
  969. if ($extendedString)
  970. $spos += $extendedRunLength;
  971. $this->sst[]=$retstr;
  972. }
  973. break;
  974. case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS:
  975. return false;
  976. break;
  977. case SPREADSHEET_EXCEL_READER_TYPE_NAME:
  978. break;
  979. case SPREADSHEET_EXCEL_READER_TYPE_FORMAT:
  980. $indexCode = $this->v($data,$pos+4);
  981. if ($version == SPREADSHEET_EXCEL_READER_BIFF8) {
  982. $numchars = $this->v($data,$pos+6);
  983. if (ord($data[$pos+8]) == 0)
  984. $formatString = substr($data, $pos+9, $numchars);
  985. else
  986. $formatString = substr($data, $pos+9, $numchars*2);
  987. } else {
  988. $numchars = ord($data[$pos+6]);
  989. $formatString = substr($data, $pos+7, $numchars*2);
  990. }
  991. $this->formatRecords[$indexCode] = $formatString;
  992. break;
  993. case SPREADSHEET_EXCEL_READER_TYPE_FONT:
  994. $height = $this->v($data,$pos+4);
  995. $option = $this->v($data,$pos+6);
  996. $color = $this->v($data,$pos+8);
  997. $weight = $this->v($data,$pos+10);
  998. $under = ord($data[$pos+14]);
  999. $font = "";
  1000. // Font name
  1001. $numchars = ord($data[$pos+18]);
  1002. if ((ord($data[$pos+19]) & 1) == 0){
  1003. $font = substr($data, $pos+20, $numchars);
  1004. } else {
  1005. $font = substr($data, $pos+20, $numchars*2);
  1006. $font = $this->_encodeUTF16($font);
  1007. }
  1008. $this->fontRecords[] = array(
  1009. 'height' => $height / 20,
  1010. 'italic' => !!($option & 2),
  1011. 'color' => $color,
  1012. 'under' => !($under==0),
  1013. 'bold' => ($weight==700),
  1014. 'font' => $font,
  1015. 'raw' => $this->dumpHexData($data, $pos+3, $length)
  1016. );
  1017. break;
  1018. case SPREADSHEET_EXCEL_READER_TYPE_PALETTE:
  1019. $colors = ord($data[$pos+4]) | ord($data[$pos+5]) << 8;
  1020. for ($coli = 0; $coli < $colors; $coli++) {
  1021. $colOff = $pos + 2 + ($coli * 4);
  1022. $colr = ord($data[$colOff]);
  1023. $colg = ord($data[$colOff+1]);
  1024. $colb = ord($data[$colOff+2]);
  1025. $this->colors[0x07 + $coli] = '#' . $this->myhex($colr) . $this->myhex($colg) . $this->myhex($colb);
  1026. }
  1027. break;
  1028. case SPREADSHEET_EXCEL_READER_TYPE_XF:
  1029. $fontIndexCode = (ord($data[$pos+4]) | ord($data[$pos+5]) << 8) - 1;
  1030. $fontIndexCode = max(0,$fontIndexCode);
  1031. $indexCode = ord($data[$pos+6]) | ord($data[$pos+7]) << 8;
  1032. $alignbit = ord($data[$pos+10]) & 3;
  1033. $bgi = (ord($data[$pos+22]) | ord($data[$pos+23]) << 8) & 0x3FFF;
  1034. $bgcolor = ($bgi & 0x7F);
  1035. // $bgcolor = ($bgi & 0x3f80) >> 7;
  1036. $align = "";
  1037. if ($alignbit==3)
  1038. $align="right";
  1039. if ($alignbit==2)
  1040. $align="center";
  1041. $fillPattern = (ord($data[$pos+21]) & 0xFC) >> 2;
  1042. if ($fillPattern == 0)
  1043. $bgcolor = "";
  1044. $xf = array();
  1045. $xf['formatIndex'] = $indexCode;
  1046. $xf['align'] = $align;
  1047. $xf['fontIndex'] = $fontIndexCode;
  1048. $xf['bgColor'] = $bgcolor;
  1049. $xf['fillPattern'] = $fillPattern;
  1050. $border = ord($data[$pos+14]) | (ord($data[$pos+15]) << 8) | (ord($data[$pos+16]) << 16) | (ord($data[$pos+17]) << 24);
  1051. $xf['borderLeft'] = $this->lineStyles[($border & 0xF)];
  1052. $xf['borderRight'] = $this->lineStyles[($border & 0xF0) >> 4];
  1053. $xf['borderTop'] = $this->lineStyles[($border & 0xF00) >> 8];
  1054. $xf['borderBottom'] = $this->lineStyles[($border & 0xF000) >> 12];
  1055. $xf['borderLeftColor'] = ($border & 0x7F0000) >> 16;
  1056. $xf['borderRightColor'] = ($border & 0x3F800000) >> 23;
  1057. $border = (ord($data[$pos+18]) | ord($data[$pos+19]) << 8);
  1058. $xf['borderTopColor'] = ($border & 0x7F);
  1059. $xf['borderBottomColor'] = ($border & 0x3F80) >> 7;
  1060. if (array_key_exists($indexCode, $this->dateFormats)) {
  1061. $xf['type'] = 'date';
  1062. $xf['format'] = $this->dateFormats[$indexCode];
  1063. if ($align=='')
  1064. $xf['align'] = 'right';
  1065. }elseif (array_key_exists($indexCode, $this->numberFormats)) {
  1066. $xf['type'] = 'number';
  1067. $xf['format'] = $this->numberFormats[$indexCode];
  1068. if ($align=='')
  1069. $xf['align'] = 'right';
  1070. }else{
  1071. $isdate = FALSE;
  1072. $formatstr = '';
  1073. if ($indexCode > 0){
  1074. if (isset($this->formatRecords[$indexCode]))
  1075. $formatstr = $this->formatRecords[$indexCode];
  1076. if ($formatstr!="") {
  1077. $tmp = preg_replace("/\;.*/","",$formatstr);
  1078. $tmp = preg_replace("/^\[[^\]]*\]/","",$tmp);
  1079. if (preg_match("/[^hmsday\/\-:\s\\\,AMP]/i", $tmp) == 0) { // found day and time format
  1080. $isdate = TRUE;
  1081. $formatstr = $tmp;
  1082. $formatstr = str_replace(array('AM/PM','mmmm','mmm'), array('a','F','M'), $formatstr);
  1083. // m/mm are used for both minutes and months - oh SNAP!
  1084. // This mess tries to fix for that.
  1085. // 'm' == minutes only if following h/hh or preceding s/ss
  1086. $formatstr = preg_replace("/(h:?)mm?/","$1i", $formatstr);
  1087. $formatstr = preg_replace("/mm?(:?s)/","i$1", $formatstr);
  1088. // A single 'm' = n in PHP
  1089. $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1090. $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1091. // else it's months
  1092. $formatstr = str_replace('mm', 'm', $formatstr);
  1093. // Convert single 'd' to 'j'
  1094. $formatstr = preg_replace("/(^|[^d])d([^d]|$)/", '$1j$2', $formatstr);
  1095. $formatstr = str_replace(array('dddd','ddd','dd','yyyy','yy','hh','h'), array('l','D','d','Y','y','H','g'), $formatstr);
  1096. $formatstr = preg_replace("/ss?/", 's', $formatstr);
  1097. }
  1098. }
  1099. }
  1100. if ($isdate){
  1101. $xf['type'] = 'date';
  1102. $xf['format'] = $formatstr;
  1103. if ($align=='')
  1104. $xf['align'] = 'right';
  1105. }else{
  1106. // If the format string has a 0 or # in it, we'll assume it's a number
  1107. if (preg_match("/[0#]/", $formatstr)) {
  1108. $xf['type'] = 'number';
  1109. if ($align=='')
  1110. $xf['align']='right';
  1111. } else {
  1112. $xf['type'] = 'other';
  1113. }
  1114. $xf['format'] = $formatstr;
  1115. $xf['code'] = $indexCode;
  1116. }
  1117. }
  1118. $this->xfRecords[] = $xf;
  1119. break;
  1120. case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR:
  1121. $this->nineteenFour = (ord($data[$pos+4]) == 1);
  1122. break;
  1123. case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET:
  1124. $rec_offset = $this->_GetInt4d($data, $pos+4);
  1125. $rec_typeFlag = ord($data[$pos+8]);
  1126. $rec_visibilityFlag = ord($data[$pos+9]);
  1127. $rec_length = ord($data[$pos+10]);
  1128. if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1129. $chartype = ord($data[$pos+11]);
  1130. if ($chartype == 0)
  1131. $rec_name = substr($data, $pos+12, $rec_length);
  1132. else
  1133. $rec_name = $this->_encodeUTF16(substr($data, $pos+12, $rec_length*2));
  1134. } elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1135. $rec_name = substr($data, $pos+11, $rec_length);
  1136. }
  1137. $this->boundsheets[] = array('name'=>$rec_name,'offset'=>$rec_offset);
  1138. break;
  1139. }
  1140. $pos += $length + 4;
  1141. $code = ord($data[$pos]) | ord($data[$pos+1])<<8;
  1142. $length = ord($data[$pos+2]) | ord($data[$pos+3])<<8;
  1143. }
  1144. foreach ($this->boundsheets as $key=>$val){
  1145. $this->sn = $key;
  1146. $this->_parsesheet($val['offset']);
  1147. }
  1148. return true;
  1149. }
  1150. /**
  1151. * Parse a worksheet
  1152. */
  1153. function _parsesheet($spos) {
  1154. $cont = true;
  1155. $data = $this->data;
  1156. // read BOF
  1157. $code = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1158. $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1159. $version = ord($data[$spos + 4]) | ord($data[$spos + 5])<<8;
  1160. $substreamType = ord($data[$spos + 6]) | ord($data[$spos + 7])<<8;
  1161. if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && ($version != SPREADSHEET_EXCEL_READER_BIFF7))
  1162. return -1;
  1163. if ($substreamType != SPREADSHEET_EXCEL_READER_WORKSHEET)
  1164. return -2;
  1165. $spos += $length + 4;
  1166. while($cont) {
  1167. $lowcode = ord($data[$spos]);
  1168. if ($lowcode == SPREADSHEET_EXCEL_READER_TYPE_EOF)
  1169. break;
  1170. $code = $lowcode | ord($data[$spos+1])<<8;
  1171. $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1172. $spos += 4;
  1173. $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
  1174. $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
  1175. unset($this->rectype);
  1176. switch ($code) {
  1177. case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION:
  1178. if (!isset($this->numRows)) {
  1179. if (($length == 10) || ($version == SPREADSHEET_EXCEL_READER_BIFF7)){
  1180. $this->sheets[$this->sn]['numRows'] = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1181. $this->sheets[$this->sn]['numCols'] = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1182. } else {
  1183. $this->sheets[$this->sn]['numRows'] = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1184. $this->sheets[$this->sn]['numCols'] = ord($data[$spos+10]) | ord($data[$spos+11]) << 8;
  1185. }
  1186. }
  1187. break;
  1188. case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS:
  1189. $cellRanges = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1190. for ($i = 0; $i < $cellRanges; $i++) {
  1191. $fr = ord($data[$spos + 8*$i + 2]) | ord($data[$spos + 8*$i + 3])<<8;
  1192. $lr = ord($data[$spos + 8*$i + 4]) | ord($data[$spos + 8*$i + 5])<<8;
  1193. $fc = ord($data[$spos + 8*$i + 6]) | ord($data[$spos + 8*$i + 7])<<8;
  1194. $lc = ord($data[$spos + 8*$i + 8]) | ord($data[$spos + 8*$i + 9])<<8;
  1195. if ($lr - $fr > 0)
  1196. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
  1197. if ($lc - $fc > 0)
  1198. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
  1199. }
  1200. break;
  1201. case SPREADSHEET_EXCEL_READER_TYPE_RK:
  1202. case SPREADSHEET_EXCEL_READER_TYPE_RK2:
  1203. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1204. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1205. $rknum = $this->_GetInt4d($data, $spos + 6);
  1206. $numValue = $this->_GetIEEE754($rknum);
  1207. $info = $this->_getCellDetails($spos,$numValue,$column);
  1208. $this->addcell($row, $column, $info['string'],$info);
  1209. break;
  1210. case SPREADSHEET_EXCEL_READER_TYPE_LABELSST:
  1211. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1212. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1213. $xfindex = ord($data[$spos+4]) | ord($data[$spos+5])<<8;
  1214. $index = $this->_GetInt4d($data, $spos + 6);
  1215. $this->addcell($row, $column, $this->sst[$index], array('xfIndex'=>$xfindex) );
  1216. break;
  1217. case SPREADSHEET_EXCEL_READER_TYPE_MULRK:
  1218. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1219. $colFirst = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1220. $colLast = ord($data[$spos + $length - 2]) | ord($data[$spos + $length - 1])<<8;
  1221. $columns = $colLast - $colFirst + 1;
  1222. $tmppos = $spos+4;
  1223. for ($i = 0; $i < $columns; $i++) {
  1224. $numValue = $this->_GetIEEE754($this->_GetInt4d($data, $tmppos + 2));
  1225. $info = $this->_getCellDetails($tmppos-4,$numValue,$colFirst + $i + 1);
  1226. $tmppos += 6;
  1227. $this->addcell($row, $colFirst + $i, $info['string'], $info);
  1228. }
  1229. break;
  1230. case SPREADSHEET_EXCEL_READER_TYPE_NUMBER:
  1231. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1232. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1233. $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1234. if ($this->isDate($spos))
  1235. $numValue = $tmp['double'];
  1236. else
  1237. $numValue = $this->createNumber($spos);
  1238. $info = $this->_getCellDetails($spos,$numValue,$column);
  1239. $this->addcell($row, $column, $info['string'], $info);
  1240. break;
  1241. case SPREADSHEET_EXCEL_READER_TYPE_FORMULA:
  1242. case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2:
  1243. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1244. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1245. if ((ord($data[$spos+6])==0) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1246. //String formula. Result follows in a STRING record
  1247. // This row/col are stored to be referenced in that record
  1248. // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1249. $previousRow = $row;
  1250. $previousCol = $column;
  1251. } elseif ((ord($data[$spos+6])==1) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1252. //Boolean formula. Result is in +2; 0=false,1=true
  1253. // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1254. if (ord($this->data[$spos+8])==1)
  1255. $this->addcell($row, $column, "TRUE");
  1256. else
  1257. $this->addcell($row, $column, "FALSE");
  1258. } elseif ((ord($data[$spos+6])==2) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1259. //Error formula. Error code is in +2;
  1260. } elseif ((ord($data[$spos+6])==3) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1261. //Formula result is a null string.
  1262. $this->addcell($row, $column, '');
  1263. } else {
  1264. // result is a number, so first 14 bytes are just like a _NUMBER record
  1265. $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1266. if ($this->isDate($spos))
  1267. $numValue = $tmp['double'];
  1268. else
  1269. $numValue = $this->createNumber($spos);
  1270. $info = $this->_getCellDetails($spos,$numValue,$column);
  1271. $this->addcell($row, $column, $info['string'], $info);
  1272. }
  1273. break;
  1274. case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR:
  1275. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1276. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1277. $string = ord($data[$spos+6]);
  1278. $this->addcell($row, $column, $string);
  1279. break;
  1280. case SPREADSHEET_EXCEL_READER_TYPE_STRING:
  1281. // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1282. if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1283. // Unicode 16 string, like an SST record
  1284. $xpos = $spos;
  1285. $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1286. $xpos += 2;
  1287. $optionFlags =ord($data[$xpos]);
  1288. $xpos++;
  1289. $asciiEncoding = (($optionFlags &0x01) == 0) ;
  1290. $extendedString = (($optionFlags & 0x04) != 0);
  1291. // See if string contains formatting information
  1292. $richString = (($optionFlags & 0x08) != 0);
  1293. if ($richString) {
  1294. // Read in the crun
  1295. $formattingRuns =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1296. $xpos += 2;
  1297. }
  1298. if ($extendedString) {
  1299. // Read in cchExtRst
  1300. $extendedRunLength =$this->_GetInt4d($this->data, $xpos);
  1301. $xpos += 4;
  1302. }
  1303. $len = ($asciiEncoding)?$numChars : $numChars*2;
  1304. $retstr =substr($data, $xpos, $len);
  1305. $xpos += $len;
  1306. $retstr = ($asciiEncoding)? $retstr : $this->_encodeUTF16($retstr);
  1307. }
  1308. elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1309. // Simple byte string
  1310. $xpos = $spos;
  1311. $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1312. $xpos += 2;
  1313. $retstr =substr($data, $xpos, $numChars);
  1314. }
  1315. $this->addcell($previousRow, $previousCol, $retstr);
  1316. break;
  1317. case SPREADSHEET_EXCEL_READER_TYPE_ROW:
  1318. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1319. $rowInfo = ord($data[$spos + 6]) | ((ord($data[$spos+7]) << 8) & 0x7FFF);
  1320. if (($rowInfo & 0x8000) > 0)
  1321. $rowHeight = -1;
  1322. else
  1323. $rowHeight = $rowInfo & 0x7FFF;
  1324. $rowHidden = (ord($data[$spos + 12]) & 0x20) >> 5;
  1325. $this->rowInfo[$this->sn][$row+1] = array('height' => $rowHeight / 20, 'hidden'=>$rowHidden );
  1326. break;
  1327. case SPREADSHEET_EXCEL_READER_TYPE_DBCELL:
  1328. break;
  1329. case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK:
  1330. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1331. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1332. $cols = ($length / 2) - 3;
  1333. for ($c = 0; $c < $cols; $c++) {
  1334. $xfindex = ord($data[$spos + 4 + ($c * 2)]) | ord($data[$spos + 5 + ($c * 2)])<<8;
  1335. $this->addcell($row, $column + $c, "", array('xfIndex'=>$xfindex));
  1336. }
  1337. break;
  1338. case SPREADSHEET_EXCEL_READER_TYPE_LABEL:
  1339. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1340. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1341. $this->addcell($row, $column, substr($data, $spos + 8, ord($data[$spos + 6]) | ord($data[$spos + 7])<<8));
  1342. break;
  1343. case SPREADSHEET_EXCEL_READER_TYPE_EOF:
  1344. $cont = false;
  1345. break;
  1346. case SPREADSHEET_EXCEL_READER_TYPE_HYPER:
  1347. // Only handle hyperlinks to a URL
  1348. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  1349. $row2 = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  1350. $column = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8;
  1351. $column2 = ord($this->data[$spos+6]) | ord($this->data[$spos+7])<<8;
  1352. $linkdata = array();
  1353. $flags = ord($this->data[$spos + 28]);
  1354. $udesc = "";
  1355. $ulink = "";
  1356. $uloc = 32;
  1357. $linkdata['flags'] = $flags;
  1358. if (($flags & 1) > 0 ) { // is a type we understand
  1359. // is there a description ?
  1360. if (($flags & 0x14) == 0x14 ) { // has a description
  1361. $uloc += 4;
  1362. $descLen = ord($this->data[$spos + 32]) | ord($this->data[$spos + 33]) << 8;
  1363. $udesc = substr($this->data, $spos + $uloc, $descLen * 2);
  1364. $uloc += 2 * $descLen;
  1365. }
  1366. $ulink = $this->read16bitstring($this->data, $spos + $uloc + 20);
  1367. if ($udesc == "")
  1368. $udesc = $ulink;
  1369. }
  1370. $linkdata['desc'] = $udesc;
  1371. $linkdata['link'] = $this->_encodeUTF16($ulink);
  1372. for ($r=$row; $r<=$row2; $r++) {
  1373. for ($c=$column; $c<=$column2; $c++) {
  1374. $this->sheets[$this->sn]['cellsInfo'][$r+1][$c+1]['hyperlink'] = $linkdata;
  1375. }
  1376. }
  1377. break;
  1378. case SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH:
  1379. $this->defaultColWidth = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1380. break;
  1381. case SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH:
  1382. $this->standardColWidth = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1383. break;
  1384. case SPREADSHEET_EXCEL_READER_TYPE_COLINFO:
  1385. $colfrom = ord($data[$spos+0]) | ord($data[$spos+1]) << 8;
  1386. $colto = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1387. $cw = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1388. $cxf = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1389. $co = ord($data[$spos+8]);
  1390. for ($coli = $colfrom; $coli <= $colto; $coli++) {
  1391. $this->colInfo[$this->sn][$coli+1] = Array('width' => $cw, 'xf' => $cxf, 'hidden' => ($co & 0x01), 'collapsed' => ($co & 0x1000) >> 12);
  1392. }
  1393. break;
  1394. default:
  1395. break;
  1396. }
  1397. $spos += $length;
  1398. }
  1399. if (!isset($this->sheets[$this->sn]['numRows']))
  1400. $th

Large files files are truncated, but you can click here to view the full file