excel_reader2.php 58 KB

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