Source for file SYLK.php
Documentation is available at SYLK.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
private $_inputEncoding = 'ANSI';
private $_sheetIndex = 0;
private $_formats = array();
* PHPExcel_Reader_IReadFilter instance
* @var PHPExcel_Reader_IReadFilter
private $_readFilter = null;
* Create a new PHPExcel_Reader_SYLK
* Can the current PHPExcel_Reader_IReader read the file?
* @param string $pFileName
public function canRead($pFilename)
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
// Read sample data (first 2 KB will do)
$fh = fopen($pFilename, 'r');
$data = fread($fh, 2048);
// Count delimiters in file
if ($delimiterCount < 1) {
// Analyze first line looking for ID; signature
if (substr($lines[0],0,4) != 'ID;P') {
* Loads PHPExcel from file
* @param string $pFilename
public function load($pFilename)
// Load into this instance
* @return PHPExcel_Reader_IReadFilter
return $this->_readFilter;
* @param PHPExcel_Reader_IReadFilter $pValue
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
* @param string $pValue Input encoding
$this->_inputEncoding = $pValue;
return $this->_inputEncoding;
* Loads PHPExcel from file into PHPExcel instance
* @param string $pFilename
* @param PHPExcel $objPHPExcel
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
$fromFormats = array('\-', '\ ');
$toFormats = array('-', ' ');
$fileHandle = fopen($pFilename, 'r');
if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for reading.");
// loop through one row (line) at a time in the file
while (($rowData = fgets($fileHandle)) !== FALSE) {
// convert SYLK encoded $rowData to UTF-8
// explode each row at semicolons while taking into account that literal semicolon (;)
// is escaped like this (;;)
foreach($rowData as $rowDatum) {
case 'P' : $formatArray['numberformat']['code'] = str_replace($fromFormats,$toFormats,substr($rowDatum,1));
case 'F' : $formatArray['font']['name'] = substr($rowDatum,1);
case 'L' : $formatArray['font']['size'] = substr($rowDatum,1);
case 'S' : $styleSettings = substr($rowDatum,1);
for ($i= 0;$i< strlen($styleSettings);++ $i) {
switch ($styleSettings{$i}) {
case 'I' : $formatArray['font']['italic'] = true;
case 'D' : $formatArray['font']['bold'] = true;
$this->_formats['P'. $this->_format++ ] = $formatArray;
} elseif ($dataType == 'C') {
$hasCalculatedValue = false;
$cellData = $cellDataFormula = '';
foreach($rowData as $rowDatum) {
case 'X' : $column = substr($rowDatum,1);
case 'Y' : $row = substr($rowDatum,1);
case 'K' : $cellData = substr($rowDatum,1);
case 'E' : $cellDataFormula = '='. substr($rowDatum,1);
// Convert R1C1 style references to A1 style references (but only when not quoted)
$temp = explode('"',$cellDataFormula);
foreach($temp as &$value) {
// Only count/replace in alternate array entries
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+ PREG_OFFSET_CAPTURE);
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
// through the formula from left to right. Reversing means that we work right to left.through
// Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
// then modify the formula to use that new reference
foreach($cellReferences as $cellReference) {
$rowReference = $cellReference[2][0];
// Empty R reference is the current row
if ($rowReference == '') $rowReference = $row;
// Bracketed R references are relative to the current row
if ($rowReference{0} == '[') $rowReference = $row + trim($rowReference,'[]');
$columnReference = $cellReference[4][0];
// Empty C reference is the current column
if ($columnReference == '') $columnReference = $column;
// Bracketed C references are relative to the current column
if ($columnReference{0} == '[') $columnReference = $column + trim($columnReference,'[]');
// Then rebuild the formula string
$cellDataFormula = implode('"',$temp);
$hasCalculatedValue = true;
$objPHPExcel->getActiveSheet()->getCell($columnLetter. $row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
if ($hasCalculatedValue) {
$objPHPExcel->getActiveSheet()->getCell($columnLetter. $row)->setCalculatedValue($cellData);
} elseif ($dataType == 'F') {
$formatStyle = $columnWidth = $styleSettings = '';
foreach($rowData as $rowDatum) {
case 'X' : $column = substr($rowDatum,1);
case 'Y' : $row = substr($rowDatum,1);
case 'P' : $formatStyle = $rowDatum;
case 'W' : list ($startCol,$endCol,$columnWidth) = explode(' ',substr($rowDatum,1));
case 'S' : $styleSettings = substr($rowDatum,1);
for ($i= 0;$i< strlen($styleSettings);++ $i) {
switch ($styleSettings{$i}) {
case 'I' : $styleData['font']['italic'] = true;
case 'D' : $styleData['font']['bold'] = true;
if (($formatStyle > '') && ($column > '') && ($row > '')) {
$objPHPExcel->getActiveSheet()->getStyle($columnLetter. $row)->applyFromArray($this->_formats[$formatStyle]);
if ((count($styleData) > 0) && ($column > '') && ($row > '')) {
$objPHPExcel->getActiveSheet()->getStyle($columnLetter. $row)->applyFromArray($styleData);
if ($startCol == $endCol) {
$objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
$objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
$objPHPExcel->getActiveSheet()->getColumnDimension(++ $startCol)->setWidth($columnWidth);
} while ($startCol != $endCol);
foreach($rowData as $rowDatum) {
case 'X' : $column = substr($rowDatum,1);
case 'Y' : $row = substr($rowDatum,1);
return $this->_sheetIndex;
* @param int $pValue Sheet index
* @return PHPExcel_Reader_SYLK
$this->_sheetIndex = $pValue;
|