PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/vendors/php_excel_reader/excel_reader.php

http://scp-soft.googlecode.com/
PHP | 1610 lines | 1458 code | 58 blank | 94 comment | 147 complexity | e81ebb635cdbf288e19c7313a601bfd6 MD5 | raw file
Possible License(s): GPL-2.0

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

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

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