SimpleXLSX.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. <?php
  2. /**
  3. * SimpleXLSX php class v0.8.23
  4. * MS Excel 2007 workbooks reader
  5. *
  6. * Copyright (c) 2012 - 2021 SimpleXLSX
  7. *
  8. * @category SimpleXLSX
  9. * @package SimpleXLSX
  10. * @copyright Copyright (c) 2012 - 2020 SimpleXLSX (https://github.com/shuchkin/simplexlsx/)
  11. * @license MIT
  12. * @version 0.8.23
  13. */
  14. /** Examples
  15. *
  16. * Example 1:
  17. * if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
  18. * print_r( $xlsx->rows() );
  19. * } else {
  20. * echo SimpleXLSX::parseError();
  21. * }
  22. *
  23. * Example 2: html table
  24. * if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
  25. * echo $xlsx->toHTML();
  26. * } else {
  27. * echo SimpleXLSX::parseError();
  28. * }
  29. *
  30. * Example 3: rowsEx
  31. * $xlsx = SimpleXLSX::parse('book.xlsx');
  32. * print_r( $xlsx->rowsEx() );
  33. *
  34. * Example 4: select worksheet
  35. * $xlsx = SimpleXLSX::parse('book.xlsx');
  36. * print_r( $xlsx->rows(1) ); // second worksheet
  37. *
  38. * Example 5: IDs and worksheet names
  39. * $xlsx = SimpleXLSX::parse('book.xlsx');
  40. * print_r( $xlsx->sheetNames() ); // array( 0 => 'Sheet 1', 1 => 'Catalog' );
  41. *
  42. * Example 6: get sheet name by index
  43. * $xlsx = SimpleXLSX::parse('book.xlsx');
  44. * echo 'Sheet Name 2 = '.$xlsx->sheetName(1);
  45. *
  46. * Example 7: getCell (very slow)
  47. * echo $xlsx->getCell(1,'D12'); // reads D12 cell from second sheet
  48. *
  49. * Example 8: read data
  50. * if ( $xlsx = SimpleXLSX::parseData( file_get_contents('http://www.example.com/example.xlsx') ) ) {
  51. * $dim = $xlsx->dimension(1);
  52. * $num_cols = $dim[0];
  53. * $num_rows = $dim[1];
  54. * echo $xlsx->sheetName(1).':'.$num_cols.'x'.$num_rows;
  55. * } else {
  56. * echo SimpleXLSX::parseError();
  57. * }
  58. *
  59. * Example 9: old style
  60. * $xlsx = new SimpleXLSX('book.xlsx');
  61. * if ( $xlsx->success() ) {
  62. * print_r( $xlsx->rows() );
  63. * } else {
  64. * echo 'xlsx error: '.$xlsx->error();
  65. * }
  66. */
  67. /** @noinspection PhpUndefinedFieldInspection */
  68. /** @noinspection PhpComposerExtensionStubsInspection */
  69. /** @noinspection MultiAssignmentUsageInspection */
  70. class SimpleXLSX {
  71. // Don't remove this string! Created by Sergey Shuchkin sergey.shuchkin@gmail.com
  72. public static $CF = [ // Cell formats
  73. 0 => 'General',
  74. 1 => '0',
  75. 2 => '0.00',
  76. 3 => '#,##0',
  77. 4 => '#,##0.00',
  78. 9 => '0%',
  79. 10 => '0.00%',
  80. 11 => '0.00E+00',
  81. 12 => '# ?/?',
  82. 13 => '# ??/??',
  83. 14 => 'mm-dd-yy',
  84. 15 => 'd-mmm-yy',
  85. 16 => 'd-mmm',
  86. 17 => 'mmm-yy',
  87. 18 => 'h:mm AM/PM',
  88. 19 => 'h:mm:ss AM/PM',
  89. 20 => 'h:mm',
  90. 21 => 'h:mm:ss',
  91. 22 => 'm/d/yy h:mm',
  92. 37 => '#,##0 ;(#,##0)',
  93. 38 => '#,##0 ;[Red](#,##0)',
  94. 39 => '#,##0.00;(#,##0.00)',
  95. 40 => '#,##0.00;[Red](#,##0.00)',
  96. 44 => '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
  97. 45 => 'mm:ss',
  98. 46 => '[h]:mm:ss',
  99. 47 => 'mmss.0',
  100. 48 => '##0.0E+0',
  101. 49 => '@',
  102. 27 => '[$-404]e/m/d',
  103. 30 => 'm/d/yy',
  104. 36 => '[$-404]e/m/d',
  105. 50 => '[$-404]e/m/d',
  106. 57 => '[$-404]e/m/d',
  107. 59 => 't0',
  108. 60 => 't0.00',
  109. 61 => 't#,##0',
  110. 62 => 't#,##0.00',
  111. 67 => 't0%',
  112. 68 => 't0.00%',
  113. 69 => 't# ?/?',
  114. 70 => 't# ??/??',
  115. ];
  116. public $cellFormats = [];
  117. public $datetimeFormat = 'Y-m-d H:i:s';
  118. public $debug;
  119. /* @var SimpleXMLElement[] $sheets */
  120. protected $sheets;
  121. protected $sheetNames = [];
  122. protected $sheetFiles = [];
  123. // scheme
  124. protected $styles;
  125. protected $hyperlinks;
  126. /* @var array[] $package */
  127. protected $package;
  128. protected $sharedstrings;
  129. protected $date1904 = 0;
  130. /*
  131. private $date_formats = array(
  132. 0xe => "d/m/Y",
  133. 0xf => "d-M-Y",
  134. 0x10 => "d-M",
  135. 0x11 => "M-Y",
  136. 0x12 => "h:i a",
  137. 0x13 => "h:i:s a",
  138. 0x14 => "H:i",
  139. 0x15 => "H:i:s",
  140. 0x16 => "d/m/Y H:i",
  141. 0x2d => "i:s",
  142. 0x2e => "H:i:s",
  143. 0x2f => "i:s.S"
  144. );
  145. private $number_formats = array(
  146. 0x1 => "%1.0f", // "0"
  147. 0x2 => "%1.2f", // "0.00",
  148. 0x3 => "%1.0f", //"#,##0",
  149. 0x4 => "%1.2f", //"#,##0.00",
  150. 0x5 => "%1.0f", //"$#,##0;($#,##0)",
  151. 0x6 => '$%1.0f', //"$#,##0;($#,##0)",
  152. 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  153. 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  154. 0x9 => '%1.0f%%', //"0%"
  155. 0xa => '%1.2f%%', //"0.00%"
  156. 0xb => '%1.2f', //"0.00E00",
  157. 0x25 => '%1.0f', //"#,##0;(#,##0)",
  158. 0x26 => '%1.0f', //"#,##0;(#,##0)",
  159. 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)",
  160. 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)",
  161. 0x29 => '%1.0f', //"#,##0;(#,##0)",
  162. 0x2a => '$%1.0f', //"$#,##0;($#,##0)",
  163. 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)",
  164. 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  165. 0x30 => '%1.0f'); //"##0.0E0";
  166. // }}}
  167. */
  168. protected $errno = 0;
  169. protected $error = false;
  170. public function __construct( $filename = null, $is_data = null, $debug = null ) {
  171. if ( $debug !== null ) {
  172. $this->debug = $debug;
  173. }
  174. $this->package = [
  175. 'filename' => '',
  176. 'mtime' => 0,
  177. 'size' => 0,
  178. 'comment' => '',
  179. 'entries' => []
  180. ];
  181. if ( $filename && $this->_unzip( $filename, $is_data ) ) {
  182. $this->_parse();
  183. }
  184. }
  185. public static function parseFile( $filename, $debug = false ) {
  186. return self::parse( $filename, false, $debug );
  187. }
  188. public static function parseData( $data, $debug = false ) {
  189. return self::parse( $data, true, $debug );
  190. }
  191. public static function parse( $filename, $is_data = false, $debug = false ) {
  192. $xlsx = new self();
  193. $xlsx->debug = $debug;
  194. if ( $xlsx->_unzip( $filename, $is_data ) ) {
  195. $xlsx->_parse();
  196. }
  197. if ( $xlsx->success() ) {
  198. return $xlsx;
  199. }
  200. self::parseError( $xlsx->error() );
  201. self::parseErrno( $xlsx->errno() );
  202. return false;
  203. }
  204. public static function parseError( $set = false ) {
  205. static $error = false;
  206. return $set ? $error = $set : $error;
  207. }
  208. public static function parseErrno( $set = false ) {
  209. static $errno = false;
  210. return $set ? $errno = $set : $errno;
  211. }
  212. protected function _unzip( $filename, $is_data = false ) {
  213. if ( $is_data ) {
  214. $this->package['filename'] = 'default.xlsx';
  215. $this->package['mtime'] = time();
  216. $this->package['size'] = $this->_strlen( $filename );
  217. $vZ = $filename;
  218. } else {
  219. if ( ! is_readable( $filename ) ) {
  220. $this->error( 1, 'File not found ' . $filename );
  221. return false;
  222. }
  223. // Package information
  224. $this->package['filename'] = $filename;
  225. $this->package['mtime'] = filemtime( $filename );
  226. $this->package['size'] = filesize( $filename );
  227. // Read file
  228. $vZ = file_get_contents( $filename );
  229. }
  230. // Cut end of central directory
  231. /* $aE = explode("\x50\x4b\x05\x06", $vZ);
  232. if (count($aE) == 1) {
  233. $this->error('Unknown format');
  234. return false;
  235. }
  236. */
  237. // Explode to each part
  238. $aE = explode( "\x50\x4b\x03\x04", $vZ );
  239. array_shift( $aE );
  240. $aEL = count( $aE );
  241. if ( $aEL === 0 ) {
  242. $this->error( 2, 'Unknown archive format' );
  243. return false;
  244. }
  245. // Search central directory end record
  246. $last = $aE[ $aEL - 1 ];
  247. $last = explode( "\x50\x4b\x05\x06", $last );
  248. if ( count( $last ) !== 2 ) {
  249. $this->error( 2, 'Unknown archive format' );
  250. return false;
  251. }
  252. // Search central directory
  253. $last = explode( "\x50\x4b\x01\x02", $last[0] );
  254. if ( count( $last ) < 2 ) {
  255. $this->error( 2, 'Unknown archive format' );
  256. return false;
  257. }
  258. $aE[ $aEL - 1 ] = $last[0];
  259. // Loop through the entries
  260. foreach ( $aE as $vZ ) {
  261. $aI = [];
  262. $aI['E'] = 0;
  263. $aI['EM'] = '';
  264. // Retrieving local file header information
  265. // $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
  266. $aP = unpack( 'v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL/v1EFL', $vZ );
  267. // Check if data is encrypted
  268. // $bE = ($aP['GPF'] && 0x0001) ? TRUE : FALSE;
  269. $bE = false;
  270. $nF = $aP['FNL'];
  271. $mF = $aP['EFL'];
  272. // Special case : value block after the compressed data
  273. if ( $aP['GPF'] & 0x0008 ) {
  274. $aP1 = unpack( 'V1CRC/V1CS/V1UCS', $this->_substr( $vZ, - 12 ) );
  275. $aP['CRC'] = $aP1['CRC'];
  276. $aP['CS'] = $aP1['CS'];
  277. $aP['UCS'] = $aP1['UCS'];
  278. // 2013-08-10
  279. $vZ = $this->_substr( $vZ, 0, - 12 );
  280. if ( $this->_substr( $vZ, - 4 ) === "\x50\x4b\x07\x08" ) {
  281. $vZ = $this->_substr( $vZ, 0, - 4 );
  282. }
  283. }
  284. // Getting stored filename
  285. $aI['N'] = $this->_substr( $vZ, 26, $nF );
  286. $aI['N'] = str_replace( '\\', '/', $aI['N'] );
  287. if ( $this->_substr( $aI['N'], - 1 ) === '/' ) {
  288. // is a directory entry - will be skipped
  289. continue;
  290. }
  291. // Truncate full filename in path and filename
  292. $aI['P'] = dirname( $aI['N'] );
  293. $aI['P'] = ( $aI['P'] === '.' ) ? '' : $aI['P'];
  294. $aI['N'] = basename( $aI['N'] );
  295. $vZ = $this->_substr( $vZ, 26 + $nF + $mF );
  296. if ( $this->_strlen( $vZ ) !== (int) $aP['CS'] ) { // check only if availabled
  297. $aI['E'] = 1;
  298. $aI['EM'] = 'Compressed size is not equal with the value in header information.';
  299. } elseif ( $bE ) {
  300. $aI['E'] = 5;
  301. $aI['EM'] = 'File is encrypted, which is not supported from this class.';
  302. } else {
  303. switch ( $aP['CM'] ) {
  304. case 0: // Stored
  305. // Here is nothing to do, the file ist flat.
  306. break;
  307. case 8: // Deflated
  308. $vZ = gzinflate( $vZ );
  309. break;
  310. case 12: // BZIP2
  311. if ( extension_loaded( 'bz2' ) ) {
  312. /** @noinspection PhpComposerExtensionStubsInspection */
  313. $vZ = bzdecompress( $vZ );
  314. } else {
  315. $aI['E'] = 7;
  316. $aI['EM'] = 'PHP BZIP2 extension not available.';
  317. }
  318. break;
  319. default:
  320. $aI['E'] = 6;
  321. $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
  322. }
  323. if ( ! $aI['E'] ) {
  324. if ( $vZ === false ) {
  325. $aI['E'] = 2;
  326. $aI['EM'] = 'Decompression of data failed.';
  327. } elseif ( $this->_strlen( $vZ ) !== (int) $aP['UCS'] ) {
  328. $aI['E'] = 3;
  329. $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
  330. } elseif ( crc32( $vZ ) !== $aP['CRC'] ) {
  331. $aI['E'] = 4;
  332. $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
  333. }
  334. }
  335. }
  336. $aI['D'] = $vZ;
  337. // DOS to UNIX timestamp
  338. $aI['T'] = mktime( ( $aP['FT'] & 0xf800 ) >> 11,
  339. ( $aP['FT'] & 0x07e0 ) >> 5,
  340. ( $aP['FT'] & 0x001f ) << 1,
  341. ( $aP['FD'] & 0x01e0 ) >> 5,
  342. $aP['FD'] & 0x001f,
  343. ( ( $aP['FD'] & 0xfe00 ) >> 9 ) + 1980 );
  344. //$this->Entries[] = &new SimpleUnzipEntry($aI);
  345. $this->package['entries'][] = [
  346. 'data' => $aI['D'],
  347. 'error' => $aI['E'],
  348. 'error_msg' => $aI['EM'],
  349. 'name' => $aI['N'],
  350. 'path' => $aI['P'],
  351. 'time' => $aI['T']
  352. ];
  353. } // end for each entries
  354. return true;
  355. }
  356. // sheets numeration: 1,2,3....
  357. public function error( $num = null, $str = null ) {
  358. if ( $num ) {
  359. $this->errno = $num;
  360. $this->error = $str;
  361. if ( $this->debug ) {
  362. trigger_error( __CLASS__ . ': ' . $this->error, E_USER_WARNING );
  363. }
  364. }
  365. return $this->error;
  366. }
  367. public function errno() {
  368. return $this->errno;
  369. }
  370. protected function _parse() {
  371. // Document data holders
  372. $this->sharedstrings = [];
  373. $this->sheets = [];
  374. // $this->styles = array();
  375. // Read relations and search for officeDocument
  376. if ( $relations = $this->getEntryXML( '_rels/.rels' ) ) {
  377. foreach ( $relations->Relationship as $rel ) {
  378. $rel_type = basename( trim( (string) $rel['Type'] ) ); // officeDocument
  379. $rel_target = $this->_getTarget( '', (string) $rel['Target'] ); // /xl/workbook.xml or xl/workbook.xml
  380. if ( $rel_type === 'officeDocument' && $workbook = $this->getEntryXML( $rel_target ) ) {
  381. $index_rId = []; // [0 => rId1]
  382. $index = 0;
  383. foreach ( $workbook->sheets->sheet as $s ) {
  384. $this->sheetNames[ $index ] = (string) $s['name'];
  385. $index_rId[ $index ] = (string) $s['id'];
  386. $index ++;
  387. }
  388. if ( (int) $workbook->workbookPr['date1904'] === 1 ) {
  389. $this->date1904 = 1;
  390. }
  391. if ( $workbookRelations = $this->getEntryXML( dirname( $rel_target ) . '/_rels/workbook.xml.rels' ) ) {
  392. // Loop relations for workbook and extract sheets...
  393. foreach ( $workbookRelations->Relationship as $workbookRelation ) {
  394. $wrel_type = basename( trim( (string) $workbookRelation['Type'] ) );
  395. $wrel_path = $this->_getTarget( dirname( $rel_target ), (string) $workbookRelation['Target'] );
  396. if ( ! $this->entryExists( $wrel_path ) ) {
  397. continue;
  398. }
  399. if ( $wrel_type === 'worksheet' ) { // Sheets
  400. if ( $sheet = $this->getEntryXML( $wrel_path ) ) {
  401. $index = array_search( (string) $workbookRelation['Id'], $index_rId, false );
  402. $this->sheets[ $index ] = $sheet;
  403. $this->sheetFiles[ $index ] = $wrel_path;
  404. }
  405. } elseif ( $wrel_type === 'sharedStrings' ) {
  406. if ( $sharedStrings = $this->getEntryXML( $wrel_path ) ) {
  407. foreach ( $sharedStrings->si as $val ) {
  408. if ( isset( $val->t ) ) {
  409. $this->sharedstrings[] = (string) $val->t;
  410. } elseif ( isset( $val->r ) ) {
  411. $this->sharedstrings[] = $this->_parseRichText( $val );
  412. }
  413. }
  414. }
  415. } elseif ( $wrel_type === 'styles' ) {
  416. $this->styles = $this->getEntryXML( $wrel_path );
  417. $nf = [];
  418. if ( $this->styles->numFmts->numFmt !== null ) {
  419. foreach ( $this->styles->numFmts->numFmt as $v ) {
  420. $nf[ (int) $v['numFmtId'] ] = (string) $v['formatCode'];
  421. }
  422. }
  423. if ( $this->styles->cellXfs->xf !== null ) {
  424. foreach ( $this->styles->cellXfs->xf as $v ) {
  425. $v = (array) $v->attributes();
  426. $v['format'] = '';
  427. if ( isset( $v['@attributes']['numFmtId'] ) ) {
  428. $v = $v['@attributes'];
  429. $fid = (int) $v['numFmtId'];
  430. // formats priority
  431. if ( isset( $nf[ $fid ] ) ) {
  432. $v['format'] = $nf[ $fid ];
  433. } elseif ( isset( self::$CF[ $fid ] ) ) {
  434. $v['format'] = self::$CF[ $fid ];
  435. }
  436. }
  437. $this->cellFormats[] = $v;
  438. }
  439. }
  440. }
  441. }
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. if ( count( $this->sheets ) ) {
  448. // Sort sheets
  449. ksort( $this->sheets );
  450. return true;
  451. }
  452. return false;
  453. }
  454. /*
  455. * @param string $name Filename in archive
  456. * @return SimpleXMLElement|bool
  457. */
  458. public function getEntryXML( $name ) {
  459. if ( $entry_xml = $this->getEntryData( $name ) ) {
  460. $entry_xml = trim( $entry_xml );
  461. // dirty remove namespace prefixes and empty rows
  462. $entry_xml = preg_replace( '/xmlns[^=]*="[^"]*"/i', '', $entry_xml ); // remove namespaces
  463. $entry_xml = preg_replace( '/[a-zA-Z0-9]+:([a-zA-Z0-9]+="[^"]+")/', '$1$2', $entry_xml ); // remove namespaced attrs
  464. $entry_xml = preg_replace( '/<[a-zA-Z0-9]+:([^>]+)>/', '<$1>', $entry_xml ); // fix namespaced openned tags
  465. $entry_xml = preg_replace( '/<\/[a-zA-Z0-9]+:([^>]+)>/', '</$1>', $entry_xml ); // fix namespaced closed tags
  466. // if ( $this->skipEmptyRows && strpos($name, '/sheet') ) {
  467. if ( strpos( $name, '/sheet' ) ) { // dirty skip empty rows
  468. $entry_xml = preg_replace( '/<row[^>]+>\s*(<c[^\/]+\/>\s*)+<\/row>/', '', $entry_xml, - 1, $cnt ); // remove empty rows
  469. $entry_xml = preg_replace( '/<row[^\/>]*\/>/', '', $entry_xml, - 1, $cnt2 );
  470. $entry_xml = preg_replace( '/<row[^>]*><\/row>/', '', $entry_xml, - 1, $cnt3 );
  471. if ( $cnt || $cnt2 || $cnt3 ) {
  472. $entry_xml = preg_replace( '/<dimension[^\/]+\/>/', '', $entry_xml );
  473. }
  474. // file_put_contents( basename( $name ), $entry_xml ); // @to do comment!!!
  475. }
  476. // XML External Entity (XXE) Prevention, libxml_disable_entity_loader deprecated in PHP 8
  477. if ( LIBXML_VERSION < 20900 ) {
  478. $_old = libxml_disable_entity_loader();
  479. }
  480. $entry_xmlobj = simplexml_load_string( $entry_xml );
  481. if ( LIBXML_VERSION < 20900 ) {
  482. /** @noinspection PhpUndefinedVariableInspection */
  483. libxml_disable_entity_loader( $_old );
  484. }
  485. if ( $entry_xmlobj ) {
  486. return $entry_xmlobj;
  487. }
  488. $e = libxml_get_last_error();
  489. if ( $e ) {
  490. $this->error( 3, 'XML-entry ' . $name . ' parser error ' . $e->message . ' line ' . $e->line );
  491. }
  492. } else {
  493. $this->error( 4, 'XML-entry not found ' . $name );
  494. }
  495. return false;
  496. }
  497. public function getEntryData( $name ) {
  498. $name = ltrim( str_replace( '\\', '/', $name ), '/' );
  499. $dir = $this->_strtoupper( dirname( $name ) );
  500. $name = $this->_strtoupper( basename( $name ) );
  501. foreach ( $this->package['entries'] as $entry ) {
  502. if ( $this->_strtoupper( $entry['path'] ) === $dir && $this->_strtoupper( $entry['name'] ) === $name ) {
  503. return $entry['data'];
  504. }
  505. }
  506. $this->error( 5, 'Entry not found ' . ( $dir ? $dir . '/' : '' ) . $name );
  507. return false;
  508. }
  509. public function entryExists( $name ) { // 0.6.6
  510. $dir = $this->_strtoupper( dirname( $name ) );
  511. $name = $this->_strtoupper( basename( $name ) );
  512. foreach ( $this->package['entries'] as $entry ) {
  513. if ( $this->_strtoupper( $entry['path'] ) === $dir && $this->_strtoupper( $entry['name'] ) === $name ) {
  514. return true;
  515. }
  516. }
  517. return false;
  518. }
  519. public function success() {
  520. return ! $this->error;
  521. }
  522. public function rows( $worksheetIndex = 0 ) {
  523. if ( ( $ws = $this->worksheet( $worksheetIndex ) ) === false ) {
  524. return false;
  525. }
  526. $dim = $this->dimension( $worksheetIndex );
  527. $numCols = $dim[0];
  528. $numRows = $dim[1];
  529. $emptyRow = [];
  530. for ( $i = 0; $i < $numCols; $i ++ ) {
  531. $emptyRow[] = '';
  532. }
  533. $rows = [];
  534. for ( $i = 0; $i < $numRows; $i ++ ) {
  535. $rows[] = $emptyRow;
  536. }
  537. $curR = 0;
  538. /* @var SimpleXMLElement $ws */
  539. foreach ( $ws->sheetData->row as $row ) {
  540. $curC = 0;
  541. foreach ( $row->c as $c ) {
  542. // detect skipped cols
  543. $idx = $this->getIndex( (string) $c['r'] );
  544. $x = $idx[0];
  545. $y = $idx[1];
  546. if ( $x > - 1 ) {
  547. $curC = $x;
  548. $curR = $y;
  549. }
  550. $rows[ $curR ][ $curC ] = $this->value( $c );
  551. $curC ++;
  552. }
  553. $curR ++;
  554. }
  555. return $rows;
  556. }
  557. // https://github.com/shuchkin/simplexlsx#gets-extend-cell-info-by--rowsex
  558. public function rowsEx( $worksheetIndex = 0 ) {
  559. if ( ( $ws = $this->worksheet( $worksheetIndex ) ) === false ) {
  560. return false;
  561. }
  562. $rows = [];
  563. $dim = $this->dimension( $worksheetIndex );
  564. $numCols = $dim[0];
  565. $numRows = $dim[1];
  566. $hiddenCols = [];
  567. /* @var SimpleXMLElement $ws */
  568. foreach( $ws->cols->col as $col ) {
  569. for ( $i = (int) $col['min']; $i <= (int) $col['max']; $i++ ) {
  570. if ( $col['hidden'] ) {
  571. $hiddenCols[] = $i - 1;
  572. }
  573. }
  574. }
  575. for ( $y = 0; $y < $numRows; $y ++ ) {
  576. for ( $x = 0; $x < $numCols; $x ++ ) {
  577. // 0.6.8
  578. $c = '';
  579. for ( $k = $x; $k >= 0; $k = (int) ( $k / 26 ) - 1 ) {
  580. $c = chr( $k % 26 + 65 ) . $c;
  581. }
  582. $rows[ $y ][ $x ] = [
  583. 'type' => '',
  584. 'name' => $c . ( $y + 1 ),
  585. 'value' => '',
  586. 'href' => '',
  587. 'f' => '',
  588. 'format' => '',
  589. 'r' => $y,
  590. 'hidden' => count($hiddenCols) && in_array($x, $hiddenCols, true )
  591. ];
  592. }
  593. }
  594. $curR = 0;
  595. foreach ( $ws->sheetData->row as $row ) {
  596. $curC = 0;
  597. $r_idx = (int) $row['r'];
  598. $r_hidden = (bool) $row['hidden'];
  599. foreach ( $row->c as $c ) {
  600. $r = (string) $c['r'];
  601. $t = (string) $c['t'];
  602. $s = (int) $c['s'];
  603. $idx = $this->getIndex( $r );
  604. $x = $idx[0];
  605. $y = $idx[1];
  606. if ( $x > - 1 ) {
  607. $curC = $x;
  608. $curR = $y;
  609. }
  610. if ( $s > 0 && isset( $this->cellFormats[ $s ] ) ) {
  611. $format = $this->cellFormats[ $s ]['format'];
  612. } else {
  613. $format = '';
  614. }
  615. $hidden = $r_hidden;
  616. if ( !$hidden && count($hiddenCols) && in_array($curC, $hiddenCols, true )) {
  617. $hidden = true;
  618. }
  619. $rows[ $curR ][ $curC ] = [
  620. 'type' => $t,
  621. 'name' => (string) $c['r'],
  622. 'value' => $this->value( $c ),
  623. 'href' => $this->href( $worksheetIndex, $c ),
  624. 'f' => (string) $c->f,
  625. 'format' => $format,
  626. 'r' => $r_idx,
  627. 'hidden' => $hidden,
  628. ];
  629. $curC ++;
  630. }
  631. $curR ++;
  632. }
  633. return $rows;
  634. }
  635. public function toHTML( $worksheetIndex = 0 ) {
  636. $s = '<table class=excel>';
  637. foreach ( $this->rows( $worksheetIndex ) as $r ) {
  638. $s .= '<tr>';
  639. foreach ( $r as $c ) {
  640. $s .= '<td nowrap>' . ( $c === '' ? '&nbsp' : htmlspecialchars( $c, ENT_QUOTES ) ) . '</td>';
  641. }
  642. $s .= "</tr>\r\n";
  643. }
  644. $s .= '</table>';
  645. return $s;
  646. }
  647. public function worksheet( $worksheetIndex = 0 ) {
  648. if ( isset( $this->sheets[ $worksheetIndex ] ) ) {
  649. $ws = $this->sheets[ $worksheetIndex ];
  650. if ( !isset($this->hyperlinks[ $worksheetIndex ]) && isset( $ws->hyperlinks ) ) {
  651. $this->hyperlinks[ $worksheetIndex ] = [];
  652. $sheet_rels = str_replace('worksheets','worksheets/_rels', $this->sheetFiles[$worksheetIndex]).'.rels';
  653. $link_ids = [];
  654. if ( $rels = $this->getEntryXML( $sheet_rels ) ) {
  655. // hyperlink
  656. // $rel_base = dirname( $sheet_rels );
  657. foreach ( $rels->Relationship as $rel ) {
  658. $rel_type = basename( trim( (string)$rel['Type'] ) );
  659. if ( $rel_type === 'hyperlink' ) {
  660. $rel_id = (string)$rel['Id'];
  661. $rel_target = (string)$rel['Target'];
  662. $link_ids[ $rel_id ] = $rel_target;
  663. }
  664. }
  665. }
  666. foreach ( $ws->hyperlinks->hyperlink as $hyperlink ) {
  667. $ref = (string) $hyperlink['ref'];
  668. if ( $this->_strpos($ref,':') > 0 ) { // A1:A8 -> A1
  669. $ref = explode(':', $ref);
  670. $ref = $ref[0];
  671. }
  672. // $this->hyperlinks[ $worksheetIndex ][ $ref ] = (string) $hyperlink['display'];
  673. $loc = (string) $hyperlink['location'];
  674. $id = (string) $hyperlink['id'];
  675. if ( $id ) {
  676. $href = $link_ids[ $id ] . ( $loc ? '#' . $loc : '');
  677. } else {
  678. $href = $loc;
  679. }
  680. $this->hyperlinks[ $worksheetIndex ][ $ref ] = $href;
  681. }
  682. }
  683. return $ws;
  684. }
  685. $this->error( 6, 'Worksheet not found ' . $worksheetIndex );
  686. return false;
  687. }
  688. /**
  689. * returns [numCols,numRows] of worksheet
  690. *
  691. * @param int $worksheetIndex
  692. *
  693. * @return array
  694. */
  695. public function dimension( $worksheetIndex = 0 ) {
  696. if ( ( $ws = $this->worksheet( $worksheetIndex ) ) === false ) {
  697. return [ 0, 0 ];
  698. }
  699. /* @var SimpleXMLElement $ws */
  700. $ref = (string) $ws->dimension['ref'];
  701. if ( $this->_strpos( $ref, ':' ) !== false ) {
  702. $d = explode( ':', $ref );
  703. $idx = $this->getIndex( $d[1] );
  704. return [ $idx[0] + 1, $idx[1] + 1 ];
  705. }
  706. /*
  707. if ( $ref !== '' ) { // 0.6.8
  708. $index = $this->getIndex( $ref );
  709. return [ $index[0] + 1, $index[1] + 1 ];
  710. }
  711. */
  712. // slow method
  713. $maxC = $maxR = 0;
  714. foreach ( $ws->sheetData->row as $row ) {
  715. foreach ( $row->c as $c ) {
  716. $idx = $this->getIndex( (string) $c['r'] );
  717. $x = $idx[0];
  718. $y = $idx[1];
  719. if ( $x > 0 ) {
  720. if ( $x > $maxC ) {
  721. $maxC = $x;
  722. }
  723. if ( $y > $maxR ) {
  724. $maxR = $y;
  725. }
  726. }
  727. }
  728. }
  729. return [ $maxC + 1, $maxR + 1 ];
  730. }
  731. public function getIndex( $cell = 'A1' ) {
  732. if ( preg_match( '/([A-Z]+)(\d+)/', $cell, $m ) ) {
  733. $col = $m[1];
  734. $row = $m[2];
  735. $colLen = $this->_strlen( $col );
  736. $index = 0;
  737. for ( $i = $colLen - 1; $i >= 0; $i -- ) {
  738. /** @noinspection PowerOperatorCanBeUsedInspection */
  739. $index += ( ord( $col[ $i ] ) - 64 ) * pow( 26, $colLen - $i - 1 );
  740. }
  741. return [ $index - 1, $row - 1 ];
  742. }
  743. // $this->error( 'Invalid cell index ' . $cell );
  744. return [ - 1, - 1 ];
  745. }
  746. public function value( $cell ) {
  747. // Determine data type
  748. $dataType = (string) $cell['t'];
  749. if ( $dataType === '' || $dataType === 'n' ) { // number
  750. $s = (int) $cell['s'];
  751. if ( $s > 0 && isset( $this->cellFormats[ $s ] ) ) {
  752. if (array_key_exists('format', $this->cellFormats[ $s ])) {
  753. $format = $this->cellFormats[ $s ]['format'];
  754. if ( preg_match( '/[mM]/', $format ) ) { // [m]onth
  755. $dataType = 'd';
  756. }
  757. }
  758. else {
  759. $dataType = 's';
  760. }
  761. }
  762. }
  763. $value = '';
  764. switch ( $dataType ) {
  765. case 's':
  766. // Value is a shared string
  767. if ( (string) $cell->v !== '' ) {
  768. $value = $this->sharedstrings[ (int) $cell->v ];
  769. }
  770. break;
  771. case 'b':
  772. // Value is boolean
  773. $value = (string) $cell->v;
  774. if ( $value === '0' ) {
  775. $value = false;
  776. } elseif ( $value === '1' ) {
  777. $value = true;
  778. } else {
  779. $value = (bool) $cell->v;
  780. }
  781. break;
  782. case 'inlineStr':
  783. // Value is rich text inline
  784. $value = $this->_parseRichText( $cell->is );
  785. break;
  786. case 'e':
  787. // Value is an error message
  788. if ( (string) $cell->v !== '' ) {
  789. $value = (string) $cell->v;
  790. }
  791. break;
  792. case 'd':
  793. // Value is a date and non-empty
  794. if ( ! empty( $cell->v ) ) {
  795. $value = $this->datetimeFormat ? gmdate( $this->datetimeFormat, $this->unixstamp( (float) $cell->v ) ) : (float) $cell->v;
  796. }
  797. break;
  798. default:
  799. // Value is a string
  800. $value = (string) $cell->v;
  801. // Check for numeric values
  802. if ( is_numeric( $value ) && $dataType !== 's' ) {
  803. /** @noinspection TypeUnsafeComparisonInspection */
  804. if ( $value == (int) $value ) {
  805. $value = (int) $value;
  806. } /** @noinspection TypeUnsafeComparisonInspection */ elseif ( $value == (float) $value ) {
  807. $value = (float) $value;
  808. }
  809. }
  810. }
  811. return $value;
  812. }
  813. public function unixstamp( $excelDateTime ) {
  814. $d = floor( $excelDateTime ); // days since 1900 or 1904
  815. $t = $excelDateTime - $d;
  816. if ( $this->date1904 ) {
  817. $d += 1462;
  818. }
  819. $t = ( abs( $d ) > 0 ) ? ( $d - 25569 ) * 86400 + round( $t * 86400 ) : round( $t * 86400 );
  820. return (int) $t;
  821. }
  822. /**
  823. * Returns cell value
  824. * VERY SLOW! Use ->rows() or ->rowsEx()
  825. *
  826. * @param int $worksheetIndex
  827. * @param string|array $cell ref or coords, D12 or [3,12]
  828. *
  829. * @return mixed Returns NULL if not found
  830. */
  831. public function getCell( $worksheetIndex = 0, $cell = 'A1' ) {
  832. if ( ( $ws = $this->worksheet( $worksheetIndex ) ) === false ) {
  833. return false;
  834. }
  835. if ( is_array( $cell )) {
  836. $cell = $this->_num2name($cell[0]).$cell[1];// [3,21] -> D21
  837. }
  838. if ( is_string( $cell ) ) {
  839. $result = $ws->sheetData->xpath( "row/c[@r='" . $cell . "']" );
  840. if ( count($result) ) {
  841. return $this->value( $result[0] );
  842. }
  843. }
  844. return null;
  845. }
  846. public function href( $worksheetIndex, $cell ) {
  847. $ref = (string) $cell['r'];
  848. return isset( $this->hyperlinks[ $worksheetIndex ][ $ref ] ) ? $this->hyperlinks[ $worksheetIndex ][ $ref ] : '';
  849. }
  850. public function sheets() {
  851. return $this->sheets;
  852. }
  853. public function sheetsCount() {
  854. return count( $this->sheets );
  855. }
  856. public function sheetName( $worksheetIndex ) {
  857. if ( isset( $this->sheetNames[ $worksheetIndex ] ) ) {
  858. return $this->sheetNames[ $worksheetIndex ];
  859. }
  860. return false;
  861. }
  862. public function sheetNames() {
  863. return $this->sheetNames;
  864. }
  865. // thx Gonzo
  866. public function getStyles() {
  867. return $this->styles;
  868. }
  869. public function getPackage() {
  870. return $this->package;
  871. }
  872. public function setDateTimeFormat( $value ) {
  873. $this->datetimeFormat = is_string( $value ) ? $value : false;
  874. }
  875. protected function _parseRichText( $is = null ) {
  876. $value = [];
  877. if ( isset( $is->t ) ) {
  878. $value[] = (string) $is->t;
  879. } elseif ( isset( $is->r ) ) {
  880. foreach ( $is->r as $run ) {
  881. $value[] = (string) $run->t;
  882. }
  883. }
  884. return implode( '', $value );
  885. }
  886. protected function _strlen( $str ) {
  887. return ( ini_get( 'mbstring.func_overload' ) & 2 ) ? mb_strlen( $str, '8bit' ) : strlen( $str );
  888. }
  889. protected function _strpos( $haystack, $needle, $offset = 0 ) {
  890. return ( ini_get( 'mbstring.func_overload' ) & 2 ) ? mb_strpos( $haystack, $needle, $offset, '8bit' ) : strpos( $haystack, $needle, $offset );
  891. }
  892. /*
  893. private function _strrpos( $haystack, $needle, $offset = 0 ) {
  894. return (ini_get('mbstring.func_overload') & 2) ? mb_strrpos( $haystack, $needle, $offset, '8bit') : strrpos($haystack, $needle, $offset);
  895. }*/
  896. protected function _strtoupper( $str ) {
  897. return ( ini_get( 'mbstring.func_overload' ) & 2 ) ? mb_strtoupper( $str, '8bit' ) : strtoupper( $str );
  898. }
  899. protected function _substr( $str, $start, $length = null ) {
  900. 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 );
  901. }
  902. protected function _getTarget( $base, $target ) {
  903. $target = trim( $target );
  904. if ( strpos( $target, '/' ) === 0 ) {
  905. return $this->_substr( $target, 1 );
  906. }
  907. $target = ( $base ? $base . '/' : '' ) . $target;
  908. // a/b/../c -> a/c
  909. $parts = explode( '/', $target );
  910. $abs = [];
  911. foreach ( $parts as $p ) {
  912. if ( '.' === $p ) {
  913. continue;
  914. }
  915. if ( '..' === $p ) {
  916. array_pop( $abs );
  917. } else {
  918. $abs[] = $p;
  919. }
  920. }
  921. return implode( '/', $abs );
  922. }
  923. protected function _num2name($num) {
  924. $numeric = ($num - 1) % 26;
  925. $letter = chr( 65 + $numeric );
  926. $num2 = (int) ( ($num-1) / 26 );
  927. if ( $num2 > 0 ) {
  928. return $this->_num2name( $num2 ) . $letter;
  929. }
  930. return $letter;
  931. }
  932. }