PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/libraries/excel_import.class.php

https://github.com/sahilbabu/phpb2b
PHP | 1689 lines | 1581 code | 61 blank | 47 comment | 108 complexity | afe9010a43e1a9ab97bd15ac20910f0f MD5 | raw file
Possible License(s): LGPL-2.1

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

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

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