PHPExcel_Reader
[ class tree: PHPExcel_Reader ] [ index: PHPExcel_Reader ] [ all elements ]

Source for file SYLK.php

Documentation is available at SYLK.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2010 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Reader
  23.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36.     // check mbstring.func_overload
  37.     if (ini_get('mbstring.func_overload'2{
  38.         throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
  39.     }
  40. }
  41.  
  42. /**
  43.  * PHPExcel_Reader_SYLK
  44.  *
  45.  * @category   PHPExcel
  46.  * @package    PHPExcel_Reader
  47.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  48.  */
  49. class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
  50. {
  51.     /**
  52.      * Input encoding
  53.      *
  54.      * @var string 
  55.      */
  56.     private $_inputEncoding;
  57.  
  58.     /**
  59.      * Delimiter
  60.      *
  61.      * @var string 
  62.      */
  63.     private $_delimiter;
  64.  
  65.     /**
  66.      * Enclosure
  67.      *
  68.      * @var string 
  69.      */
  70.     private $_enclosure;
  71.  
  72.     /**
  73.      * Line ending
  74.      *
  75.      * @var string 
  76.      */
  77.     private $_lineEnding;
  78.  
  79.     /**
  80.      * Sheet index to read
  81.      *
  82.      * @var int 
  83.      */
  84.     private $_sheetIndex;
  85.  
  86.     /**
  87.      * Formats
  88.      *
  89.      * @var array 
  90.      */
  91.     private $_formats array();
  92.  
  93.     /**
  94.      * Format Count
  95.      *
  96.      * @var int 
  97.      */
  98.     private $_format 0;
  99.  
  100.     /**
  101.      * PHPExcel_Reader_IReadFilter instance
  102.      *
  103.      * @var PHPExcel_Reader_IReadFilter 
  104.      */
  105.     private $_readFilter null;
  106.  
  107.     /**
  108.      * Create a new PHPExcel_Reader_SYLK
  109.      */
  110.     public function __construct({
  111.         $this->_inputEncoding 'ANSI';
  112.         $this->_delimiter     ';';
  113.         $this->_enclosure     '"';
  114.         $this->_lineEnding     PHP_EOL;
  115.         $this->_sheetIndex     0;
  116.         $this->_readFilter     new PHPExcel_Reader_DefaultReadFilter();
  117.     }
  118.  
  119.     /**
  120.      * Can the current PHPExcel_Reader_IReader read the file?
  121.      *
  122.      * @param     string         $pFileName 
  123.      * @return     boolean 
  124.      */
  125.     public function canRead($pFilename)
  126.     {
  127.         // Check if file exists
  128.         if (!file_exists($pFilename)) {
  129.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  130.         }
  131.  
  132.         // Read sample data (first 2 KB will do)
  133.         $fh fopen($pFilename'r');
  134.         $data fread($fh2048);
  135.         fclose($fh);
  136.  
  137.         // Count delimiters in file
  138.         $delimiterCount substr_count($data';');
  139.         if ($delimiterCount 1{
  140.             return false;
  141.         }
  142.  
  143.         // Analyze first line looking for ID; signature
  144.         $lines explode("\n"$data);
  145.         if (substr($lines[0],0,4!= 'ID;P'{
  146.             return false;
  147.         }
  148.  
  149.         return true;
  150.     }
  151.  
  152.     /**
  153.      * Loads PHPExcel from file
  154.      *
  155.      * @param     string         $pFilename 
  156.      * @return     PHPExcel 
  157.      * @throws     Exception
  158.      */
  159.     public function load($pFilename)
  160.     {
  161.         // Create new PHPExcel
  162.         $objPHPExcel new PHPExcel();
  163.  
  164.         // Load into this instance
  165.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  166.     }
  167.  
  168.     /**
  169.      * Read filter
  170.      *
  171.      * @return PHPExcel_Reader_IReadFilter 
  172.      */
  173.     public function getReadFilter({
  174.         return $this->_readFilter;
  175.     }
  176.  
  177.     /**
  178.      * Set read filter
  179.      *
  180.      * @param PHPExcel_Reader_IReadFilter $pValue 
  181.      */
  182.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  183.         $this->_readFilter $pValue;
  184.         return $this;
  185.     }
  186.  
  187.     /**
  188.      * Set input encoding
  189.      *
  190.      * @param string $pValue Input encoding
  191.      */
  192.     public function setInputEncoding($pValue 'ANSI')
  193.     {
  194.         $this->_inputEncoding $pValue;
  195.         return $this;
  196.     }
  197.  
  198.     /**
  199.      * Get input encoding
  200.      *
  201.      * @return string 
  202.      */
  203.     public function getInputEncoding()
  204.     {
  205.         return $this->_inputEncoding;
  206.     }
  207.  
  208.     /**
  209.      * Loads PHPExcel from file into PHPExcel instance
  210.      *
  211.      * @param     string         $pFilename 
  212.      * @param    PHPExcel    $objPHPExcel 
  213.      * @return     PHPExcel 
  214.      * @throws     Exception
  215.      */
  216.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  217.     {
  218.         // Check if file exists
  219.         if (!file_exists($pFilename)) {
  220.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  221.         }
  222.  
  223.         // Create new PHPExcel
  224.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  225.             $objPHPExcel->createSheet();
  226.         }
  227.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  228.  
  229.         $fromFormats    array('\-',    '\ ');
  230.         $toFormats        array('-',    ' ');
  231.  
  232.         // Open file
  233.         $fileHandle fopen($pFilename'r');
  234.         if ($fileHandle === false{
  235.             throw new Exception("Could not open file $pFilename for reading.");
  236.         }
  237.  
  238.         // Loop through file
  239.         $rowData array();
  240.         $column $row '';
  241.  
  242.         // loop through one row (line) at a time in the file
  243.         while (($rowData fgets($fileHandle)) !== FALSE{
  244.  
  245.             // convert SYLK encoded $rowData to UTF-8
  246.             $rowData PHPExcel_Shared_String::SYLKtoUTF8($rowData);
  247.  
  248.             // explode each row at semicolons while taking into account that literal semicolon (;)
  249.             // is escaped like this (;;)
  250.             $rowData explode("\t",str_replace('¤',';',str_replace(';',"\t",str_replace(';;','¤',rtrim($rowData)))));
  251.  
  252.             $dataType array_shift($rowData);
  253.             //    Read shared styles
  254.             if ($dataType == 'P'{
  255.                 $formatArray array();
  256.                 foreach($rowData as $rowDatum{
  257.                     switch($rowDatum{0}{
  258.                         case 'P' :    $formatArray['numberformat']['code'str_replace($fromFormats,$toFormats,substr($rowDatum,1));
  259.                                     break;
  260.                         case 'E' :
  261.                         case 'F' :    $formatArray['font']['name'substr($rowDatum,1);
  262.                                     break;
  263.                         case 'L' :    $formatArray['font']['size'substr($rowDatum,1);
  264.                                     break;
  265.                         case 'S' :    $styleSettings substr($rowDatum,1);
  266.                                     for ($i=0;$i<strlen($styleSettings);++$i{
  267.                                         switch ($styleSettings{$i}{
  268.                                             case 'I' :    $formatArray['font']['italic'true;
  269.                                                         break;
  270.                                             case 'D' :    $formatArray['font']['bold'true;
  271.                                                         break;
  272.                                             case 'T' :    $formatArray['borders']['top']['style'PHPExcel_Style_Border::BORDER_THIN;
  273.                                                         break;
  274.                                             case 'B' :    $formatArray['borders']['bottom']['style'PHPExcel_Style_Border::BORDER_THIN;
  275.                                                         break;
  276.                                             case 'L' :    $formatArray['borders']['left']['style'PHPExcel_Style_Border::BORDER_THIN;
  277.                                                         break;
  278.                                             case 'R' :    $formatArray['borders']['right']['style'PHPExcel_Style_Border::BORDER_THIN;
  279.                                                         break;
  280.                                         }
  281.                                     }
  282.                                     break;
  283.                     }
  284.                 }
  285.                 $this->_formats['P'.$this->_format++$formatArray;
  286.             //    Read cell value data
  287.             elseif ($dataType == 'C'{
  288.                 $hasCalculatedValue false;
  289.                 $cellData $cellDataFormula '';
  290.                 foreach($rowData as $rowDatum{
  291.                     switch($rowDatum{0}{
  292.                         case 'C' :
  293.                         case 'X' :    $column substr($rowDatum,1);
  294.                                     break;
  295.                         case 'R' :
  296.                         case 'Y' :    $row substr($rowDatum,1);
  297.                                     break;
  298.                         case 'K' :    $cellData substr($rowDatum,1);
  299.                                     break;
  300.                         case 'E' :    $cellDataFormula '='.substr($rowDatum,1);
  301.                                     //    Convert R1C1 style references to A1 style references (but only when not quoted)
  302.                                     $temp explode('"',$cellDataFormula);
  303.                                     foreach($temp as $key => &$value{
  304.                                         //    Only count/replace in alternate array entries
  305.                                         if (($key 2== 0{
  306.                                             preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value$cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
  307.                                             //    Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  308.                                             //        through the formula from left to right. Reversing means that we work right to left.through
  309.                                             //        the formula
  310.                                             $cellReferences array_reverse($cellReferences);
  311.                                             //    Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  312.                                             //        then modify the formula to use that new reference
  313.                                             foreach($cellReferences as $cellReference{
  314.                                                 $rowReference $cellReference[2][0];
  315.                                                 //    Empty R reference is the current row
  316.                                                 if ($rowReference == ''$rowReference $row;
  317.                                                 //    Bracketed R references are relative to the current row
  318.                                                 if ($rowReference{0== '['$rowReference $row trim($rowReference,'[]');
  319.                                                 $columnReference $cellReference[4][0];
  320.                                                 //    Empty C reference is the current column
  321.                                                 if ($columnReference == ''$columnReference $column;
  322.                                                 //    Bracketed C references are relative to the current column
  323.                                                 if ($columnReference{0== '['$columnReference $column trim($columnReference,'[]');
  324.                                                 $A1CellReference PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
  325.  
  326.                                                 $value substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
  327.                                             }
  328.                                         }
  329.                                     }
  330.                                     unset($value);
  331.                                     //    Then rebuild the formula string
  332.                                     $cellDataFormula implode('"',$temp);
  333.                                     $hasCalculatedValue true;
  334.                                     break;
  335.                     }
  336.                 }
  337.                 $columnLetter PHPExcel_Cell::stringFromColumnIndex($column-1);
  338.                 $cellData PHPExcel_Calculation::_unwrapResult($cellData);
  339.  
  340.                 // Set cell value
  341.                 $objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setValue(($hasCalculatedValue$cellDataFormula $cellData);
  342.                 if ($hasCalculatedValue{
  343.                     $cellData PHPExcel_Calculation::_unwrapResult($cellData);
  344.                     $objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setCalculatedValue($cellData);
  345.                 }
  346.             //    Read cell formatting
  347.             elseif ($dataType == 'F'{
  348.                 $formatStyle $columnWidth $styleSettings '';
  349.                 $styleData array();
  350.                 foreach($rowData as $rowDatum{
  351.                     switch($rowDatum{0}{
  352.                         case 'C' :
  353.                         case 'X' :    $column substr($rowDatum,1);
  354.                                     break;
  355.                         case 'R' :
  356.                         case 'Y' :    $row substr($rowDatum,1);
  357.                                     break;
  358.                         case 'P' :    $formatStyle $rowDatum;
  359.                                     break;
  360.                         case 'W' :    list($startCol,$endCol,$columnWidthexplode(' ',substr($rowDatum,1));
  361.                                     break;
  362.                         case 'S' :    $styleSettings substr($rowDatum,1);
  363.                                     for ($i=0;$i<strlen($styleSettings);++$i{
  364.                                         switch ($styleSettings{$i}{
  365.                                             case 'I' :    $styleData['font']['italic'true;
  366.                                                         break;
  367.                                             case 'D' :    $styleData['font']['bold'true;
  368.                                                         break;
  369.                                             case 'T' :    $styleData['borders']['top']['style'PHPExcel_Style_Border::BORDER_THIN;
  370.                                                         break;
  371.                                             case 'B' :    $styleData['borders']['bottom']['style'PHPExcel_Style_Border::BORDER_THIN;
  372.                                                         break;
  373.                                             case 'L' :    $styleData['borders']['left']['style'PHPExcel_Style_Border::BORDER_THIN;
  374.                                                         break;
  375.                                             case 'R' :    $styleData['borders']['right']['style'PHPExcel_Style_Border::BORDER_THIN;
  376.                                                         break;
  377.                                         }
  378.                                     }
  379.                                     break;
  380.                     }
  381.                 }
  382.                 if (($formatStyle ''&& ($column ''&& ($row '')) {
  383.                     $columnLetter PHPExcel_Cell::stringFromColumnIndex($column-1);
  384.                     $objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($this->_formats[$formatStyle]);
  385.                 }
  386.                 if ((count($styleData0&& ($column ''&& ($row '')) {
  387.                     $columnLetter PHPExcel_Cell::stringFromColumnIndex($column-1);
  388.                     $objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($styleData);
  389.                 }
  390.                 if ($columnWidth ''{
  391.                     if ($startCol == $endCol{
  392.                         $startCol PHPExcel_Cell::stringFromColumnIndex($startCol-1);
  393.                         $objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
  394.                     else {
  395.                         $startCol PHPExcel_Cell::stringFromColumnIndex($startCol-1);
  396.                         $endCol PHPExcel_Cell::stringFromColumnIndex($endCol-1);
  397.                         $objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
  398.                         do {
  399.                             $objPHPExcel->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth);
  400.                         while ($startCol != $endCol);
  401.                     }
  402.                 }
  403.             else {
  404.                 foreach($rowData as $rowDatum{
  405.                     switch($rowDatum{0}{
  406.                         case 'C' :
  407.                         case 'X' :    $column substr($rowDatum,1);
  408.                                     break;
  409.                         case 'R' :
  410.                         case 'Y' :    $row substr($rowDatum,1);
  411.                                     break;
  412.                     }
  413.                 }
  414.             }
  415.         }
  416.  
  417.         // Close file
  418.         fclose($fileHandle);
  419.  
  420.         // Return
  421.         return $objPHPExcel;
  422.     }
  423.  
  424.     /**
  425.      * Get delimiter
  426.      *
  427.      * @return string 
  428.      */
  429.     public function getDelimiter({
  430.         return $this->_delimiter;
  431.     }
  432.  
  433.     /**
  434.      * Set delimiter
  435.      *
  436.      * @param    string    $pValue        Delimiter, defaults to ,
  437.      * @return PHPExcel_Reader_SYLK 
  438.      */
  439.     public function setDelimiter($pValue ','{
  440.         $this->_delimiter $pValue;
  441.         return $this;
  442.     }
  443.  
  444.     /**
  445.      * Get enclosure
  446.      *
  447.      * @return string 
  448.      */
  449.     public function getEnclosure({
  450.         return $this->_enclosure;
  451.     }
  452.  
  453.     /**
  454.      * Set enclosure
  455.      *
  456.      * @param    string    $pValue        Enclosure, defaults to "
  457.      * @return PHPExcel_Reader_SYLK 
  458.      */
  459.     public function setEnclosure($pValue '"'{
  460.         if ($pValue == ''{
  461.             $pValue '"';
  462.         }
  463.         $this->_enclosure $pValue;
  464.         return $this;
  465.     }
  466.  
  467.     /**
  468.      * Get line ending
  469.      *
  470.      * @return string 
  471.      */
  472.     public function getLineEnding({
  473.         return $this->_lineEnding;
  474.     }
  475.  
  476.     /**
  477.      * Set line ending
  478.      *
  479.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  480.      * @return PHPExcel_Reader_SYLK 
  481.      */
  482.     public function setLineEnding($pValue PHP_EOL{
  483.         $this->_lineEnding $pValue;
  484.         return $this;
  485.     }
  486.  
  487.     /**
  488.      * Get sheet index
  489.      *
  490.      * @return int 
  491.      */
  492.     public function getSheetIndex({
  493.         return $this->_sheetIndex;
  494.     }
  495.  
  496.     /**
  497.      * Set sheet index
  498.      *
  499.      * @param    int        $pValue        Sheet index
  500.      * @return PHPExcel_Reader_SYLK 
  501.      */
  502.     public function setSheetIndex($pValue 0{
  503.         $this->_sheetIndex $pValue;
  504.         return $this;
  505.     }
  506.  
  507. }

Documentation generated on Tue, 01 Jun 2010 17:06:16 +0200 by phpDocumentor 1.4.3