Newer
Older
DirtyScripts / RS / ExcelCal / SimpleXLSX.php
root on 21 Apr 2022 39 KB xls2ical
  1. <?php /** @noinspection MultiAssignmentUsageInspection */
  2.  
  3. namespace Shuchkin;
  4.  
  5. use SimpleXMLElement;
  6.  
  7. /**
  8. * SimpleXLSX php class
  9. * MS Excel 2007+ workbooks reader
  10. *
  11. * Copyright (c) 2012 - 2022 SimpleXLSX
  12. *
  13. * @category SimpleXLSX
  14. * @package SimpleXLSX
  15. * @copyright Copyright (c) 2012 - 2022 SimpleXLSX (https://github.com/shuchkin/simplexlsx/)
  16. * @license MIT
  17. */
  18.  
  19. /** Examples
  20. *
  21. * use Shuchkin\SimpleXLSX;
  22. *
  23. * Example 1:
  24. * if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
  25. * foreach ($xlsx->rows() as $r) {
  26. * print_r( $r );
  27. * }
  28. * } else {
  29. * echo SimpleXLSX::parseError();
  30. * }
  31. *
  32. * Example 2: html table
  33. * if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
  34. * echo $xlsx->toHTML();
  35. * } else {
  36. * echo SimpleXLSX::parseError();
  37. * }
  38. *
  39. * Example 3: rowsEx
  40. * $xlsx = SimpleXLSX::parse('book.xlsx');
  41. * foreach ( $xlsx->rowsEx() as $r ) {
  42. * print_r( $r );
  43. * }
  44. *
  45. * Example 4: select worksheet
  46. * $xlsx = SimpleXLSX::parse('book.xlsx');
  47. * foreach( $xlsx->rows(1) as $r ) { // second worksheet
  48. * print_t( $r );
  49. * }
  50. *
  51. * Example 5: IDs and worksheet names
  52. * $xlsx = SimpleXLSX::parse('book.xlsx');
  53. * print_r( $xlsx->sheetNames() ); // array( 0 => 'Sheet 1', 1 => 'Catalog' );
  54. *
  55. * Example 6: get sheet name by index
  56. * $xlsx = SimpleXLSX::parse('book.xlsx');
  57. * echo 'Sheet Name 2 = '.$xlsx->sheetName(1);
  58. *
  59. * Example 7: getCell (very slow)
  60. * echo $xlsx->getCell(1,'D12'); // reads D12 cell from second sheet
  61. *
  62. * Example 8: read data
  63. * if ( $xlsx = SimpleXLSX::parseData( file_get_contents('http://www.example.com/example.xlsx') ) ) {
  64. * $dim = $xlsx->dimension(1);
  65. * $num_cols = $dim[0];
  66. * $num_rows = $dim[1];
  67. * echo $xlsx->sheetName(1).':'.$num_cols.'x'.$num_rows;
  68. * } else {
  69. * echo SimpleXLSX::parseError();
  70. * }
  71. *
  72. * Example 9: old style
  73. * $xlsx = new SimpleXLSX('book.xlsx');
  74. * if ( $xlsx->success() ) {
  75. * print_r( $xlsx->rows() );
  76. * } else {
  77. * echo 'xlsx error: '.$xlsx->error();
  78. * }
  79. */
  80. class SimpleXLSX
  81. {
  82. // Don't remove this string! Created by Sergey Shuchkin sergey.shuchkin@gmail.com
  83. public static $CF = [ // Cell formats
  84. 0 => 'General',
  85. 1 => '0',
  86. 2 => '0.00',
  87. 3 => '#,##0',
  88. 4 => '#,##0.00',
  89. 9 => '0%',
  90. 10 => '0.00%',
  91. 11 => '0.00E+00',
  92. 12 => '# ?/?',
  93. 13 => '# ??/??',
  94. 14 => 'mm-dd-yy',
  95. 15 => 'd-mmm-yy',
  96. 16 => 'd-mmm',
  97. 17 => 'mmm-yy',
  98. 18 => 'h:mm AM/PM',
  99. 19 => 'h:mm:ss AM/PM',
  100. 20 => 'h:mm',
  101. 21 => 'h:mm:ss',
  102. 22 => 'm/d/yy h:mm',
  103.  
  104. 37 => '#,##0 ;(#,##0)',
  105. 38 => '#,##0 ;[Red](#,##0)',
  106. 39 => '#,##0.00;(#,##0.00)',
  107. 40 => '#,##0.00;[Red](#,##0.00)',
  108.  
  109. 44 => '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
  110. 45 => 'mm:ss',
  111. 46 => '[h]:mm:ss',
  112. 47 => 'mmss.0',
  113. 48 => '##0.0E+0',
  114. 49 => '@',
  115.  
  116. 27 => '[$-404]e/m/d',
  117. 30 => 'm/d/yy',
  118. 36 => '[$-404]e/m/d',
  119. 50 => '[$-404]e/m/d',
  120. 57 => '[$-404]e/m/d',
  121.  
  122. 59 => 't0',
  123. 60 => 't0.00',
  124. 61 => 't#,##0',
  125. 62 => 't#,##0.00',
  126. 67 => 't0%',
  127. 68 => 't0.00%',
  128. 69 => 't# ?/?',
  129. 70 => 't# ??/??',
  130. ];
  131. public $nf = []; // number formats
  132. public $cellFormats = []; // cellXfs
  133. public $datetimeFormat = 'Y-m-d H:i:s';
  134. public $debug;
  135. public $activeSheet = 0;
  136. public $rowsExReader;
  137.  
  138. /* @var SimpleXMLElement[] $sheets */
  139. protected $sheets;
  140. protected $sheetNames = [];
  141. protected $sheetFiles = [];
  142. // scheme
  143. public $styles;
  144. protected $hyperlinks;
  145. /* @var array[] $package */
  146. protected $package;
  147. protected $sharedstrings;
  148. protected $date1904 = 0;
  149.  
  150.  
  151. /*
  152. private $date_formats = array(
  153. 0xe => "d/m/Y",
  154. 0xf => "d-M-Y",
  155. 0x10 => "d-M",
  156. 0x11 => "M-Y",
  157. 0x12 => "h:i a",
  158. 0x13 => "h:i:s a",
  159. 0x14 => "H:i",
  160. 0x15 => "H:i:s",
  161. 0x16 => "d/m/Y H:i",
  162. 0x2d => "i:s",
  163. 0x2e => "H:i:s",
  164. 0x2f => "i:s.S"
  165. );
  166. private $number_formats = array(
  167. 0x1 => "%1.0f", // "0"
  168. 0x2 => "%1.2f", // "0.00",
  169. 0x3 => "%1.0f", //"#,##0",
  170. 0x4 => "%1.2f", //"#,##0.00",
  171. 0x5 => "%1.0f", //"$#,##0;($#,##0)",
  172. 0x6 => '$%1.0f', //"$#,##0;($#,##0)",
  173. 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  174. 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  175. 0x9 => '%1.0f%%', //"0%"
  176. 0xa => '%1.2f%%', //"0.00%"
  177. 0xb => '%1.2f', //"0.00E00",
  178. 0x25 => '%1.0f', //"#,##0;(#,##0)",
  179. 0x26 => '%1.0f', //"#,##0;(#,##0)",
  180. 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)",
  181. 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)",
  182. 0x29 => '%1.0f', //"#,##0;(#,##0)",
  183. 0x2a => '$%1.0f', //"$#,##0;($#,##0)",
  184. 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)",
  185. 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  186. 0x30 => '%1.0f'); //"##0.0E0";
  187. // }}}
  188. */
  189. protected $errno = 0;
  190. protected $error = false;
  191. /**
  192. * @var false|SimpleXMLElement
  193. */
  194. public $theme;
  195.  
  196.  
  197. public function __construct($filename = null, $is_data = null, $debug = null)
  198. {
  199. if ($debug !== null) {
  200. $this->debug = $debug;
  201. }
  202. $this->package = [
  203. 'filename' => '',
  204. 'mtime' => 0,
  205. 'size' => 0,
  206. 'comment' => '',
  207. 'entries' => []
  208. ];
  209. if ($filename && $this->_unzip($filename, $is_data)) {
  210. $this->_parse();
  211. }
  212. }
  213.  
  214. protected function _unzip($filename, $is_data = false)
  215. {
  216.  
  217. if ($is_data) {
  218. $this->package['filename'] = 'default.xlsx';
  219. $this->package['mtime'] = time();
  220. $this->package['size'] = $this->_strlen($filename);
  221.  
  222. $vZ = $filename;
  223. } else {
  224. if (!is_readable($filename)) {
  225. $this->error(1, 'File not found ' . $filename);
  226.  
  227. return false;
  228. }
  229.  
  230. // Package information
  231. $this->package['filename'] = $filename;
  232. $this->package['mtime'] = filemtime($filename);
  233. $this->package['size'] = filesize($filename);
  234.  
  235. // Read file
  236. $vZ = file_get_contents($filename);
  237. }
  238. // Cut end of central directory
  239. /* $aE = explode("\x50\x4b\x05\x06", $vZ);
  240.  
  241. if (count($aE) == 1) {
  242. $this->error('Unknown format');
  243. return false;
  244. }
  245. */
  246. // Explode to each part
  247. $aE = explode("\x50\x4b\x03\x04", $vZ);
  248. array_shift($aE);
  249.  
  250. $aEL = count($aE);
  251. if ($aEL === 0) {
  252. $this->error(2, 'Unknown archive format');
  253.  
  254. return false;
  255. }
  256. // Search central directory end record
  257. $last = $aE[$aEL - 1];
  258. $last = explode("\x50\x4b\x05\x06", $last);
  259. if (count($last) !== 2) {
  260. $this->error(2, 'Unknown archive format');
  261.  
  262. return false;
  263. }
  264. // Search central directory
  265. $last = explode("\x50\x4b\x01\x02", $last[0]);
  266. if (count($last) < 2) {
  267. $this->error(2, 'Unknown archive format');
  268.  
  269. return false;
  270. }
  271. $aE[$aEL - 1] = $last[0];
  272.  
  273. // Loop through the entries
  274. foreach ($aE as $vZ) {
  275. $aI = [];
  276. $aI['E'] = 0;
  277. $aI['EM'] = '';
  278. // Retrieving local file header information
  279. // $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
  280. $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL/v1EFL', $vZ);
  281.  
  282. // Check if data is encrypted
  283. // $bE = ($aP['GPF'] && 0x0001) ? TRUE : FALSE;
  284. // $bE = false;
  285. $nF = $aP['FNL'];
  286. $mF = $aP['EFL'];
  287.  
  288. // Special case : value block after the compressed data
  289. if ($aP['GPF'] & 0x0008) {
  290. $aP1 = unpack('V1CRC/V1CS/V1UCS', $this->_substr($vZ, -12));
  291.  
  292. $aP['CRC'] = $aP1['CRC'];
  293. $aP['CS'] = $aP1['CS'];
  294. $aP['UCS'] = $aP1['UCS'];
  295. // 2013-08-10
  296. $vZ = $this->_substr($vZ, 0, -12);
  297. if ($this->_substr($vZ, -4) === "\x50\x4b\x07\x08") {
  298. $vZ = $this->_substr($vZ, 0, -4);
  299. }
  300. }
  301.  
  302. // Getting stored filename
  303. $aI['N'] = $this->_substr($vZ, 26, $nF);
  304. $aI['N'] = str_replace('\\', '/', $aI['N']);
  305.  
  306. if ($this->_substr($aI['N'], -1) === '/') {
  307. // is a directory entry - will be skipped
  308. continue;
  309. }
  310.  
  311. // Truncate full filename in path and filename
  312. $aI['P'] = dirname($aI['N']);
  313. $aI['P'] = ($aI['P'] === '.') ? '' : $aI['P'];
  314. $aI['N'] = basename($aI['N']);
  315.  
  316. $vZ = $this->_substr($vZ, 26 + $nF + $mF);
  317.  
  318. if ($this->_strlen($vZ) !== (int)$aP['CS']) { // check only if availabled
  319. $aI['E'] = 1;
  320. $aI['EM'] = 'Compressed size is not equal with the value in header information.';
  321. }
  322. // } elseif ( $bE ) {
  323. // $aI['E'] = 5;
  324. // $aI['EM'] = 'File is encrypted, which is not supported from this class.';
  325. /* } else {
  326. switch ($aP['CM']) {
  327. case 0: // Stored
  328. // Here is nothing to do, the file ist flat.
  329. break;
  330. case 8: // Deflated
  331. $vZ = gzinflate($vZ);
  332. break;
  333. case 12: // BZIP2
  334. if (extension_loaded('bz2')) {
  335. $vZ = bzdecompress($vZ);
  336. } else {
  337. $aI['E'] = 7;
  338. $aI['EM'] = 'PHP BZIP2 extension not available.';
  339. }
  340. break;
  341. default:
  342. $aI['E'] = 6;
  343. $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
  344. }
  345. if (!$aI['E']) {
  346. if ($vZ === false) {
  347. $aI['E'] = 2;
  348. $aI['EM'] = 'Decompression of data failed.';
  349. } elseif ($this->_strlen($vZ) !== (int)$aP['UCS']) {
  350. $aI['E'] = 3;
  351. $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
  352. } elseif (crc32($vZ) !== $aP['CRC']) {
  353. $aI['E'] = 4;
  354. $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
  355. }
  356. }
  357. }
  358. */
  359.  
  360. // DOS to UNIX timestamp
  361. $aI['T'] = mktime(
  362. ($aP['FT'] & 0xf800) >> 11,
  363. ($aP['FT'] & 0x07e0) >> 5,
  364. ($aP['FT'] & 0x001f) << 1,
  365. ($aP['FD'] & 0x01e0) >> 5,
  366. $aP['FD'] & 0x001f,
  367. (($aP['FD'] & 0xfe00) >> 9) + 1980
  368. );
  369.  
  370. $this->package['entries'][] = [
  371. 'data' => $vZ,
  372. 'ucs' => (int)$aP['UCS'], // ucompresses size
  373. 'cm' => $aP['CM'], // compressed method
  374. 'cs' => isset($aP['CS']) ? (int) $aP['CS'] : 0, // compresses size
  375. 'crc' => $aP['CRC'],
  376. 'error' => $aI['E'],
  377. 'error_msg' => $aI['EM'],
  378. 'name' => $aI['N'],
  379. 'path' => $aI['P'],
  380. 'time' => $aI['T']
  381. ];
  382. } // end for each entries
  383.  
  384. return true;
  385. }
  386.  
  387. protected function _strlen($str)
  388. {
  389. return (ini_get('mbstring.func_overload') & 2) ? mb_strlen($str, '8bit') : strlen($str);
  390. }
  391.  
  392. public function error($num = null, $str = null)
  393. {
  394. if ($num) {
  395. $this->errno = $num;
  396. $this->error = $str;
  397. if ($this->debug) {
  398. trigger_error(__CLASS__ . ': ' . $this->error, E_USER_WARNING);
  399. }
  400. }
  401.  
  402. return $this->error;
  403. }
  404.  
  405. protected function _substr($str, $start, $length = null)
  406. {
  407. return (ini_get('mbstring.func_overload') & 2) ? mb_substr($str, $start, ($length === null) ? mb_strlen($str, '8bit') : $length, '8bit') : substr($str, $start, ($length === null) ? strlen($str) : $length);
  408. }
  409.  
  410. protected function _parse()
  411. {
  412. // Document data holders
  413. $this->sharedstrings = [];
  414. $this->sheets = [];
  415. // $this->styles = array();
  416. // $m1 = 0; // memory_get_peak_usage( true );
  417. // Read relations and search for officeDocument
  418. if ($relations = $this->getEntryXML('_rels/.rels')) {
  419. foreach ($relations->Relationship as $rel) {
  420. $rel_type = basename(trim((string)$rel['Type'])); // officeDocument
  421. $rel_target = $this->_getTarget('', (string)$rel['Target']); // /xl/workbook.xml or xl/workbook.xml
  422.  
  423. if ($rel_type === 'officeDocument' && $workbook = $this->getEntryXML($rel_target)) {
  424. $index_rId = []; // [0 => rId1]
  425.  
  426. $index = 0;
  427. foreach ($workbook->sheets->sheet as $s) {
  428. $this->sheetNames[$index] = (string)$s['name'];
  429. $index_rId[$index] = (string)$s['id'];
  430. $index++;
  431. }
  432. if ((int)$workbook->workbookPr['date1904'] === 1) {
  433. $this->date1904 = 1;
  434. }
  435.  
  436.  
  437. if ($workbookRelations = $this->getEntryXML(dirname($rel_target) . '/_rels/workbook.xml.rels')) {
  438. // Loop relations for workbook and extract sheets...
  439. foreach ($workbookRelations->Relationship as $workbookRelation) {
  440. $wrel_type = basename(trim((string)$workbookRelation['Type'])); // worksheet
  441. $wrel_path = $this->_getTarget(dirname($rel_target), (string)$workbookRelation['Target']);
  442. if (!$this->entryExists($wrel_path)) {
  443. continue;
  444. }
  445.  
  446.  
  447. if ($wrel_type === 'worksheet') { // Sheets
  448. if ($sheet = $this->getEntryXML($wrel_path)) {
  449. $index = array_search((string)$workbookRelation['Id'], $index_rId, true);
  450. $this->sheets[$index] = $sheet;
  451. $this->sheetFiles[$index] = $wrel_path;
  452. }
  453. } elseif ($wrel_type === 'sharedStrings') {
  454. if ($sharedStrings = $this->getEntryXML($wrel_path)) {
  455. foreach ($sharedStrings->si as $val) {
  456. if (isset($val->t)) {
  457. $this->sharedstrings[] = (string)$val->t;
  458. } elseif (isset($val->r)) {
  459. $this->sharedstrings[] = $this->_parseRichText($val);
  460. }
  461. }
  462. }
  463. } elseif ($wrel_type === 'styles') {
  464. $this->styles = $this->getEntryXML($wrel_path);
  465.  
  466. // number formats
  467. $this->nf = [];
  468. if (isset($this->styles->numFmts->numFmt)) {
  469. foreach ($this->styles->numFmts->numFmt as $v) {
  470. $this->nf[(int)$v['numFmtId']] = (string)$v['formatCode'];
  471. }
  472. }
  473.  
  474. $this->cellFormats = [];
  475. if (isset($this->styles->cellXfs->xf)) {
  476. foreach ($this->styles->cellXfs->xf as $v) {
  477. $x = [
  478. 'format' => null
  479. ];
  480. foreach ($v->attributes() as $k1 => $v1) {
  481. $x[ $k1 ] = (int) $v1;
  482. }
  483. if (isset($x['numFmtId'])) {
  484. if (isset($this->nf[$x['numFmtId']])) {
  485. $x['format'] = $this->nf[$x['numFmtId']];
  486. } elseif (isset(self::$CF[$x['numFmtId']])) {
  487. $x['format'] = self::$CF[$x['numFmtId']];
  488. }
  489. }
  490.  
  491. $this->cellFormats[] = $x;
  492. }
  493. }
  494. } elseif ($wrel_type === 'theme') {
  495. $this->theme = $this->getEntryXML($wrel_path);
  496. }
  497. }
  498.  
  499. break;
  500. }
  501. // reptile hack :: find active sheet from workbook.xml
  502. foreach ($workbook->bookViews->workbookView as $s) {
  503. if (!empty($s['activeTab'])) {
  504. $this->activeSheet = (int)$s['activeTab'];
  505. }
  506. }
  507. }
  508. }
  509. }
  510.  
  511. // $m2 = memory_get_peak_usage(true);
  512. // echo __FUNCTION__.' M='.round( ($m2-$m1) / 1048576, 2).'MB'.PHP_EOL;
  513.  
  514. if (count($this->sheets)) {
  515. // Sort sheets
  516. ksort($this->sheets);
  517.  
  518. return true;
  519. }
  520.  
  521. return false;
  522. }
  523.  
  524. public function getEntryXML($name)
  525. {
  526. if ($entry_xml = $this->getEntryData($name)) {
  527. $this->deleteEntry($name); // economy memory
  528. // dirty remove namespace prefixes and empty rows
  529. $entry_xml = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $entry_xml); // remove namespaces
  530. $entry_xml .= ' '; // force run garbage collector
  531. $entry_xml = preg_replace('/[a-zA-Z0-9]+:([a-zA-Z0-9]+="[^"]+")/', '$1', $entry_xml); // remove namespaced attrs
  532. $entry_xml .= ' ';
  533. $entry_xml = preg_replace('/<[a-zA-Z0-9]+:([^>]+)>/', '<$1>', $entry_xml); // fix namespaced openned tags
  534. $entry_xml .= ' ';
  535. $entry_xml = preg_replace('/<\/[a-zA-Z0-9]+:([^>]+)>/', '</$1>', $entry_xml); // fix namespaced closed tags
  536. $entry_xml .= ' ';
  537.  
  538. if (strpos($name, '/sheet')) { // dirty skip empty rows
  539. // remove <row...> <c /><c /></row>
  540. $entry_xml = preg_replace('/<row[^>]+>\s*(<c[^\/]+\/>\s*)+<\/row>/', '', $entry_xml, -1, $cnt);
  541. $entry_xml .= ' ';
  542. // remove <row />
  543. $entry_xml = preg_replace('/<row[^\/>]*\/>/', '', $entry_xml, -1, $cnt2);
  544. $entry_xml .= ' ';
  545. // remove <row...></row>
  546. $entry_xml = preg_replace('/<row[^>]*><\/row>/', '', $entry_xml, -1, $cnt3);
  547. $entry_xml .= ' ';
  548. if ($cnt || $cnt2 || $cnt3) {
  549. $entry_xml = preg_replace('/<dimension[^\/]+\/>/', '', $entry_xml);
  550. $entry_xml .= ' ';
  551. }
  552. // file_put_contents( basename( $name ), $entry_xml ); // @to do comment!!!
  553. }
  554. $entry_xml = trim($entry_xml);
  555.  
  556. // $m1 = memory_get_usage();
  557. // XML External Entity (XXE) Prevention, libxml_disable_entity_loader deprecated in PHP 8
  558. if (LIBXML_VERSION < 20900 && function_exists('libxml_disable_entity_loader')) {
  559. $_old = libxml_disable_entity_loader();
  560. }
  561.  
  562. $_old_uie = libxml_use_internal_errors(true);
  563.  
  564. $entry_xmlobj = simplexml_load_string($entry_xml, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_PARSEHUGE);
  565.  
  566. libxml_use_internal_errors($_old_uie);
  567.  
  568. if (LIBXML_VERSION < 20900 && function_exists('libxml_disable_entity_loader')) {
  569. /** @noinspection PhpUndefinedVariableInspection */
  570. libxml_disable_entity_loader($_old);
  571. }
  572.  
  573. // $m2 = memory_get_usage();
  574. // echo round( ($m2-$m1) / (1024 * 1024), 2).' MB'.PHP_EOL;
  575.  
  576. if ($entry_xmlobj) {
  577. return $entry_xmlobj;
  578. }
  579. $e = libxml_get_last_error();
  580. if ($e) {
  581. $this->error(3, 'XML-entry ' . $name . ' parser error ' . $e->message . ' line ' . $e->line);
  582. }
  583. } else {
  584. $this->error(4, 'XML-entry not found ' . $name);
  585. }
  586.  
  587. return false;
  588. }
  589.  
  590. // sheets numeration: 1,2,3....
  591.  
  592. public function getEntryData($name)
  593. {
  594. $name = ltrim(str_replace('\\', '/', $name), '/');
  595. $dir = $this->_strtoupper(dirname($name));
  596. $name = $this->_strtoupper(basename($name));
  597. foreach ($this->package['entries'] as &$entry) {
  598. if ($this->_strtoupper($entry['path']) === $dir && $this->_strtoupper($entry['name']) === $name) {
  599. if ($entry['error']) {
  600. return false;
  601. }
  602. switch ($entry['cm']) {
  603. case -1:
  604. case 0: // Stored
  605. // Here is nothing to do, the file ist flat.
  606. break;
  607. case 8: // Deflated
  608. $entry['data'] = gzinflate($entry['data']);
  609. break;
  610. case 12: // BZIP2
  611. if (extension_loaded('bz2')) {
  612. $entry['data'] = bzdecompress($entry['data']);
  613. } else {
  614. $entry['error'] = 7;
  615. $entry['error_message'] = 'PHP BZIP2 extension not available.';
  616. }
  617. break;
  618. default:
  619. $entry['error'] = 6;
  620. $entry['error_msg'] = 'De-/Compression method '.$entry['cm'].' is not supported.';
  621. }
  622. if (!$entry['error'] && $entry['cm'] > -1) {
  623. $entry['cm'] = -1;
  624. if ($entry['data'] === false) {
  625. $entry['error'] = 2;
  626. $entry['error_msg'] = 'Decompression of data failed.';
  627. } elseif ($this->_strlen($entry['data']) !== (int)$entry['ucs']) {
  628. $entry['error'] = 3;
  629. $entry['error_msg'] = 'Uncompressed size is not equal with the value in header information.';
  630. } elseif (crc32($entry['data']) !== $entry['crc']) {
  631. $entry['error'] = 4;
  632. $entry['error_msg'] = 'CRC32 checksum is not equal with the value in header information.';
  633. }
  634. }
  635.  
  636. return $entry['data'];
  637. }
  638. }
  639. unset($entry);
  640. $this->error(5, 'Entry not found ' . ($dir ? $dir . '/' : '') . $name);
  641.  
  642. return false;
  643. }
  644. public function deleteEntry($name)
  645. {
  646. $name = ltrim(str_replace('\\', '/', $name), '/');
  647. $dir = $this->_strtoupper(dirname($name));
  648. $name = $this->_strtoupper(basename($name));
  649. foreach ($this->package['entries'] as $k => $entry) {
  650. if ($this->_strtoupper($entry['path']) === $dir && $this->_strtoupper($entry['name']) === $name) {
  651. unset($this->package['entries'][$k]);
  652. return true;
  653. }
  654. }
  655. return false;
  656. }
  657.  
  658. protected function _strtoupper($str)
  659. {
  660. return (ini_get('mbstring.func_overload') & 2) ? mb_strtoupper($str, '8bit') : strtoupper($str);
  661. }
  662.  
  663. protected function _getTarget($base, $target)
  664. {
  665. $target = trim($target);
  666. if (strpos($target, '/') === 0) {
  667. return $this->_substr($target, 1);
  668. }
  669. $target = ($base ? $base . '/' : '') . $target;
  670. // a/b/../c -> a/c
  671. $parts = explode('/', $target);
  672. $abs = [];
  673. foreach ($parts as $p) {
  674. if ('.' === $p) {
  675. continue;
  676. }
  677. if ('..' === $p) {
  678. array_pop($abs);
  679. } else {
  680. $abs[] = $p;
  681. }
  682. }
  683. return implode('/', $abs);
  684. }
  685.  
  686. /*
  687. * @param string $name Filename in archive
  688. * @return SimpleXMLElement|bool
  689. */
  690.  
  691. public function entryExists($name)
  692. {
  693. // 0.6.6
  694. $dir = $this->_strtoupper(dirname($name));
  695. $name = $this->_strtoupper(basename($name));
  696. foreach ($this->package['entries'] as $entry) {
  697. if ($this->_strtoupper($entry['path']) === $dir && $this->_strtoupper($entry['name']) === $name) {
  698. return true;
  699. }
  700. }
  701.  
  702. return false;
  703. }
  704.  
  705. protected function _parseRichText($is = null)
  706. {
  707. $value = [];
  708.  
  709. if (isset($is->t)) {
  710. $value[] = (string)$is->t;
  711. } elseif (isset($is->r)) {
  712. foreach ($is->r as $run) {
  713. $value[] = (string)$run->t;
  714. }
  715. }
  716.  
  717. return implode('', $value);
  718. }
  719.  
  720. public static function parseFile($filename, $debug = false)
  721. {
  722. return self::parse($filename, false, $debug);
  723. }
  724.  
  725. public static function parse($filename, $is_data = false, $debug = false)
  726. {
  727. $xlsx = new self();
  728. $xlsx->debug = $debug;
  729. if ($xlsx->_unzip($filename, $is_data)) {
  730. $xlsx->_parse();
  731. }
  732. if ($xlsx->success()) {
  733. return $xlsx;
  734. }
  735. self::parseError($xlsx->error());
  736. self::parseErrno($xlsx->errno());
  737.  
  738. return false;
  739. }
  740.  
  741. public function success()
  742. {
  743. return !$this->error;
  744. }
  745.  
  746. // https://github.com/shuchkin/simplexlsx#gets-extend-cell-info-by--rowsex
  747.  
  748. public static function parseError($set = false)
  749. {
  750. static $error = false;
  751.  
  752. return $set ? $error = $set : $error;
  753. }
  754.  
  755. public static function parseErrno($set = false)
  756. {
  757. static $errno = false;
  758.  
  759. return $set ? $errno = $set : $errno;
  760. }
  761.  
  762. public function errno()
  763. {
  764. return $this->errno;
  765. }
  766.  
  767. public static function parseData($data, $debug = false)
  768. {
  769. return self::parse($data, true, $debug);
  770. }
  771.  
  772.  
  773.  
  774. public function worksheet($worksheetIndex = 0)
  775. {
  776.  
  777.  
  778. if (isset($this->sheets[$worksheetIndex])) {
  779. $ws = $this->sheets[$worksheetIndex];
  780.  
  781. if (!isset($this->hyperlinks[$worksheetIndex]) && isset($ws->hyperlinks)) {
  782. $this->hyperlinks[$worksheetIndex] = [];
  783. $sheet_rels = str_replace('worksheets', 'worksheets/_rels', $this->sheetFiles[$worksheetIndex]) . '.rels';
  784. $link_ids = [];
  785.  
  786. if ($rels = $this->getEntryXML($sheet_rels)) {
  787. // hyperlink
  788. // $rel_base = dirname( $sheet_rels );
  789. foreach ($rels->Relationship as $rel) {
  790. $rel_type = basename(trim((string)$rel['Type']));
  791. if ($rel_type === 'hyperlink') {
  792. $rel_id = (string)$rel['Id'];
  793. $rel_target = (string)$rel['Target'];
  794. $link_ids[$rel_id] = $rel_target;
  795. }
  796. }
  797. }
  798. foreach ($ws->hyperlinks->hyperlink as $hyperlink) {
  799. $ref = (string)$hyperlink['ref'];
  800. if ($this->_strpos($ref, ':') > 0) { // A1:A8 -> A1
  801. $ref = explode(':', $ref);
  802. $ref = $ref[0];
  803. }
  804. // $this->hyperlinks[ $worksheetIndex ][ $ref ] = (string) $hyperlink['display'];
  805. $loc = (string)$hyperlink['location'];
  806. $id = (string)$hyperlink['id'];
  807. if ($id) {
  808. $href = $link_ids[$id] . ($loc ? '#' . $loc : '');
  809. } else {
  810. $href = $loc;
  811. }
  812. $this->hyperlinks[$worksheetIndex][$ref] = $href;
  813. }
  814. }
  815.  
  816. return $ws;
  817. }
  818. $this->error(6, 'Worksheet not found ' . $worksheetIndex);
  819.  
  820. return false;
  821. }
  822.  
  823. protected function _strpos($haystack, $needle, $offset = 0)
  824. {
  825. return (ini_get('mbstring.func_overload') & 2) ? mb_strpos($haystack, $needle, $offset, '8bit') : strpos($haystack, $needle, $offset);
  826. }
  827.  
  828. /**
  829. * returns [numCols,numRows] of worksheet
  830. *
  831. * @param int $worksheetIndex
  832. *
  833. * @return array
  834. */
  835. public function dimension($worksheetIndex = 0)
  836. {
  837.  
  838. if (($ws = $this->worksheet($worksheetIndex)) === false) {
  839. return [0, 0];
  840. }
  841. /* @var SimpleXMLElement $ws */
  842.  
  843. $ref = (string)$ws->dimension['ref'];
  844.  
  845. if ($this->_strpos($ref, ':') !== false) {
  846. $d = explode(':', $ref);
  847. $idx = $this->getIndex($d[1]);
  848.  
  849. return [$idx[0] + 1, $idx[1] + 1];
  850. }
  851. /*
  852. if ( $ref !== '' ) { // 0.6.8
  853. $index = $this->getIndex( $ref );
  854.  
  855. return [ $index[0] + 1, $index[1] + 1 ];
  856. }
  857. */
  858.  
  859. // slow method
  860. $maxC = $maxR = 0;
  861. foreach ($ws->sheetData->row as $row) {
  862. foreach ($row->c as $c) {
  863. $idx = $this->getIndex((string)$c['r']);
  864. $x = $idx[0];
  865. $y = $idx[1];
  866. if ($x > 0) {
  867. if ($x > $maxC) {
  868. $maxC = $x;
  869. }
  870. if ($y > $maxR) {
  871. $maxR = $y;
  872. }
  873. }
  874. }
  875. }
  876.  
  877. return [$maxC + 1, $maxR + 1];
  878. }
  879.  
  880. public function getIndex($cell = 'A1')
  881. {
  882.  
  883. if (preg_match('/([A-Z]+)(\d+)/', $cell, $m)) {
  884. $col = $m[1];
  885. $row = $m[2];
  886.  
  887. $colLen = $this->_strlen($col);
  888. $index = 0;
  889.  
  890. for ($i = $colLen - 1; $i >= 0; $i--) {
  891. $index += (ord($col[$i]) - 64) * pow(26, $colLen - $i - 1);
  892. }
  893.  
  894. return [$index - 1, $row - 1];
  895. }
  896.  
  897. // $this->error( 'Invalid cell index ' . $cell );
  898.  
  899. return [-1, -1];
  900. }
  901.  
  902. public function value($cell)
  903. {
  904. // Determine data type
  905. $dataType = (string)$cell['t'];
  906.  
  907. if ($dataType === '' || $dataType === 'n') { // number
  908. $s = (int)$cell['s'];
  909. if ($s > 0 && isset($this->cellFormats[$s])) {
  910. if (array_key_exists('format', $this->cellFormats[$s])) {
  911. $format = $this->cellFormats[$s]['format'];
  912. if (preg_match('/(m|AM|PM)/', preg_replace('/\"[^"]+\"/', '', $format))) { // [mm]onth,AM|PM
  913. $dataType = 'D';
  914. }
  915. } else {
  916. $dataType = 'n';
  917. }
  918. }
  919. }
  920.  
  921. $value = '';
  922.  
  923. switch ($dataType) {
  924. case 's':
  925. // Value is a shared string
  926. if ((string)$cell->v !== '') {
  927. $value = $this->sharedstrings[(int)$cell->v];
  928. }
  929. break;
  930.  
  931. case 'str': // formula?
  932. if ((string)$cell->v !== '') {
  933. $value = (string)$cell->v;
  934. }
  935. break;
  936.  
  937. case 'b':
  938. // Value is boolean
  939. $value = (string)$cell->v;
  940. if ($value === '0') {
  941. $value = false;
  942. } elseif ($value === '1') {
  943. $value = true;
  944. } else {
  945. $value = (bool)$cell->v;
  946. }
  947.  
  948. break;
  949.  
  950. case 'inlineStr':
  951. // Value is rich text inline
  952. $value = $this->_parseRichText($cell->is);
  953.  
  954. break;
  955.  
  956. case 'e':
  957. // Value is an error message
  958. if ((string)$cell->v !== '') {
  959. $value = (string)$cell->v;
  960. }
  961. break;
  962.  
  963. case 'D':
  964. // Date as float
  965. if (!empty($cell->v)) {
  966. $value = $this->datetimeFormat ? gmdate($this->datetimeFormat, $this->unixstamp((float)$cell->v)) : (float)$cell->v;
  967. }
  968. break;
  969.  
  970. case 'd':
  971. // Date as ISO YYYY-MM-DD
  972. if ((string)$cell->v !== '') {
  973. $value = (string)$cell->v;
  974. }
  975. break;
  976.  
  977. default:
  978. // Value is a string
  979. $value = (string)$cell->v;
  980.  
  981. // Check for numeric values
  982. if (is_numeric($value)) {
  983. /** @noinspection TypeUnsafeComparisonInspection */
  984. if ($value == (int)$value) {
  985. $value = (int)$value;
  986. } /** @noinspection TypeUnsafeComparisonInspection */ elseif ($value == (float)$value) {
  987. $value = (float)$value;
  988. }
  989. }
  990. }
  991.  
  992. return $value;
  993. }
  994.  
  995. public function unixstamp($excelDateTime)
  996. {
  997.  
  998. $d = floor($excelDateTime); // days since 1900 or 1904
  999. $t = $excelDateTime - $d;
  1000.  
  1001. if ($this->date1904) {
  1002. $d += 1462;
  1003. }
  1004.  
  1005. $t = (abs($d) > 0) ? ($d - 25569) * 86400 + round($t * 86400) : round($t * 86400);
  1006.  
  1007. return (int)$t;
  1008. }
  1009.  
  1010. public function href($worksheetIndex, $cell)
  1011. {
  1012. $ref = (string)$cell['r'];
  1013. return isset($this->hyperlinks[$worksheetIndex][$ref]) ? $this->hyperlinks[$worksheetIndex][$ref] : '';
  1014. }
  1015.  
  1016. public function toHTML($worksheetIndex = 0)
  1017. {
  1018. $s = '<table class=excel>';
  1019. foreach ($this->readRows($worksheetIndex) as $r) {
  1020. $s .= '<tr>';
  1021. foreach ($r as $c) {
  1022. $s .= '<td nowrap>' . ($c === '' ? '&nbsp' : htmlspecialchars($c, ENT_QUOTES)) . '</td>';
  1023. }
  1024. $s .= "</tr>\r\n";
  1025. }
  1026. $s .= '</table>';
  1027.  
  1028. return $s;
  1029. }
  1030. public function toHTMLEx($worksheetIndex = 0)
  1031. {
  1032. $s = '<table class=excel>';
  1033. $y = 0;
  1034. foreach ($this->readRowsEx($worksheetIndex) as $r) {
  1035. $s .= '<tr>';
  1036. $x = 0;
  1037. foreach ($r as $c) {
  1038. $tag = 'td';
  1039. $css = $c['css'];
  1040. if ($y === 0) {
  1041. $tag = 'th';
  1042. $css .= $c['width'] ? 'width: '.round($c['width'] * 0.47, 2).'em;' : '';
  1043. }
  1044.  
  1045. if ($x === 0 && $c['height']) {
  1046. $css .= 'height: '.round($c['height'] * 1.3333).'px;';
  1047. }
  1048. $s .= '<'.$tag.' style="'.$css.'" nowrap>' . ($c['value'] === '' ? '&nbsp' : htmlspecialchars($c['value'], ENT_QUOTES)) . '</'.$tag.'>';
  1049. $x++;
  1050. }
  1051. $s .= "</tr>\r\n";
  1052. $y++;
  1053. }
  1054. $s .= '</table>';
  1055.  
  1056. return $s;
  1057. }
  1058. public function rows($worksheetIndex = 0, $limit = 0)
  1059. {
  1060. return iterator_to_array($this->readRows($worksheetIndex, $limit), false);
  1061. }
  1062. // thx Gonzo
  1063. /**
  1064. * @param $worksheetIndex
  1065. * @param $limit
  1066. * @return \Generator
  1067. */
  1068. public function readRows($worksheetIndex = 0, $limit = 0)
  1069. {
  1070.  
  1071. if (($ws = $this->worksheet($worksheetIndex)) === false) {
  1072. return;
  1073. }
  1074. $dim = $this->dimension($worksheetIndex);
  1075. $numCols = $dim[0];
  1076. $numRows = $dim[1];
  1077.  
  1078. $emptyRow = [];
  1079. for ($i = 0; $i < $numCols; $i++) {
  1080. $emptyRow[] = '';
  1081. }
  1082.  
  1083. $curR = 0;
  1084. $_limit = $limit;
  1085. /* @var SimpleXMLElement $ws */
  1086. foreach ($ws->sheetData->row as $row) {
  1087. $r = $emptyRow;
  1088. $curC = 0;
  1089. foreach ($row->c as $c) {
  1090. // detect skipped cols
  1091. $idx = $this->getIndex((string)$c['r']);
  1092. $x = $idx[0];
  1093. $y = $idx[1];
  1094.  
  1095. if ($x > -1) {
  1096. $curC = $x;
  1097. while ($curR < $y) {
  1098. yield $emptyRow;
  1099. $curR++;
  1100. $_limit--;
  1101. if ($_limit === 0) {
  1102. return;
  1103. }
  1104. }
  1105. }
  1106. $r[$curC] = $this->value($c);
  1107. $curC++;
  1108. }
  1109. yield $r;
  1110.  
  1111. $curR++;
  1112. $_limit--;
  1113. if ($_limit === 0) {
  1114. return;
  1115. }
  1116. }
  1117. while ($curR < $numRows) {
  1118. yield $emptyRow;
  1119. $curR++;
  1120. $_limit--;
  1121. if ($_limit === 0) {
  1122. return;
  1123. }
  1124. }
  1125. }
  1126.  
  1127. public function rowsEx($worksheetIndex = 0, $limit = 0)
  1128. {
  1129. return iterator_to_array($this->readRowsEx($worksheetIndex, $limit), false);
  1130. }
  1131. // https://github.com/shuchkin/simplexlsx#gets-extend-cell-info-by--rowsex
  1132. /**
  1133. * @param $worksheetIndex
  1134. * @param $limit
  1135. * @return \Generator|null
  1136. */
  1137. public function readRowsEx($worksheetIndex = 0, $limit = 0)
  1138. {
  1139. if (!$this->rowsExReader) {
  1140. require_once __DIR__ . '/SimpleXLSXEx.php';
  1141. $this->rowsExReader = new SimpleXLSXEx($this);
  1142. }
  1143. return $this->rowsExReader->readRowsEx($worksheetIndex, $limit);
  1144. }
  1145.  
  1146. /**
  1147. * Returns cell value
  1148. * VERY SLOW! Use ->rows() or ->rowsEx()
  1149. *
  1150. * @param int $worksheetIndex
  1151. * @param string|array $cell ref or coords, D12 or [3,12]
  1152. *
  1153. * @return mixed Returns NULL if not found
  1154. */
  1155. public function getCell($worksheetIndex = 0, $cell = 'A1')
  1156. {
  1157.  
  1158. if (($ws = $this->worksheet($worksheetIndex)) === false) {
  1159. return false;
  1160. }
  1161. if (is_array($cell)) {
  1162. $cell = $this->_num2name($cell[0]) . $cell[1];// [3,21] -> D21
  1163. }
  1164. if (is_string($cell)) {
  1165. $result = $ws->sheetData->xpath("row/c[@r='" . $cell . "']");
  1166. if (count($result)) {
  1167. return $this->value($result[0]);
  1168. }
  1169. }
  1170.  
  1171. return null;
  1172. }
  1173.  
  1174. protected function _num2name($num)
  1175. {
  1176. $numeric = ($num - 1) % 26;
  1177. $letter = chr(65 + $numeric);
  1178. $num2 = (int)(($num - 1) / 26);
  1179. if ($num2 > 0) {
  1180. return $this->_num2name($num2) . $letter;
  1181. }
  1182. return $letter;
  1183. }
  1184.  
  1185. public function getSheets()
  1186. {
  1187. return $this->sheets;
  1188. }
  1189.  
  1190. public function sheetsCount()
  1191. {
  1192. return count($this->sheets);
  1193. }
  1194.  
  1195. public function sheetName($worksheetIndex)
  1196. {
  1197. if (isset($this->sheetNames[$worksheetIndex])) {
  1198. return $this->sheetNames[$worksheetIndex];
  1199. }
  1200.  
  1201. return false;
  1202. }
  1203.  
  1204. public function sheetNames()
  1205. {
  1206.  
  1207. return $this->sheetNames;
  1208. }
  1209.  
  1210. public function getStyles()
  1211. {
  1212. return $this->styles;
  1213. }
  1214.  
  1215. public function getPackage()
  1216. {
  1217. return $this->package;
  1218. }
  1219.  
  1220. public function setDateTimeFormat($value)
  1221. {
  1222. $this->datetimeFormat = is_string($value) ? $value : false;
  1223. }
  1224. }
Buy Me A Coffee