63 parent::__construct();
82 $data = fread($this->fileHandle, 2048);
85 $delimiterCount = substr_count(
$data,
';');
86 $hasDelimiter = $delimiterCount > 0;
89 $lines = explode(
"\n",
$data);
90 $hasId = substr($lines[0], 0, 4) ===
'ID;P';
92 fclose($this->fileHandle);
94 return $hasDelimiter && $hasId;
99 if (!$this->
canRead($pFilename)) {
100 throw new ReaderException($pFilename .
' is an Invalid SYLK file.');
118 $this->inputEncoding = $pValue;
152 $worksheetInfo[0][
'worksheetName'] = basename($pFilename,
'.slk');
157 while (($rowData = fgets(
$fileHandle)) !==
false) {
165 $rowData = explode(
"\t", str_replace(
'¤',
';', str_replace(
';',
"\t", str_replace(
';;',
'¤', rtrim($rowData)))));
167 $dataType = array_shift($rowData);
168 if ($dataType ==
'B') {
169 foreach ($rowData as $rowDatum) {
170 switch ($rowDatum[0]) {
172 $columnIndex = (int) substr($rowDatum, 1) - 1;
176 $rowIndex = substr($rowDatum, 1);
186 $worksheetInfo[0][
'lastColumnIndex'] = $columnIndex;
187 $worksheetInfo[0][
'totalRows'] = $rowIndex;
189 $worksheetInfo[0][
'totalColumns'] = $worksheetInfo[0][
'lastColumnIndex'] + 1;
194 return $worksheetInfo;
204 public function load($pFilename)
230 private function processFormula(
string $rowDatum,
bool &$hasCalculatedValue,
string &$cellDataFormula,
string $row,
string $column): void
232 $cellDataFormula =
'=' . substr($rowDatum, 1);
234 $temp = explode(
'"', $cellDataFormula);
236 foreach ($temp as &$value) {
239 preg_match_all(
'/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
243 $cellReferences = array_reverse($cellReferences);
246 foreach ($cellReferences as $cellReference) {
247 $rowReference = $cellReference[2][0];
249 if ($rowReference ==
'') {
250 $rowReference =
$row;
253 if ($rowReference[0] ==
'[') {
254 $rowReference = (int) $row + (
int) trim($rowReference,
'[]');
256 $columnReference = $cellReference[4][0];
258 if ($columnReference ==
'') {
259 $columnReference = $column;
262 if ($columnReference[0] ==
'[') {
263 $columnReference = (int) $column + (
int) trim($columnReference,
'[]');
267 $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
273 $cellDataFormula = implode(
'"', $temp);
274 $hasCalculatedValue =
true;
280 $hasCalculatedValue =
false;
281 $cellDataFormula = $cellData =
'';
282 foreach ($rowData as $rowDatum) {
283 switch ($rowDatum[0]) {
286 $column = substr($rowDatum, 1);
291 $row = substr($rowDatum, 1);
295 $cellData = substr($rowDatum, 1);
299 $this->
processFormula($rowDatum, $hasCalculatedValue, $cellDataFormula, $row, $column);
308 $this->
processCFinal($spreadsheet, $hasCalculatedValue, $cellDataFormula, $cellData,
"$columnLetter$row");
311 private function processCFinal(
Spreadsheet &$spreadsheet,
bool $hasCalculatedValue,
string $cellDataFormula,
string $cellData,
string $coordinate): void
314 $spreadsheet->
getActiveSheet()->getCell($coordinate)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
315 if ($hasCalculatedValue) {
317 $spreadsheet->
getActiveSheet()->getCell($coordinate)->setCalculatedValue($cellData);
324 $formatStyle = $columnWidth =
'';
325 $startCol = $endCol =
'';
328 foreach ($rowData as $rowDatum) {
329 switch ($rowDatum[0]) {
332 $column = substr($rowDatum, 1);
337 $row = substr($rowDatum, 1);
341 $formatStyle = $rowDatum;
345 [$startCol, $endCol, $columnWidth] = explode(
' ', substr($rowDatum, 1));
354 $this->
addFormats($spreadsheet, $formatStyle, $row, $column);
355 $this->
addFonts($spreadsheet, $fontStyle, $row, $column);
356 $this->
addStyle($spreadsheet, $styleData, $row, $column);
357 $this->
addWidth($spreadsheet, $columnWidth, $startCol, $endCol);
369 private function styleSettings(
string $rowDatum, array &$styleData,
string &$fontStyle): void
371 $styleSettings = substr($rowDatum, 1);
372 $iMax = strlen($styleSettings);
373 for (
$i = 0;
$i < $iMax; ++
$i) {
374 $char = $styleSettings[
$i];
375 if (array_key_exists($char, $this->styleSettingsFont)) {
376 $styleData[
'font'][$this->styleSettingsFont[$char]] =
true;
377 } elseif (array_key_exists($char, $this->styleSettingsBorder)) {
378 $styleData[
'borders'][$this->styleSettingsBorder[$char]][
'borderStyle'] =
Border::BORDER_THIN;
379 } elseif ($char ==
'S') {
381 } elseif ($char ==
'M') {
382 if (preg_match(
'/M([1-9]\\d*)/', $styleSettings, $matches)) {
383 $fontStyle = $matches[1];
391 if ($formatStyle && $column >
'' && $row >
'') {
393 if (isset($this->formats[$formatStyle])) {
394 $spreadsheet->
getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->formats[$formatStyle]);
401 if ($fontStyle && $column >
'' && $row >
'') {
403 if (isset($this->fonts[$fontStyle])) {
404 $spreadsheet->
getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->fonts[$fontStyle]);
411 if ((!empty($styleData)) && $column >
'' && $row >
'') {
413 $spreadsheet->
getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($styleData);
417 private function addWidth(
Spreadsheet $spreadsheet,
string $columnWidth,
string $startCol,
string $endCol): void
419 if ($columnWidth >
'') {
420 if ($startCol == $endCol) {
422 $spreadsheet->
getActiveSheet()->getColumnDimension($startCol)->setWidth((
float) $columnWidth);
426 $spreadsheet->
getActiveSheet()->getColumnDimension($startCol)->setWidth((
float) $columnWidth);
428 $spreadsheet->
getActiveSheet()->getColumnDimension(++$startCol)->setWidth((
float) $columnWidth);
429 }
while ($startCol !== $endCol);
438 $fromFormats = [
'\-',
'\ '];
439 $toFormats = [
'-',
' '];
440 foreach ($rowData as $rowDatum) {
441 switch ($rowDatum[0]) {
443 $formatArray[
'numberFormat'][
'formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1));
448 $formatArray[
'font'][
'name'] = substr($rowDatum, 1);
452 $formatArray[
'font'][
'size'] = substr($rowDatum, 1) / 20;
470 if (preg_match(
'/L([1-9]\\d*)/', $rowDatum, $matches)) {
471 $fontColor = $matches[1] % 8;
472 $formatArray[
'font'][
'color'][
'argb'] = $this->colorArray[$fontColor];
478 $styleSettings = substr($rowDatum, 1);
479 $iMax = strlen($styleSettings);
480 for (
$i = 0;
$i < $iMax; ++
$i) {
481 if (array_key_exists($styleSettings[
$i], $this->fontStyleMappings)) {
482 $formatArray[
'font'][$this->fontStyleMappings[$styleSettings[
$i]]] =
true;
489 if (array_key_exists(
'numberFormat', $formatArray)) {
492 } elseif (array_key_exists(
'font', $formatArray)) {
495 if ($this->fontcount === 1) {
526 while (($rowDataTxt = fgets(
$fileHandle)) !==
false) {
532 $rowData = explode(
"\t", str_replace(
'¤',
';', str_replace(
';',
"\t", str_replace(
';;',
'¤', rtrim($rowDataTxt)))));
534 $dataType = array_shift($rowData);
535 if ($dataType ==
'P') {
538 } elseif ($dataType ==
'C') {
541 } elseif ($dataType ==
'F') {
558 foreach ($rowData as $rowDatum) {
559 $char0 = $rowDatum[0];
560 if ($char0 ===
'X' || $char0 ==
'C') {
561 $column = substr($rowDatum, 1);
562 } elseif ($char0 ===
'Y' || $char0 ==
'R') {
563 $row = substr($rowDatum, 1);
587 $this->sheetIndex = $pValue;
processCFinal(Spreadsheet &$spreadsheet, bool $hasCalculatedValue, string $cellDataFormula, string $cellData, string $coordinate)
processPFinal(Spreadsheet &$spreadsheet, array $formatArray)
addFonts(Spreadsheet &$spreadsheet, string $fontStyle, string $row, string $column)
getActiveSheet()
Get active sheet.
setInputEncoding($pValue)
Set input encoding.
const FILL_PATTERN_GRAY125
static SYLKtoUTF8($pValue)
Convert SYLK encoded string to UTF-8.
canReadOrBust(string $pFilename)
getDefaultStyle()
Get default style.
setSheetIndex($pValue)
Set sheet index.
processPColors(string $rowDatum, array &$formatArray)
listWorksheetInfo($pFilename)
Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)...
load($pFilename)
Loads PhpSpreadsheet from file.
loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
canRead($pFilename)
Validate that the current file is a SYLK file.
processPFontStyles(string $rowDatum, array &$formatArray)
addStyle(Spreadsheet &$spreadsheet, array $styleData, string $row, string $column)
createSheet($sheetIndex=null)
Create sheet and add it to this workbook.
processCRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column)
setActiveSheetIndex($pIndex)
Set active sheet index.
getInputEncoding()
Get input encoding.
columnRowFromRowData(array $rowData, string &$column, string &$row)
const SHEET_TITLE_MAXIMUM_LENGTH
getSheetIndex()
Get sheet index.
addWidth(Spreadsheet $spreadsheet, string $columnWidth, string $startCol, string $endCol)
static unwrapResult($value)
Remove quotes used as a wrapper to identify string values.
addFormats(Spreadsheet &$spreadsheet, string $formatStyle, string $row, string $column)
processFormula(string $rowDatum, bool &$hasCalculatedValue, string &$cellDataFormula, string $row, string $column)
styleSettings(string $rowDatum, array &$styleData, string &$fontStyle)
processFRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column)
__construct()
Create a new SYLK Reader instance.
processPRecord(array $rowData, Spreadsheet &$spreadsheet)
static stringFromColumnIndex($columnIndex)
String from column index.
openFile($pFilename)
Open file for reading.
getSheetCount()
Get sheet count.