You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1327 lines
51 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. namespace Shuchkin;
  3. /*
  4. * Example
  5. echo '<pre>';
  6. if ( $xls = SimpleXLS::parse('excel5book.xls')) {
  7. print_r( $xls->rows() ); // dump first sheet
  8. print_r( $xls->rows(1)); /// dump second sheet
  9. } else {
  10. echo SimpleXLS::parseError();
  11. }
  12. echo '</pre>';
  13. */
  14. /**
  15. * A class for reading Microsoft Excel Spreadsheets.
  16. *
  17. * SimpleXLS version 2016-2020 packaged by Sergey Shuchkin <sergey.shuchkin@gmail.com> from
  18. * Spreadsheet_Excel_Reader class developed by Vadim Tkachenko <vt@phpapache.com>
  19. */
  20. class SimpleXLS
  21. {
  22. public const BIFF8 = 0x600;
  23. public const BIFF7 = 0x500;
  24. public const WORKBOOKGLOBALS = 0x5;
  25. public const WORKSHEET = 0x10;
  26. //const TYPE_BOF = 0x809;
  27. public const TYPE_EOF = 0x0a;
  28. public const TYPE_BOUNDSHEET = 0x85;
  29. public const TYPE_DIMENSION = 0x200;
  30. public const TYPE_ROW = 0x208;
  31. public const TYPE_DBCELL = 0xd7;
  32. public const TYPE_FILEPASS = 0x2f;
  33. //const TYPE_NOTE = 0x1c;
  34. //const TYPE_TXO = 0x1b6;
  35. public const TYPE_RK = 0x7e;
  36. public const TYPE_RK2 = 0x27e;
  37. public const TYPE_MULRK = 0xbd;
  38. public const TYPE_MULBLANK = 0xbe;
  39. //const TYPE_INDEX = 0x20b;
  40. public const TYPE_SST = 0xfc;
  41. //const TYPE_EXTSST = 0xff;
  42. //const TYPE_CONTINUE = 0x3c;
  43. public const TYPE_LABEL = 0x204;
  44. public const TYPE_LABELSST = 0xfd;
  45. public const TYPE_NUMBER = 0x203;
  46. public const TYPE_NAME = 0x18;
  47. //const TYPE_ARRAY = 0x221;
  48. //const TYPE_STRING = 0x207;
  49. public const TYPE_FORMULA = 0x406;
  50. public const TYPE_FORMULA2 = 0x6;
  51. public const TYPE_FORMAT = 0x41e;
  52. public const TYPE_XF = 0xe0;
  53. public const TYPE_BOOLERR = 0x205;
  54. //const TYPE_UNKNOWN = 0xffff;
  55. public const TYPE_NINETEENFOUR = 0x22;
  56. public const TYPE_MERGEDCELLS = 0xE5;
  57. public const TYPE_WINDOW1 = 0x3D;
  58. //const DEF_NUM_FORMAT = "%.2f";
  59. public const DEF_NUM_FORMAT = '%s';
  60. // OLE
  61. public const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c;
  62. public const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c;
  63. public const ROOT_START_BLOCK_POS = 0x30;
  64. public const BIG_BLOCK_SIZE = 0x200;
  65. public const SMALL_BLOCK_SIZE = 0x40;
  66. public const EXTENSION_BLOCK_POS = 0x44;
  67. public const NUM_EXTENSION_BLOCK_POS = 0x48;
  68. public const PROPERTY_STORAGE_BLOCK_SIZE = 0x80;
  69. public const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c;
  70. public const SMALL_BLOCK_THRESHOLD = 0x1000;
  71. // property storage offsets
  72. public const SIZE_OF_NAME_POS = 0x40;
  73. public const TYPE_POS = 0x42;
  74. public const START_BLOCK_POS = 0x74;
  75. public const SIZE_POS = 0x78;
  76. /**
  77. * Array of worksheets found
  78. *
  79. * @var array
  80. * @access public
  81. */
  82. public $boundsheets = array();
  83. public $activeSheet = 0;
  84. /**
  85. * Array of format records found
  86. *
  87. * @var array
  88. * @access public
  89. */
  90. public $formatRecords = array();
  91. /**
  92. *
  93. * @var array
  94. * @access public
  95. */
  96. public $sst = array();
  97. /**
  98. * Array of worksheets
  99. *
  100. * The data is stored in 'cells' and the meta-data is stored in an array
  101. * called 'cellsInfo'
  102. *
  103. * Example:
  104. *
  105. * $sheets --> 'cells' --> row --> column --> Interpreted value
  106. * --> 'cellsInfo' --> row --> column --> 'type' - Can be 'date', 'number', or 'unknown'
  107. * --> 'raw' - The raw data that Excel stores for that data cell
  108. *
  109. * @var array
  110. * @access public
  111. */
  112. public $sheets = array();
  113. /**
  114. * List of default date formats used by Excel
  115. *
  116. * @var array
  117. * @access public
  118. */
  119. public $dateFormats = array(
  120. 0xe => 'd/m/Y',
  121. 0xf => 'd-M-Y',
  122. 0x10 => 'd-M',
  123. 0x11 => 'M-Y',
  124. 0x12 => 'h:i a',
  125. 0x13 => 'h:i:s a',
  126. 0x14 => 'H:i',
  127. 0x15 => 'H:i:s',
  128. 0x16 => 'd/m/Y H:i',
  129. 0x2d => 'i:s',
  130. 0x2e => 'H:i:s',
  131. 0x2f => 'i:s.S'
  132. );
  133. /**
  134. * Default number formats used by Excel
  135. *
  136. * @var array
  137. * @access public
  138. */
  139. public $numberFormats = array(
  140. 0x1 => '%1.0f', // "0"
  141. 0x2 => '%1.2f', // "0.00",
  142. 0x3 => '%1.0f', //"#,##0",
  143. 0x4 => '%1.2f', //"#,##0.00",
  144. 0x5 => '%1.0f', /*"$#,##0;($#,##0)",*/
  145. 0x6 => '$%1.0f', /*"$#,##0;($#,##0)",*/
  146. 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  147. 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  148. 0x9 => '%1.0f%%', // "0%"
  149. 0xa => '%1.2f%%', // "0.00%"
  150. 0xb => '%1.2f', // 0.00E00",
  151. 0x25 => '%1.0f', // "#,##0;(#,##0)",
  152. 0x26 => '%1.0f', //"#,##0;(#,##0)",
  153. 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)",
  154. 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)",
  155. 0x29 => '%1.0f', //"#,##0;(#,##0)",
  156. 0x2a => '$%1.0f', //"$#,##0;($#,##0)",
  157. 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)",
  158. 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  159. 0x30 => '%1.0f'
  160. );
  161. protected $datetimeFormat = 'Y-m-d H:i:s';
  162. /**
  163. * Default encoding
  164. *
  165. * @var string
  166. * @access private
  167. */
  168. protected $defaultEncoding = 'UTF-8';
  169. /**
  170. * Default number format
  171. *
  172. * @var integer
  173. * @access private
  174. */
  175. protected $defaultFormat = self::DEF_NUM_FORMAT;
  176. /**
  177. * List of formats to use for each column
  178. *
  179. * @var array
  180. * @access private
  181. */
  182. protected $columnsFormat = array();
  183. protected $nineteenFour;
  184. protected $multiplier;
  185. protected $sn;
  186. protected $curFormat;
  187. // OLERead
  188. protected $data;
  189. protected $bigBlockChain;
  190. protected $smallBlockChain;
  191. protected $rootEntry;
  192. protected $entry;
  193. protected $props;
  194. // sergey.shuchkin@gmail.com
  195. protected $wrkbook; // false - to use excel format
  196. protected $error = false;
  197. protected $debug;
  198. // {{{ Spreadsheet_Excel_Reader()
  199. /**
  200. * Constructor
  201. *
  202. * @param string $filename XLS Filename or xls contents
  203. * @param bool $isData If True then $filename is contents
  204. * @param bool $debug Trigger PHP errors?
  205. */
  206. public function __construct(string $filename, bool $isData = false, bool $debug = false)
  207. {
  208. $this->debug = $debug;
  209. $this->_oleread($filename, $isData);
  210. $this->_parse();
  211. }
  212. public static function parseFile($filename, $debug = false)
  213. {
  214. return self::parse($filename, false, $debug);
  215. }
  216. public static function parseData($data, $debug = false)
  217. {
  218. return self::parse($data, true, $debug);
  219. }
  220. public static function parse($filename, $isData = false, $debug = false)
  221. {
  222. $xls = new self($filename, $isData, $debug);
  223. if ($xls->success()) {
  224. return $xls;
  225. }
  226. self::parseError($xls->error());
  227. return false;
  228. }
  229. public static function parseError($set = false)
  230. {
  231. static $error = false;
  232. return $set ? $error = $set : $error;
  233. }
  234. public function error($set = false)
  235. {
  236. if ($set) {
  237. $this->error = $set;
  238. if ($this->debug) {
  239. trigger_error($set);
  240. }
  241. }
  242. return $this->error;
  243. }
  244. public function success(): bool
  245. {
  246. return ! $this->error;
  247. }
  248. public function rows($worksheetIndex = 0, $limit = 0)
  249. {
  250. return iterator_to_array($this->readRows($worksheetIndex, $limit), false);
  251. }
  252. /**
  253. * @param $sheetNum
  254. * @param $limit
  255. * @return \Generator
  256. */
  257. public function readRows($sheetNum = 0, $limit = 0)
  258. {
  259. if ($this->sheets[ $sheetNum ]) {
  260. $s = $this->sheets[ $sheetNum ];
  261. for ($i = 0; $i < $s['numRows']; $i ++) {
  262. $r = array();
  263. for ($j = 0; $j < $s['numCols']; $j ++) {
  264. $r[ $j ] = $s['cells'][$i][$j] ?? '';
  265. }
  266. yield $r;
  267. $limit--;
  268. if ($limit === 0) {
  269. break;
  270. }
  271. }
  272. }
  273. return;
  274. }
  275. public function rowsEx($sheetNum = 0, $limit = 0): array
  276. {
  277. if ($this->sheets[ $sheetNum ]) {
  278. $s = $this->sheets[ $sheetNum ];
  279. $result = array();
  280. for ($i = 0; $i < $s['numRows']; $i ++) {
  281. $r = array();
  282. for ($j = 0; $j < $s['numCols']; $j ++) {
  283. $v = $s['cellsInfo'][$i][$j] ?? array();
  284. // if ( $v['type'] === self::TYPE_RK || $v['type'] === self::TYPE_RK2 ||
  285. $v['value'] = $s['cells'][$i][$j] ?? '';
  286. $r[ $j ] = $v;
  287. }
  288. $result[] = $r;
  289. $limit--;
  290. if ($limit === 0) {
  291. break;
  292. }
  293. }
  294. return $result;
  295. }
  296. return [];
  297. }
  298. public function toHTML($worksheetIndex = 0): string
  299. {
  300. $s = '<table class=excel>';
  301. foreach ($this->rows($worksheetIndex) as $r) {
  302. $s .= '<tr>';
  303. foreach ($r as $c) {
  304. $s .= '<td nowrap>' . ( $c === '' ? '&nbsp' : htmlspecialchars($c, ENT_QUOTES) ) . '</td>';
  305. }
  306. $s .= "</tr>\r\n";
  307. }
  308. $s .= '</table>';
  309. return $s;
  310. }
  311. public function setDateTimeFormat($value): SimpleXLS
  312. {
  313. $this->datetimeFormat = is_string($value) ? $value : false;
  314. return $this;
  315. }
  316. public function sheetNames(): array
  317. {
  318. $result = array();
  319. foreach ($this->boundsheets as $k => $v) {
  320. $result[ $k ] = $v['name'];
  321. }
  322. return $result;
  323. }
  324. public function sheetName($index)
  325. {
  326. return isset($this->boundsheets[ $index ]) ? $this->boundsheets[ $index ]['name'] : null;
  327. }
  328. // }}}
  329. protected function _oleread($sFileName, $isData = false): bool
  330. {
  331. if ($isData) {
  332. $this->data = $sFileName;
  333. } else {
  334. // check if file exist and is readable (Darko Miljanovic)
  335. if (! is_readable($sFileName)) {
  336. $this->error('File not is readable ' . $sFileName);
  337. return false;
  338. }
  339. $this->data = file_get_contents($sFileName);
  340. if (! $this->data) {
  341. $this->error('File reading error ' . $sFileName);
  342. return false;
  343. }
  344. }
  345. //echo IDENTIFIER_OLE;
  346. //echo 'start';
  347. if (strpos($this->data, pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1)) !== 0) {
  348. $this->error('File is not XLS');
  349. return false;
  350. }
  351. $numBigBlockDepotBlocks = $this->_getInt4d(self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  352. $sbdStartBlock = $this->_getInt4d(self::SMALL_BLOCK_DEPOT_BLOCK_POS);
  353. $rootStartBlock = $this->_getInt4d(self::ROOT_START_BLOCK_POS);
  354. $extensionBlock = $this->_getInt4d(self::EXTENSION_BLOCK_POS);
  355. $numExtensionBlocks = $this->_getInt4d(self::NUM_EXTENSION_BLOCK_POS);
  356. /*
  357. echo $this->numBigBlockDepotBlocks." ";
  358. echo $this->sbdStartBlock." ";
  359. echo $this->rootStartBlock." ";
  360. echo $this->extensionBlock." ";
  361. echo $this->numExtensionBlocks." ";
  362. */
  363. //echo "sbdStartBlock = $this->sbdStartBlock\n";
  364. $bigBlockDepotBlocks = array();
  365. $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS;
  366. // echo "pos = $pos";
  367. $bbdBlocks = $numBigBlockDepotBlocks;
  368. if ($numExtensionBlocks !== 0) {
  369. $bbdBlocks = ( self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS ) / 4;
  370. }
  371. for ($i = 0; $i < $bbdBlocks; $i ++) {
  372. $bigBlockDepotBlocks[ $i ] = $this->_getInt4d($pos);
  373. $pos += 4;
  374. }
  375. for ($j = 0; $j < $numExtensionBlocks; $j ++) {
  376. $pos = ( $extensionBlock + 1 ) * self::BIG_BLOCK_SIZE;
  377. $blocksToRead = min($numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1);
  378. for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i ++) {
  379. $bigBlockDepotBlocks[ $i ] = $this->_getInt4d($pos);
  380. $pos += 4;
  381. }
  382. $bbdBlocks += $blocksToRead;
  383. if ($bbdBlocks < $numBigBlockDepotBlocks) {
  384. $extensionBlock = $this->_getInt4d($pos);
  385. }
  386. }
  387. // var_dump($bigBlockDepotBlocks);
  388. // readBigBlockDepot
  389. $index = 0;
  390. $this->bigBlockChain = array();
  391. for ($i = 0; $i < $numBigBlockDepotBlocks; $i ++) {
  392. $pos = ( $bigBlockDepotBlocks[ $i ] + 1 ) * self::BIG_BLOCK_SIZE;
  393. //echo "pos = $pos";
  394. for ($j = 0; $j < self::BIG_BLOCK_SIZE / 4; $j ++) {
  395. $this->bigBlockChain[ $index ] = $this->_getInt4d($pos);
  396. $pos += 4;
  397. $index ++;
  398. }
  399. }
  400. //var_dump($this->bigBlockChain);
  401. //echo '=====2';
  402. // readSmallBlockDepot();
  403. $index = 0;
  404. $sbdBlock = $sbdStartBlock;
  405. $this->smallBlockChain = array();
  406. while ($sbdBlock !== - 2) {
  407. $pos = ( $sbdBlock + 1 ) * self::BIG_BLOCK_SIZE;
  408. for ($j = 0; $j < self::BIG_BLOCK_SIZE / 4; $j ++) {
  409. $this->smallBlockChain[ $index ] = $this->_getInt4d($pos);
  410. $pos += 4;
  411. $index ++;
  412. }
  413. $sbdBlock = $this->bigBlockChain[ $sbdBlock ];
  414. }
  415. // readData(rootStartBlock)
  416. $block = $rootStartBlock;
  417. $this->entry = $this->_readData($block);
  418. /*
  419. while ($block != -2) {
  420. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  421. $this->entry = $this->entry.substr($this->_data, $pos, self::BIG_BLOCK_SIZE);
  422. $block = $this->bigBlockChain[$block];
  423. }
  424. */
  425. //echo '==='.$this->entry."===";
  426. $this->_readPropertySets();
  427. $this->data = $this->_readWorkBook();
  428. return true;
  429. }
  430. // {{{ setOutputEncoding()
  431. protected function _getInt2d($pos): int
  432. {
  433. return ord($this->data[ $pos ]) | ord($this->data[ $pos + 1 ]) << 8;
  434. // return ($value > 0x7FFFFFFF) ? $value - 0x100000000 : $value;
  435. }
  436. protected function _getInt4d($pos): int
  437. {
  438. $value = ord($this->data[ $pos ]) | ( ord($this->data[ $pos + 1 ]) << 8 ) | ( ord($this->data[ $pos + 2 ]) << 16 ) | ( ord($this->data[ $pos + 3 ]) << 24 );
  439. return ($value > 0x7FFFFFFF) ? $value - 0x100000000 : $value;
  440. }
  441. // }}}
  442. // {{{ setRowColOffset()
  443. protected function _readData($bl): string
  444. {
  445. $block = $bl;
  446. $data = '';
  447. while ($block !== - 2) {
  448. $pos = ( $block + 1 ) * self::BIG_BLOCK_SIZE;
  449. $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  450. //echo "pos = $pos data=$data\n";
  451. $block = $this->bigBlockChain[ $block ];
  452. }
  453. return $data;
  454. }
  455. // }}}
  456. // {{{ setDefaultFormat()
  457. protected function _readPropertySets(): void
  458. {
  459. $offset = 0;
  460. //var_dump($this->entry);
  461. while ($offset < strlen($this->entry)) {
  462. $d = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE);
  463. $nameSize = ord($d[ self::SIZE_OF_NAME_POS ]) | ( ord($d[ self::SIZE_OF_NAME_POS + 1 ]) << 8 );
  464. $type = ord($d[ self::TYPE_POS ]);
  465. //$maxBlock = $this->_strlen($d) / self::BIG_BLOCK_SIZE - 1;
  466. $startBlock = ord($d[ self::START_BLOCK_POS]) | ( ord($d[ self::START_BLOCK_POS + 1 ]) << 8 ) | ( ord($d[ self::START_BLOCK_POS + 2 ]) << 16 ) | ( ord($d[ self::START_BLOCK_POS + 3 ]) << 24 );
  467. $size = ord($d[ self::SIZE_POS]) | ( ord($d[ self::SIZE_POS + 1 ]) << 8 ) | ( ord($d[ self::SIZE_POS + 2 ]) << 16 ) | ( ord($d[ self::SIZE_POS + 3 ]) << 24 );
  468. $name = '';
  469. for ($i = 0; $i < $nameSize; $i ++) {
  470. $name .= $d[ $i ];
  471. }
  472. $name = str_replace("\x00", '', $name);
  473. $this->props[] = array(
  474. 'name' => $name,
  475. 'type' => $type,
  476. 'startBlock' => $startBlock,
  477. 'size' => $size
  478. );
  479. if (( $name === 'Workbook' ) || ( $name === 'Book' )) {
  480. $this->wrkbook = count($this->props) - 1;
  481. }
  482. if ($name === 'Root Entry') {
  483. $this->rootEntry = count($this->props) - 1;
  484. }
  485. //echo "name ==$name=\n";
  486. $offset += self::PROPERTY_STORAGE_BLOCK_SIZE;
  487. }
  488. }
  489. // }}}
  490. // {{{ setColumnFormat()
  491. protected function _readWorkBook(): string
  492. {
  493. if ($this->props[ $this->wrkbook ]['size'] < self::SMALL_BLOCK_THRESHOLD) {
  494. // getSmallBlockStream(PropertyStorage ps)
  495. $rootdata = $this->_readData($this->props[ $this->rootEntry ]['startBlock']);
  496. $streamData = '';
  497. $block = (int) $this->props[ $this->wrkbook ]['startBlock'];
  498. //$count = 0;
  499. while ($block !== - 2) {
  500. $pos = $block * self::SMALL_BLOCK_SIZE;
  501. $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE);
  502. $block = $this->smallBlockChain[ $block ];
  503. }
  504. return $streamData;
  505. }
  506. $numBlocks = $this->props[ $this->wrkbook ]['size'] / self::BIG_BLOCK_SIZE;
  507. if ($this->props[ $this->wrkbook ]['size'] % self::BIG_BLOCK_SIZE !== 0) {
  508. $numBlocks ++;
  509. }
  510. if ($numBlocks === 0) {
  511. return '';
  512. }
  513. //echo "numBlocks = $numBlocks\n";
  514. //byte[] streamData = new byte[numBlocks * self::BIG_BLOCK_SIZE];
  515. //print_r($this->wrkbook);
  516. $streamData = '';
  517. $block = $this->props[ $this->wrkbook ]['startBlock'];
  518. //echo "block = $block";
  519. while ($block !== - 2) {
  520. $pos = ( $block + 1 ) * self::BIG_BLOCK_SIZE;
  521. $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  522. $block = $this->bigBlockChain[ $block ];
  523. }
  524. //echo 'stream'.$streamData;
  525. return $streamData;
  526. }
  527. // }}}
  528. protected function parseSubstreamHeader($pos): array
  529. {
  530. $length = $this->_getInt2d($pos + 2);
  531. $version = $this->_getInt2d($pos + 4);
  532. $substreamType = $this->_getInt2d($pos + 6);
  533. return array( $length, $version, $substreamType );
  534. }
  535. // {{{ _parse()
  536. /**
  537. * Parse a workbook
  538. *
  539. * @access private
  540. * @return bool
  541. */
  542. protected function _parse()
  543. {
  544. $pos = 0;
  545. // $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  546. [$length, $version, $substreamType] = $this->parseSubstreamHeader($pos);
  547. // echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n";
  548. // die();
  549. if (( $version !== self::BIFF8 ) &&
  550. ( $version !== self::BIFF7 )
  551. ) {
  552. return false;
  553. }
  554. if ($substreamType !== self::WORKBOOKGLOBALS) {
  555. return false;
  556. }
  557. //print_r($rec);
  558. $pos += $length + 4;
  559. $code = ord($this->data[ $pos ]) | ord($this->data[ $pos + 1 ]) << 8;
  560. $length = ord($this->data[ $pos + 2 ]) | ord($this->data[ $pos + 3 ]) << 8;
  561. while ($code !== self::TYPE_EOF) {
  562. switch ($code) {
  563. case self::TYPE_SST:
  564. //echo "Type_SST\n";
  565. $formattingRuns = 0;
  566. $extendedRunLength = 0;
  567. $spos = $pos + 4;
  568. $limitpos = $spos + $length;
  569. $uniqueStrings = $this->_getInt4d($spos + 4);
  570. $spos += 8;
  571. for ($i = 0; $i < $uniqueStrings; $i ++) {
  572. // Read in the number of characters
  573. if ($spos === $limitpos) {
  574. $opcode = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  575. $conlength = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  576. if ($opcode !== 0x3c) {
  577. return - 1;
  578. }
  579. $spos += 4;
  580. $limitpos = $spos + $conlength;
  581. }
  582. $numChars = ord($this->data[ $spos ]) | ( ord($this->data[ $spos + 1 ]) << 8 );
  583. //echo "i = $i pos = $pos numChars = $numChars ";
  584. $spos += 2;
  585. $optionFlags = ord($this->data[ $spos ]);
  586. $spos ++;
  587. $asciiEncoding = ( ( $optionFlags & 0x01 ) === 0 );
  588. $extendedString = ( ( $optionFlags & 0x04 ) !== 0 );
  589. // See if string contains formatting information
  590. $richString = ( ( $optionFlags & 0x08 ) !== 0 );
  591. if ($richString) {
  592. // Read in the crun
  593. $formattingRuns = $this->_getInt2d($spos);
  594. $spos += 2;
  595. }
  596. if ($extendedString) {
  597. // Read in cchExtRst
  598. $extendedRunLength = $this->_getInt4d($spos);
  599. $spos += 4;
  600. }
  601. $len = $asciiEncoding ? $numChars : $numChars * 2;
  602. if ($spos + $len < $limitpos) {
  603. $retstr = substr($this->data, $spos, $len);
  604. $spos += $len;
  605. } else {
  606. // found countinue
  607. $retstr = substr($this->data, $spos, $limitpos - $spos);
  608. $bytesRead = $limitpos - $spos;
  609. $charsLeft = $numChars - ( $asciiEncoding ? $bytesRead : ( $bytesRead / 2 ) );
  610. $spos = $limitpos;
  611. while ($charsLeft > 0) {
  612. $opcode = $this->_getInt2d($spos);
  613. $conlength = $this->_getInt2d($spos + 2);
  614. if ($opcode !== 0x3c) {
  615. return - 1;
  616. }
  617. $spos += 4;
  618. $limitpos = $spos + $conlength;
  619. $option = ord($this->data[ $spos ]);
  620. $spos ++;
  621. if ($asciiEncoding && ( $option === 0 )) {
  622. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  623. $retstr .= substr($this->data, $spos, $len);
  624. $charsLeft -= $len;
  625. $asciiEncoding = true;
  626. } elseif (! $asciiEncoding && ( $option !== 0 )) {
  627. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  628. $retstr .= substr($this->data, $spos, $len);
  629. $charsLeft -= $len / 2;
  630. $asciiEncoding = false;
  631. } elseif (! $asciiEncoding && ( $option === 0 )) {
  632. // Bummer - the string starts off as Unicode, but after the
  633. // continuation it is in straightforward ASCII encoding
  634. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  635. for ($j = 0; $j < $len; $j ++) {
  636. $retstr .= $this->data[ $spos + $j ] . chr(0);
  637. }
  638. $charsLeft -= $len;
  639. $asciiEncoding = false;
  640. } else {
  641. $newstr = '';
  642. for ($j = 0, $len_retstr = strlen($retstr); $j < $len_retstr; $j ++) {
  643. $newstr = $retstr[ $j ] . chr(0);
  644. }
  645. $retstr = $newstr;
  646. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  647. $retstr .= substr($this->data, $spos, $len);
  648. $charsLeft -= $len / 2;
  649. $asciiEncoding = false;
  650. //echo "Izavrat\n";
  651. }
  652. $spos += $len;
  653. }
  654. }
  655. $retstr = $asciiEncoding ? $this->_latin1toDef($retstr) : $this->_UTF16toDef($retstr);
  656. // echo "Str $i = $retstr\n";
  657. if ($richString) {
  658. $spos += 4 * $formattingRuns;
  659. }
  660. // For extended strings, skip over the extended string data
  661. if ($extendedString) {
  662. $spos += $extendedRunLength;
  663. }
  664. //if ($retstr == 'Derby'){
  665. // echo "bb\n";
  666. //}
  667. $this->sst[] = $retstr;
  668. }
  669. /*$continueRecords = array();
  670. while ($this->getNextCode() == Type_CONTINUE) {
  671. $continueRecords[] = &$this->nextRecord();
  672. }
  673. //echo " 1 Type_SST\n";
  674. $this->shareStrings = new SSTRecord($r, $continueRecords);
  675. //print_r($this->shareStrings->strings);
  676. */
  677. // echo 'SST read: '.($time_end-$time_start)."\n";
  678. break;
  679. case self::TYPE_FILEPASS:
  680. return false;
  681. case self::TYPE_NAME:
  682. //echo "Type_NAME\n";
  683. break;
  684. case self::TYPE_FORMAT:
  685. $indexCode = $this->_getInt2d($pos + 4);
  686. if ($version === self::BIFF8) {
  687. $numchars = $this->_getInt2d($pos + 6);
  688. if (ord($this->data[ $pos + 8 ]) === 0) { // ascii
  689. $formatString = substr($this->data, $pos + 9, $numchars);
  690. $formatString = $this->_latin1toDef($formatString);
  691. } else {
  692. $formatString = substr($this->data, $pos + 9, $numchars * 2);
  693. $formatString = $this->_UTF16toDef($formatString);
  694. }
  695. } else {
  696. $numchars = ord($this->data[ $pos + 6 ]);
  697. $formatString = substr($this->data, $pos + 7, $numchars * 2);
  698. $formatString = $this->_latin1toDef($formatString);
  699. }
  700. $this->formatRecords[ $indexCode ] = $formatString;
  701. // echo "Type.FORMAT[$indexCode]=$formatString\n";
  702. break;
  703. case self::TYPE_XF:
  704. $formatstr = '';
  705. $indexCode = $this->_getInt2d($pos + 6);
  706. // echo "\nType.XF code=".$indexCode." dateFormat=".$this->dateFormats[ $indexCode ]." numberFormats=".$this->numberFormats[ $indexCode ].PHP_EOL;
  707. if (array_key_exists($indexCode, $this->dateFormats)) {
  708. //echo "isdate ".$dateFormats[$indexCode];
  709. $this->formatRecords['xfrecords'][] = array(
  710. 'type' => 'date',
  711. 'format' => $this->dateFormats[ $indexCode ]
  712. );
  713. } elseif (array_key_exists($indexCode, $this->numberFormats)) {
  714. //echo "isnumber ".$this->numberFormats[$indexCode];
  715. $this->formatRecords['xfrecords'][] = array(
  716. 'type' => 'number',
  717. 'format' => $this->numberFormats[ $indexCode ]
  718. );
  719. } else {
  720. $isdate = false;
  721. if ($indexCode > 0) {
  722. if (isset($this->formatRecords[ $indexCode ])) {
  723. // die( 'L:'.__LINE__ );
  724. $formatstr = $this->formatRecords[ $indexCode ];
  725. }
  726. //echo '.other.';
  727. // echo "\nfl=".strlen( $formatstr)." fs=$formatstr=\n";
  728. // echo "\ncode=".$indexCode." fl=".strlen( $formatstr)." fs=$formatstr=\n";
  729. $fs = str_replace('\\', '', $formatstr);
  730. if ($fs && preg_match('/^[hmsday\/\-:\., ]+$/i', $fs)) { // found day and time format
  731. $isdate = true;
  732. $formatstr = str_replace(array( 'yyyy',':mm','mm','dddd','dd', 'h','ss' ), array('Y',':i','m','l','d', 'H','s' ), $fs);
  733. }
  734. }
  735. if ($isdate) {
  736. $this->formatRecords['xfrecords'][] = array(
  737. 'type' => 'date',
  738. 'format' => $formatstr,
  739. 'code' => $indexCode
  740. );
  741. } else {
  742. // echo 'fs='.$formatstr.PHP_EOL;
  743. $this->formatRecords['xfrecords'][] = array(
  744. 'type' => 'other',
  745. 'format' => '',
  746. 'code' => $indexCode
  747. );
  748. }
  749. }
  750. // echo count( $this->formatRecords['xfrecords'] ).' fs='.$formatstr.' ' . PHP_EOL;
  751. //echo "\n";
  752. break;
  753. case self::TYPE_NINETEENFOUR:
  754. //echo "Type.NINETEENFOUR\n";
  755. $this->nineteenFour = ( ord($this->data[ $pos + 4 ]) === 1 );
  756. break;
  757. case self::TYPE_BOUNDSHEET:
  758. //echo "Type.BOUNDSHEET\n";
  759. $rec_offset = $this->_getInt4d($pos + 4);
  760. // $rec_typeFlag = ord($this->_data[$pos + 8]);
  761. $rec_length = ord($this->data[ $pos + 10 ]);
  762. $hidden = false;
  763. $rec_name = '';
  764. if ($version === self::BIFF8) {
  765. //ord($this->data[$pos + 9])
  766. $hidden = ord($this->data[$pos + 8]) === 1;
  767. $chartype = ord($this->data[ $pos + 11 ]);
  768. if ($chartype === 0) {
  769. $rec_name = substr($this->data, $pos + 12, $rec_length);
  770. $rec_name = $this->_latin1toDef($rec_name);
  771. } else {
  772. $rec_name = substr($this->data, $pos + 12, $rec_length * 2);
  773. $rec_name = $this->_UTF16toDef($rec_name);
  774. }
  775. } elseif ($version === self::BIFF7) {
  776. $rec_name = substr($this->data, $pos + 11, $rec_length);
  777. }
  778. $this->boundsheets[] = array(
  779. 'name' => $rec_name,
  780. 'offset' => $rec_offset,
  781. 'hidden' => $hidden,
  782. 'active' => false
  783. );
  784. break;
  785. case self::TYPE_WINDOW1:
  786. $this->activeSheet = $this->_getInt2d($pos + 14);
  787. break;
  788. }
  789. //echo "Code = ".base_convert($r['code'],10,16)."\n";
  790. $pos += $length + 4;
  791. $code = $this->_getInt2d($pos);
  792. $length = $this->_getInt2d($pos + 2);
  793. //$r = &$this->nextRecord();
  794. //echo "1 Code = ".base_convert($r['code'],10,16)."\n";
  795. }
  796. foreach ($this->boundsheets as $key => $val) {
  797. $this->sn = $key;
  798. $this->_parseSheet($val['offset']);
  799. if ($key === $this->activeSheet) {
  800. $this->boundsheets[ $key ]['active'] = true;
  801. }
  802. }
  803. return true;
  804. }
  805. protected function _latin1toDef($string)
  806. {
  807. $result = $string;
  808. if ($this->defaultEncoding) {
  809. $result = mb_convert_encoding($string, $this->defaultEncoding, 'ISO-8859-1');
  810. }
  811. return $result;
  812. }
  813. protected function _UTF16toDef($string)
  814. {
  815. $result = $string;
  816. if ($this->defaultEncoding && $this->defaultEncoding !== 'UTF-16LE') {
  817. $result = mb_convert_encoding($string, $this->defaultEncoding, 'UTF-16LE');
  818. }
  819. return $result;
  820. }
  821. protected function _parseSheet($spos): bool
  822. {
  823. $cont = true;
  824. // read BOF
  825. // $code = ord($this->_data[$spos]) | ord($this->_data[$spos + 1]) << 8;
  826. [$length, $version, $substreamType] = $this->parseSubstreamHeader($spos);
  827. if (( $version !== self::BIFF8 ) && ( $version !== self::BIFF7 )) {
  828. return false;
  829. }
  830. if ($substreamType !== self::WORKSHEET) {
  831. return false;
  832. }
  833. //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n";
  834. $spos += $length + 4;
  835. //var_dump($this->formatRecords);
  836. //echo "code $code $length";
  837. $this->sheets[ $this->sn ]['maxrow'] = 0;
  838. $this->sheets[ $this->sn ]['maxcol'] = 0;
  839. $this->sheets[ $this->sn ]['numRows'] = 0;
  840. $this->sheets[ $this->sn ]['numCols'] = 0;
  841. while ($cont) {
  842. //echo "mem= ".memory_get_usage()."\n";
  843. // $r = &$this->file->nextRecord();
  844. $lowcode = ord($this->data[ $spos ]);
  845. if ($lowcode === self::TYPE_EOF) {
  846. break;
  847. }
  848. $t_code = $lowcode | ord($this->data[ $spos + 1 ]) << 8;
  849. $length = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  850. $spos += 4;
  851. //echo "Code=".base_convert($code,10,16)." $code\n";
  852. $this->multiplier = 1; // need for format with %
  853. switch ($t_code) {
  854. case self::TYPE_DIMENSION:
  855. //echo 'Type_DIMENSION ';
  856. if (!isset($this->numRows)) {
  857. if (( $length === 10 ) || ( $version === self::BIFF7 )) {
  858. $this->sheets[ $this->sn ]['numRows'] = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  859. $this->sheets[ $this->sn ]['numCols'] = ord($this->data[ $spos + 6 ]) | ord($this->data[ $spos + 7 ]) << 8;
  860. } else {
  861. $this->sheets[ $this->sn ]['numRows'] = ord($this->data[ $spos + 4 ]) | ord($this->data[ $spos + 5 ]) << 8;
  862. $this->sheets[ $this->sn ]['numCols'] = ord($this->data[ $spos + 10 ]) | ord($this->data[ $spos + 11 ]) << 8;
  863. }
  864. }
  865. //echo 'numRows '.$this->numRows.' '.$this->numCols."\n";
  866. break;
  867. case self::TYPE_MERGEDCELLS:
  868. $cellRanges = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  869. for ($i = 0; $i < $cellRanges; $i ++) {
  870. $fr = ord($this->data[ $spos + 8 * $i + 2 ]) | ord($this->data[ $spos + 8 * $i + 3 ]) << 8;
  871. $lr = ord($this->data[ $spos + 8 * $i + 4 ]) | ord($this->data[ $spos + 8 * $i + 5 ]) << 8;
  872. $fc = ord($this->data[ $spos + 8 * $i + 6 ]) | ord($this->data[ $spos + 8 * $i + 7 ]) << 8;
  873. $lc = ord($this->data[ $spos + 8 * $i + 8 ]) | ord($this->data[ $spos + 8 * $i + 9 ]) << 8;
  874. //$this->sheets[$this->sn]['mergedCells'][] = array($fr + 1, $fc + 1, $lr + 1, $lc + 1);
  875. if ($lr - $fr > 0) {
  876. $this->sheets[ $this->sn ]['cellsInfo'][ $fr + 1 ][ $fc + 1 ]['rowspan'] = $lr - $fr + 1;
  877. }
  878. if ($lc - $fc > 0) {
  879. $this->sheets[ $this->sn ]['cellsInfo'][ $fr + 1 ][ $fc + 1 ]['colspan'] = $lc - $fc + 1;
  880. }
  881. }
  882. //echo "Merged Cells $cellRanges $lr $fr $lc $fc\n";
  883. break;
  884. case self::TYPE_RK:
  885. case self::TYPE_RK2:
  886. //echo 'self::TYPE_RK'."\n";
  887. $row = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  888. $column = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  889. $rknum = $this->_getInt4d($spos + 6);
  890. $numValue = $this->_getIEEE754($rknum);
  891. //echo $numValue." ";
  892. $t_alias = 'n';
  893. if ($this->isDate($spos)) {
  894. [$string, $raw] = $this->createDate((int) $numValue);
  895. $t_alias = 'd';
  896. } else {
  897. $raw = $numValue;
  898. if (isset($this->columnsFormat[ $column + 1 ])) {
  899. $this->curFormat = $this->columnsFormat[ $column + 1 ];
  900. }
  901. $string = sprintf($this->curFormat, $numValue * $this->multiplier);
  902. //$this->addcell(RKRecord($r));
  903. }
  904. $this->addCell($row, $column, $string, $raw, $t_code, $t_alias);
  905. //echo "Type_RK $row $column $string $raw {$this->curformat}\n";
  906. break;
  907. case self::TYPE_LABELSST:
  908. $row = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  909. $column = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  910. // $xfindex = ord($this->_data[$spos + 4]) | ord($this->_data[$spos + 5]) << 8;
  911. $index = $this->_getInt4d($spos + 6);
  912. //var_dump($this->sst);
  913. $this->addCell($row, $column, $this->sst[ $index ], $index, $t_code, 's');
  914. //echo "LabelSST $row $column $string\n";
  915. break;
  916. case self::TYPE_MULRK:
  917. $row = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  918. $colFirst = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  919. $colLast = ord($this->data[ $spos + $length - 2 ]) | ord($this->data[ $spos + $length - 1 ]) << 8;
  920. $columns = $colLast - $colFirst + 1;
  921. $tmppos = $spos + 4;
  922. $t_alias = 'n';
  923. for ($i = 0; $i < $columns; $i ++) {
  924. $numValue = $this->_getIEEE754($this->_getInt4d($tmppos + 2));
  925. if ($this->isDate($tmppos - 4)) {
  926. [$string, $raw] = $this->createDate((int) $numValue);
  927. $t_alias = 'd';
  928. } else {
  929. $raw = $numValue;
  930. if (isset($this->columnsFormat[ $colFirst + $i + 1 ])) {
  931. $this->curFormat = $this->columnsFormat[ $colFirst + $i + 1 ];
  932. }
  933. $string = sprintf($this->curFormat, $numValue * $this->multiplier);
  934. }
  935. //$rec['rknumbers'][$i]['xfindex'] = ord($rec['data'][$pos]) | ord($rec['data'][$pos+1]) << 8;
  936. $tmppos += 6;
  937. $this->addCell($row, $colFirst + $i, $string, $raw, $t_code, $t_alias);
  938. //echo "MULRK $row ".($colFirst + $i)." $string\n";
  939. }
  940. //MulRKRecord($r);
  941. // Get the individual cell records from the multiple record
  942. //$num = ;
  943. break;
  944. case self::TYPE_NUMBER:
  945. $row = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  946. $column = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  947. $tmp = unpack('ddouble', substr($this->data, $spos + 6, 8)); // It machine machine dependent
  948. $t_alias = 'n';
  949. if ($this->isDate($spos)) {
  950. [$string, $raw] = $this->createDate((int) $tmp['double']);
  951. $t_alias = 'd';
  952. // $this->addcell(DateRecord($r, 1));
  953. } else {
  954. //$raw = $tmp[''];
  955. if (isset($this->columnsFormat[ $column + 1 ])) {
  956. $this->curFormat = $this->columnsFormat[ $column + 1 ];
  957. }
  958. $raw = $this->createNumber($spos);
  959. $string = sprintf($this->curFormat, $raw * $this->multiplier);
  960. // $this->addcell(NumberRecord($r));
  961. }
  962. $this->addCell($row, $column, $string, $raw, $t_code, $t_alias);
  963. //echo "Number $row $column $string\n";
  964. break;
  965. case self::TYPE_FORMULA:
  966. case self::TYPE_FORMULA2:
  967. $row = ord($this->data[ $spos ]) | ord($this->data[ $spos + 1 ]) << 8;
  968. $column = ord($this->data[ $spos + 2 ]) | ord($this->data[ $spos + 3 ]) << 8;
  969. /*
  970. $byte6 = ord($this->_data[$spos + 6]);
  971. $byte12 = ord($this->_data[$spos + 12]);
  972. $byte13 = ord($this->_data[$spos + 13]);
  973. if ( $byte6 === 0 && $byte12 === 255 && $byte13 === 255 ) {
  974. //String formula. Result follows in a STRING record
  975. //echo "FORMULA $row $column Formula with a string<br>\n";
  976. } else if ($byte6 === 1 && $byte12 === 255 && $byte13 === 255 ) {
  977. //Boolean formula. Result is in +2; 0=false,1=true
  978. } else if ($byte6 === 2 && $byte12 === 255 && $byte13 === 255) {
  979. //Error formula. Error code is in +2;
  980. } else if ( $byte6 === 3 && $byte12 === 255 && $byte13 === 255) {
  981. //Formula result is a null string.
  982. */
  983. if (! ( ord($this->data[ $spos + 6 ]) < 4 && ord($this->data[ $spos + 12 ]) === 255 && ord($this->data[ $spos + 13 ]) === 255 )) {
  984. // result is a number, so first 14 bytes are just like a _NUMBER record
  985. $tmp = unpack('ddouble', substr($this->data, $spos + 6, 8)); // It machine machine dependent
  986. if ($this->isDate($spos)) {
  987. [$string, $raw] = $this->createDate((int) $tmp['double']);
  988. // $this->addcell(DateRecord($r, 1));
  989. } else {
  990. //$raw = $tmp[''];
  991. if (isset($this->columnsFormat[ $column + 1 ])) {
  992. $this->curFormat = $this->columnsFormat[ $column + 1 ];
  993. }
  994. $raw = $this->createNumber($spos);
  995. $string = sprintf($this->curFormat, $raw * $this->multiplier);
  996. // $this->addcell(NumberRecord($r));
  997. }
  998. $this->addCell($row, $column, $string, $raw, $t_code, 'f');
  999. //echo "Number $row $column $string\n";
  1000. }
  1001. break;
  1002. case self::TYPE_BOOLERR:
  1003. $row = $this->_getInt2d($spos);
  1004. $column = $this->_getInt2d($spos + 2);
  1005. $string = ord($this->data[ $spos + 6 ]);
  1006. $this->addCell($row, $column, $string, $string, $t_code, 'b');
  1007. //echo 'Type_BOOLERR '."\n";
  1008. break;
  1009. case self::TYPE_ROW:
  1010. case self::TYPE_DBCELL:
  1011. case self::TYPE_MULBLANK:
  1012. break;
  1013. case self::TYPE_LABEL:
  1014. $row = $this->_getInt2d($spos);
  1015. $column = $this->_getInt2d($spos + 2);
  1016. $string = substr($this->data, $spos + 8, ord($this->data[ $spos + 6 ]) | ord($this->data[ $spos + 7 ]) << 8);
  1017. $this->addCell($row, $column, $string, '', $t_code, 'inlineStr');
  1018. // $this->addcell(LabelRecord($r));
  1019. break;
  1020. case self::TYPE_EOF:
  1021. $cont = false;
  1022. break;
  1023. default:
  1024. //echo ' unknown :'.base_convert($r['code'],10,16)."\n";
  1025. break;
  1026. }
  1027. $spos += $length;
  1028. }
  1029. if ($this->sheets[ $this->sn ]['numRows'] === 0) {
  1030. $this->sheets[ $this->sn ]['numRows'] = $this->sheets[ $this->sn ]['maxrow'];
  1031. }
  1032. if ($this->sheets[ $this->sn ]['numCols'] === 0) {
  1033. $this->sheets[ $this->sn ]['numCols'] = $this->sheets[ $this->sn ]['maxcol'];
  1034. }
  1035. return true;
  1036. }
  1037. //}}}
  1038. //{{{ createDate()
  1039. protected function _getIEEE754($rknum)
  1040. {
  1041. if (( $rknum & 0x02 ) !== 0) {
  1042. $value = $rknum >> 2;
  1043. } else {
  1044. //mmp
  1045. // first comment out the previously existing 7 lines of code here
  1046. // $tmp = unpack("d", pack("VV", 0, ($rknum & 0xfffffffc)));
  1047. // //$value = $tmp[''];
  1048. // if (array_key_exists(1, $tmp)) {
  1049. // $value = $tmp[1];
  1050. // } else {
  1051. // $value = $tmp[''];
  1052. // }
  1053. // I got my info on IEEE754 encoding from
  1054. // http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  1055. // The RK format calls for using only the most significant 30 bits of the
  1056. // 64 bit floating point value. The other 34 bits are assumed to be 0
  1057. // So, we use the upper 30 bits of $rknum as follows...
  1058. $sign = ( $rknum & 0x80000000 ) >> 31;
  1059. $exp = ( $rknum & 0x7ff00000 ) >> 20;
  1060. $mantissa = ( 0x100000 | ( $rknum & 0x000ffffc ) );
  1061. $value = $mantissa / (2 ** (20 - ($exp - 1023)));
  1062. if ($sign) {
  1063. $value = - 1 * $value;
  1064. }
  1065. //end of changes by mmp
  1066. }
  1067. if (( $rknum & 0x01 ) !== 0) {
  1068. $value /= 100;
  1069. }
  1070. return $value;
  1071. }
  1072. protected function isDate($spos): bool
  1073. {
  1074. //$xfindex = GetInt2d(, 4);
  1075. $xfindex = ord($this->data[ $spos + 4 ]) | ord($this->data[ $spos + 5 ]) << 8;
  1076. // echo 'check is date '.$xfindex.' '.$this->formatRecords['xfrecords'][$xfindex]['type']." ".$this->formatRecords['xfrecords'][ $xfindex ]['format']."\n";
  1077. if ($this->formatRecords['xfrecords'][ $xfindex ]['type'] === 'date') {
  1078. $this->curFormat = $this->formatRecords['xfrecords'][ $xfindex ]['format'];
  1079. return true;
  1080. }
  1081. if ($this->formatRecords['xfrecords'][ $xfindex ]['type'] === 'number') {
  1082. $this->curFormat = $this->formatRecords['xfrecords'][ $xfindex ]['format'];
  1083. if (strpos($this->curFormat, '%%') !== false) {
  1084. $this->multiplier = 100;
  1085. }
  1086. } else {
  1087. $this->curFormat = $this->defaultFormat;
  1088. }
  1089. return false;
  1090. }
  1091. /**
  1092. * Convert the raw Excel date into a human readable format
  1093. *
  1094. * Dates in Excel are stored as number of seconds from an epoch. On
  1095. * Windows, the epoch is 30/12/1899 and on Mac it's 01/01/1904
  1096. *
  1097. * @param integer $timevalue The raw Excel value to convert
  1098. *
  1099. * @return array First element is the converted date, the second element is number a unix timestamp
  1100. */
  1101. public function createDate(int $timevalue): array
  1102. {
  1103. // $offset = ($timeoffset===null)? date('Z') : $timeoffset * 3600;
  1104. if ($timevalue > 1) {
  1105. $timevalue -= ( $this->nineteenFour ? 24107 : 25569 );
  1106. }
  1107. $ts = round($timevalue * 24 * 3600);
  1108. $string = $this->datetimeFormat ? gmdate($this->datetimeFormat, $ts) : gmdate($this->curFormat, $ts);
  1109. return array( $string, $ts );
  1110. }
  1111. protected function addCell($row, $col, $string, $raw = '', $type_code = 0, $type_alias = ''): void
  1112. {
  1113. //echo "ADD cel $row-$col $string\n";
  1114. $this->sheets[ $this->sn ]['maxrow'] = max($this->sheets[ $this->sn ]['maxrow'], $row);
  1115. $this->sheets[ $this->sn ]['maxcol'] = max($this->sheets[ $this->sn ]['maxcol'], $col);
  1116. $this->sheets[ $this->sn ]['cells'][ $row ][ $col ] = $string;
  1117. if ($raw) {
  1118. $this->sheets[ $this->sn ]['cellsInfo'][ $row ][ $col ]['raw'] = $raw;
  1119. }
  1120. if ($type_code) {
  1121. $this->sheets[$this->sn]['cellsInfo'][$row][$col]['type'] = $type_code;
  1122. $this->sheets[$this->sn]['cellsInfo'][$row][$col]['t'] = $type_alias;
  1123. }
  1124. }
  1125. protected function createNumber($spos)
  1126. {
  1127. $rknumhigh = $this->_getInt4d($spos + 10);
  1128. $rknumlow = $this->_getInt4d($spos + 6);
  1129. //for ($i=0; $i<8; $i++) { echo ord($this->_data[$i+$spos+6]) . " "; } echo "<br>";
  1130. $sign = ( $rknumhigh & 0x80000000 ) >> 31;
  1131. $exp = ( $rknumhigh & 0x7ff00000 ) >> 20;
  1132. $mantissa = ( 0x100000 | ( $rknumhigh & 0x000fffff ) );
  1133. $mantissalow1 = ( $rknumlow & 0x80000000 ) >> 31;
  1134. $mantissalow2 = ( $rknumlow & 0x7fffffff );
  1135. $value = $mantissa / (2 ** (20 - ($exp - 1023)));
  1136. if ($mantissalow1 !== 0) {
  1137. $value += 1 / (2 ** (21 - ($exp - 1023)));
  1138. }
  1139. $value += $mantissalow2 / (2 ** (52 - ($exp - 1023)));
  1140. //echo "Sign = $sign, Exp = $exp, mantissahighx = $mantissa, mantissalow1 = $mantissalow1, mantissalow2 = $mantissalow2<br>\n";
  1141. if ($sign) {
  1142. $value = - 1 * $value;
  1143. }
  1144. return $value;
  1145. }
  1146. /**
  1147. * Set the encoding method
  1148. *
  1149. * @param string $encoding Encoding to use
  1150. *
  1151. * @access public
  1152. */
  1153. public function setOutputEncoding(string $encoding): SimpleXLS
  1154. {
  1155. $this->defaultEncoding = $encoding;
  1156. return $this;
  1157. }
  1158. /**
  1159. * Set the default number format, ex. "%.2f"
  1160. *
  1161. * @access public
  1162. *
  1163. * @param string $sFormat Default format
  1164. */
  1165. public function setDefaultFormat(string $sFormat): SimpleXLS
  1166. {
  1167. $this->defaultFormat = $sFormat;
  1168. return $this;
  1169. }
  1170. /**
  1171. * Force a column to use a certain format
  1172. *
  1173. * @access public
  1174. *
  1175. * @param integer $column Column number
  1176. * @param string $sFormat Format
  1177. */
  1178. public function setColumnFormat(int $column, string $sFormat): SimpleXLS
  1179. {
  1180. $this->columnsFormat[ $column ] = $sFormat;
  1181. return $this;
  1182. }
  1183. }