Slk.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Reader;
  3. use InvalidArgumentException;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  5. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  6. use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
  7. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  8. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  9. use PhpOffice\PhpSpreadsheet\Style\Border;
  10. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  11. class Slk extends BaseReader
  12. {
  13. /**
  14. * Input encoding.
  15. *
  16. * @var string
  17. */
  18. private $inputEncoding = 'ANSI';
  19. /**
  20. * Sheet index to read.
  21. *
  22. * @var int
  23. */
  24. private $sheetIndex = 0;
  25. /**
  26. * Formats.
  27. *
  28. * @var array
  29. */
  30. private $formats = [];
  31. /**
  32. * Format Count.
  33. *
  34. * @var int
  35. */
  36. private $format = 0;
  37. /**
  38. * Fonts.
  39. *
  40. * @var array
  41. */
  42. private $fonts = [];
  43. /**
  44. * Font Count.
  45. *
  46. * @var int
  47. */
  48. private $fontcount = 0;
  49. /**
  50. * Create a new SYLK Reader instance.
  51. */
  52. public function __construct()
  53. {
  54. parent::__construct();
  55. }
  56. /**
  57. * Validate that the current file is a SYLK file.
  58. *
  59. * @param string $pFilename
  60. *
  61. * @return bool
  62. */
  63. public function canRead($pFilename)
  64. {
  65. try {
  66. $this->openFile($pFilename);
  67. } catch (InvalidArgumentException $e) {
  68. return false;
  69. }
  70. // Read sample data (first 2 KB will do)
  71. $data = fread($this->fileHandle, 2048);
  72. // Count delimiters in file
  73. $delimiterCount = substr_count($data, ';');
  74. $hasDelimiter = $delimiterCount > 0;
  75. // Analyze first line looking for ID; signature
  76. $lines = explode("\n", $data);
  77. $hasId = substr($lines[0], 0, 4) === 'ID;P';
  78. fclose($this->fileHandle);
  79. return $hasDelimiter && $hasId;
  80. }
  81. private function canReadOrBust(string $pFilename): void
  82. {
  83. if (!$this->canRead($pFilename)) {
  84. throw new ReaderException($pFilename . ' is an Invalid SYLK file.');
  85. }
  86. $this->openFile($pFilename);
  87. }
  88. /**
  89. * Set input encoding.
  90. *
  91. * @deprecated no use is made of this property
  92. *
  93. * @param string $pValue Input encoding, eg: 'ANSI'
  94. *
  95. * @return $this
  96. *
  97. * @codeCoverageIgnore
  98. */
  99. public function setInputEncoding($pValue)
  100. {
  101. $this->inputEncoding = $pValue;
  102. return $this;
  103. }
  104. /**
  105. * Get input encoding.
  106. *
  107. * @deprecated no use is made of this property
  108. *
  109. * @return string
  110. *
  111. * @codeCoverageIgnore
  112. */
  113. public function getInputEncoding()
  114. {
  115. return $this->inputEncoding;
  116. }
  117. /**
  118. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
  119. *
  120. * @param string $pFilename
  121. *
  122. * @return array
  123. */
  124. public function listWorksheetInfo($pFilename)
  125. {
  126. // Open file
  127. $this->canReadOrBust($pFilename);
  128. $fileHandle = $this->fileHandle;
  129. rewind($fileHandle);
  130. $worksheetInfo = [];
  131. $worksheetInfo[0]['worksheetName'] = basename($pFilename, '.slk');
  132. // loop through one row (line) at a time in the file
  133. $rowIndex = 0;
  134. $columnIndex = 0;
  135. while (($rowData = fgets($fileHandle)) !== false) {
  136. $columnIndex = 0;
  137. // convert SYLK encoded $rowData to UTF-8
  138. $rowData = StringHelper::SYLKtoUTF8($rowData);
  139. // explode each row at semicolons while taking into account that literal semicolon (;)
  140. // is escaped like this (;;)
  141. $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData)))));
  142. $dataType = array_shift($rowData);
  143. if ($dataType == 'B') {
  144. foreach ($rowData as $rowDatum) {
  145. switch ($rowDatum[0]) {
  146. case 'X':
  147. $columnIndex = (int) substr($rowDatum, 1) - 1;
  148. break;
  149. case 'Y':
  150. $rowIndex = substr($rowDatum, 1);
  151. break;
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. $worksheetInfo[0]['lastColumnIndex'] = $columnIndex;
  158. $worksheetInfo[0]['totalRows'] = $rowIndex;
  159. $worksheetInfo[0]['lastColumnLetter'] = Coordinate::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex'] + 1);
  160. $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
  161. // Close file
  162. fclose($fileHandle);
  163. return $worksheetInfo;
  164. }
  165. /**
  166. * Loads PhpSpreadsheet from file.
  167. *
  168. * @param string $pFilename
  169. *
  170. * @return Spreadsheet
  171. */
  172. public function load($pFilename)
  173. {
  174. // Create new Spreadsheet
  175. $spreadsheet = new Spreadsheet();
  176. // Load into this instance
  177. return $this->loadIntoExisting($pFilename, $spreadsheet);
  178. }
  179. private $colorArray = [
  180. 'FF00FFFF', // 0 - cyan
  181. 'FF000000', // 1 - black
  182. 'FFFFFFFF', // 2 - white
  183. 'FFFF0000', // 3 - red
  184. 'FF00FF00', // 4 - green
  185. 'FF0000FF', // 5 - blue
  186. 'FFFFFF00', // 6 - yellow
  187. 'FFFF00FF', // 7 - magenta
  188. ];
  189. private $fontStyleMappings = [
  190. 'B' => 'bold',
  191. 'I' => 'italic',
  192. 'U' => 'underline',
  193. ];
  194. private function processFormula(string $rowDatum, bool &$hasCalculatedValue, string &$cellDataFormula, string $row, string $column): void
  195. {
  196. $cellDataFormula = '=' . substr($rowDatum, 1);
  197. // Convert R1C1 style references to A1 style references (but only when not quoted)
  198. $temp = explode('"', $cellDataFormula);
  199. $key = false;
  200. foreach ($temp as &$value) {
  201. // Only count/replace in alternate array entries
  202. if ($key = !$key) {
  203. preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
  204. // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  205. // through the formula from left to right. Reversing means that we work right to left.through
  206. // the formula
  207. $cellReferences = array_reverse($cellReferences);
  208. // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  209. // then modify the formula to use that new reference
  210. foreach ($cellReferences as $cellReference) {
  211. $rowReference = $cellReference[2][0];
  212. // Empty R reference is the current row
  213. if ($rowReference == '') {
  214. $rowReference = $row;
  215. }
  216. // Bracketed R references are relative to the current row
  217. if ($rowReference[0] == '[') {
  218. $rowReference = (int) $row + (int) trim($rowReference, '[]');
  219. }
  220. $columnReference = $cellReference[4][0];
  221. // Empty C reference is the current column
  222. if ($columnReference == '') {
  223. $columnReference = $column;
  224. }
  225. // Bracketed C references are relative to the current column
  226. if ($columnReference[0] == '[') {
  227. $columnReference = (int) $column + (int) trim($columnReference, '[]');
  228. }
  229. $A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference;
  230. $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
  231. }
  232. }
  233. }
  234. unset($value);
  235. // Then rebuild the formula string
  236. $cellDataFormula = implode('"', $temp);
  237. $hasCalculatedValue = true;
  238. }
  239. private function processCRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column): void
  240. {
  241. // Read cell value data
  242. $hasCalculatedValue = false;
  243. $cellDataFormula = $cellData = '';
  244. foreach ($rowData as $rowDatum) {
  245. switch ($rowDatum[0]) {
  246. case 'C':
  247. case 'X':
  248. $column = substr($rowDatum, 1);
  249. break;
  250. case 'R':
  251. case 'Y':
  252. $row = substr($rowDatum, 1);
  253. break;
  254. case 'K':
  255. $cellData = substr($rowDatum, 1);
  256. break;
  257. case 'E':
  258. $this->processFormula($rowDatum, $hasCalculatedValue, $cellDataFormula, $row, $column);
  259. break;
  260. }
  261. }
  262. $columnLetter = Coordinate::stringFromColumnIndex((int) $column);
  263. $cellData = Calculation::unwrapResult($cellData);
  264. // Set cell value
  265. $this->processCFinal($spreadsheet, $hasCalculatedValue, $cellDataFormula, $cellData, "$columnLetter$row");
  266. }
  267. private function processCFinal(Spreadsheet &$spreadsheet, bool $hasCalculatedValue, string $cellDataFormula, string $cellData, string $coordinate): void
  268. {
  269. // Set cell value
  270. $spreadsheet->getActiveSheet()->getCell($coordinate)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
  271. if ($hasCalculatedValue) {
  272. $cellData = Calculation::unwrapResult($cellData);
  273. $spreadsheet->getActiveSheet()->getCell($coordinate)->setCalculatedValue($cellData);
  274. }
  275. }
  276. private function processFRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column): void
  277. {
  278. // Read cell formatting
  279. $formatStyle = $columnWidth = '';
  280. $startCol = $endCol = '';
  281. $fontStyle = '';
  282. $styleData = [];
  283. foreach ($rowData as $rowDatum) {
  284. switch ($rowDatum[0]) {
  285. case 'C':
  286. case 'X':
  287. $column = substr($rowDatum, 1);
  288. break;
  289. case 'R':
  290. case 'Y':
  291. $row = substr($rowDatum, 1);
  292. break;
  293. case 'P':
  294. $formatStyle = $rowDatum;
  295. break;
  296. case 'W':
  297. [$startCol, $endCol, $columnWidth] = explode(' ', substr($rowDatum, 1));
  298. break;
  299. case 'S':
  300. $this->styleSettings($rowDatum, $styleData, $fontStyle);
  301. break;
  302. }
  303. }
  304. $this->addFormats($spreadsheet, $formatStyle, $row, $column);
  305. $this->addFonts($spreadsheet, $fontStyle, $row, $column);
  306. $this->addStyle($spreadsheet, $styleData, $row, $column);
  307. $this->addWidth($spreadsheet, $columnWidth, $startCol, $endCol);
  308. }
  309. private $styleSettingsFont = ['D' => 'bold', 'I' => 'italic'];
  310. private $styleSettingsBorder = [
  311. 'B' => 'bottom',
  312. 'L' => 'left',
  313. 'R' => 'right',
  314. 'T' => 'top',
  315. ];
  316. private function styleSettings(string $rowDatum, array &$styleData, string &$fontStyle): void
  317. {
  318. $styleSettings = substr($rowDatum, 1);
  319. $iMax = strlen($styleSettings);
  320. for ($i = 0; $i < $iMax; ++$i) {
  321. $char = $styleSettings[$i];
  322. if (array_key_exists($char, $this->styleSettingsFont)) {
  323. $styleData['font'][$this->styleSettingsFont[$char]] = true;
  324. } elseif (array_key_exists($char, $this->styleSettingsBorder)) {
  325. $styleData['borders'][$this->styleSettingsBorder[$char]]['borderStyle'] = Border::BORDER_THIN;
  326. } elseif ($char == 'S') {
  327. $styleData['fill']['fillType'] = \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_GRAY125;
  328. } elseif ($char == 'M') {
  329. if (preg_match('/M([1-9]\\d*)/', $styleSettings, $matches)) {
  330. $fontStyle = $matches[1];
  331. }
  332. }
  333. }
  334. }
  335. private function addFormats(Spreadsheet &$spreadsheet, string $formatStyle, string $row, string $column): void
  336. {
  337. if ($formatStyle && $column > '' && $row > '') {
  338. $columnLetter = Coordinate::stringFromColumnIndex((int) $column);
  339. if (isset($this->formats[$formatStyle])) {
  340. $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->formats[$formatStyle]);
  341. }
  342. }
  343. }
  344. private function addFonts(Spreadsheet &$spreadsheet, string $fontStyle, string $row, string $column): void
  345. {
  346. if ($fontStyle && $column > '' && $row > '') {
  347. $columnLetter = Coordinate::stringFromColumnIndex((int) $column);
  348. if (isset($this->fonts[$fontStyle])) {
  349. $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->fonts[$fontStyle]);
  350. }
  351. }
  352. }
  353. private function addStyle(Spreadsheet &$spreadsheet, array $styleData, string $row, string $column): void
  354. {
  355. if ((!empty($styleData)) && $column > '' && $row > '') {
  356. $columnLetter = Coordinate::stringFromColumnIndex($column);
  357. $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($styleData);
  358. }
  359. }
  360. private function addWidth(Spreadsheet $spreadsheet, string $columnWidth, string $startCol, string $endCol): void
  361. {
  362. if ($columnWidth > '') {
  363. if ($startCol == $endCol) {
  364. $startCol = Coordinate::stringFromColumnIndex((int) $startCol);
  365. $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth((float) $columnWidth);
  366. } else {
  367. $startCol = Coordinate::stringFromColumnIndex($startCol);
  368. $endCol = Coordinate::stringFromColumnIndex($endCol);
  369. $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth((float) $columnWidth);
  370. do {
  371. $spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth((float) $columnWidth);
  372. } while ($startCol !== $endCol);
  373. }
  374. }
  375. }
  376. private function processPRecord(array $rowData, Spreadsheet &$spreadsheet): void
  377. {
  378. // Read shared styles
  379. $formatArray = [];
  380. $fromFormats = ['\-', '\ '];
  381. $toFormats = ['-', ' '];
  382. foreach ($rowData as $rowDatum) {
  383. switch ($rowDatum[0]) {
  384. case 'P':
  385. $formatArray['numberFormat']['formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1));
  386. break;
  387. case 'E':
  388. case 'F':
  389. $formatArray['font']['name'] = substr($rowDatum, 1);
  390. break;
  391. case 'M':
  392. $formatArray['font']['size'] = substr($rowDatum, 1) / 20;
  393. break;
  394. case 'L':
  395. $this->processPColors($rowDatum, $formatArray);
  396. break;
  397. case 'S':
  398. $this->processPFontStyles($rowDatum, $formatArray);
  399. break;
  400. }
  401. }
  402. $this->processPFinal($spreadsheet, $formatArray);
  403. }
  404. private function processPColors(string $rowDatum, array &$formatArray): void
  405. {
  406. if (preg_match('/L([1-9]\\d*)/', $rowDatum, $matches)) {
  407. $fontColor = $matches[1] % 8;
  408. $formatArray['font']['color']['argb'] = $this->colorArray[$fontColor];
  409. }
  410. }
  411. private function processPFontStyles(string $rowDatum, array &$formatArray): void
  412. {
  413. $styleSettings = substr($rowDatum, 1);
  414. $iMax = strlen($styleSettings);
  415. for ($i = 0; $i < $iMax; ++$i) {
  416. if (array_key_exists($styleSettings[$i], $this->fontStyleMappings)) {
  417. $formatArray['font'][$this->fontStyleMappings[$styleSettings[$i]]] = true;
  418. }
  419. }
  420. }
  421. private function processPFinal(Spreadsheet &$spreadsheet, array $formatArray): void
  422. {
  423. if (array_key_exists('numberFormat', $formatArray)) {
  424. $this->formats['P' . $this->format] = $formatArray;
  425. ++$this->format;
  426. } elseif (array_key_exists('font', $formatArray)) {
  427. ++$this->fontcount;
  428. $this->fonts[$this->fontcount] = $formatArray;
  429. if ($this->fontcount === 1) {
  430. $spreadsheet->getDefaultStyle()->applyFromArray($formatArray);
  431. }
  432. }
  433. }
  434. /**
  435. * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
  436. *
  437. * @param string $pFilename
  438. *
  439. * @return Spreadsheet
  440. */
  441. public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
  442. {
  443. // Open file
  444. $this->canReadOrBust($pFilename);
  445. $fileHandle = $this->fileHandle;
  446. rewind($fileHandle);
  447. // Create new Worksheets
  448. while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
  449. $spreadsheet->createSheet();
  450. }
  451. $spreadsheet->setActiveSheetIndex($this->sheetIndex);
  452. $spreadsheet->getActiveSheet()->setTitle(substr(basename($pFilename, '.slk'), 0, Worksheet::SHEET_TITLE_MAXIMUM_LENGTH));
  453. // Loop through file
  454. $column = $row = '';
  455. // loop through one row (line) at a time in the file
  456. while (($rowDataTxt = fgets($fileHandle)) !== false) {
  457. // convert SYLK encoded $rowData to UTF-8
  458. $rowDataTxt = StringHelper::SYLKtoUTF8($rowDataTxt);
  459. // explode each row at semicolons while taking into account that literal semicolon (;)
  460. // is escaped like this (;;)
  461. $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowDataTxt)))));
  462. $dataType = array_shift($rowData);
  463. if ($dataType == 'P') {
  464. // Read shared styles
  465. $this->processPRecord($rowData, $spreadsheet);
  466. } elseif ($dataType == 'C') {
  467. // Read cell value data
  468. $this->processCRecord($rowData, $spreadsheet, $row, $column);
  469. } elseif ($dataType == 'F') {
  470. // Read cell formatting
  471. $this->processFRecord($rowData, $spreadsheet, $row, $column);
  472. } else {
  473. $this->columnRowFromRowData($rowData, $column, $row);
  474. }
  475. }
  476. // Close file
  477. fclose($fileHandle);
  478. // Return
  479. return $spreadsheet;
  480. }
  481. private function columnRowFromRowData(array $rowData, string &$column, string &$row): void
  482. {
  483. foreach ($rowData as $rowDatum) {
  484. $char0 = $rowDatum[0];
  485. if ($char0 === 'X' || $char0 == 'C') {
  486. $column = substr($rowDatum, 1);
  487. } elseif ($char0 === 'Y' || $char0 == 'R') {
  488. $row = substr($rowDatum, 1);
  489. }
  490. }
  491. }
  492. /**
  493. * Get sheet index.
  494. *
  495. * @return int
  496. */
  497. public function getSheetIndex()
  498. {
  499. return $this->sheetIndex;
  500. }
  501. /**
  502. * Set sheet index.
  503. *
  504. * @param int $pValue Sheet index
  505. *
  506. * @return $this
  507. */
  508. public function setSheetIndex($pValue)
  509. {
  510. $this->sheetIndex = $pValue;
  511. return $this;
  512. }
  513. }