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.2, 2010-01-11
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35. }
  36.  
  37. /** PHPExcel */
  38. require_once PHPEXCEL_ROOT 'PHPExcel.php';
  39.  
  40. /** PHPExcel_Reader_IReader */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/IReader.php';
  42.  
  43. /** PHPExcel_Worksheet */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Worksheet.php';
  45.  
  46. /** PHPExcel_Cell */
  47. require_once PHPEXCEL_ROOT 'PHPExcel/Cell.php';
  48.  
  49. /** PHPExcel_Calculation */
  50. require_once PHPEXCEL_ROOT 'PHPExcel/Calculation.php';
  51.  
  52.  /** PHPExcel_Reader_DefaultReadFilter */
  53. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/DefaultReadFilter.php';
  54.  
  55.  
  56. /**
  57.  * PHPExcel_Reader_SYLK
  58.  *
  59.  * @category   PHPExcel
  60.  * @package    PHPExcel_Reader
  61.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  62.  */
  63. class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
  64. {
  65.     /**
  66.      * Input encoding
  67.      *
  68.      * @var string 
  69.      */
  70.     private $_inputEncoding;
  71.  
  72.     /**
  73.      * Delimiter
  74.      *
  75.      * @var string 
  76.      */
  77.     private $_delimiter;
  78.  
  79.     /**
  80.      * Enclosure
  81.      *
  82.      * @var string 
  83.      */
  84.     private $_enclosure;
  85.  
  86.     /**
  87.      * Line ending
  88.      *
  89.      * @var string 
  90.      */
  91.     private $_lineEnding;
  92.  
  93.     /**
  94.      * Sheet index to read
  95.      *
  96.      * @var int 
  97.      */
  98.     private $_sheetIndex;
  99.  
  100.     /**
  101.      * Formats
  102.      *
  103.      * @var array 
  104.      */
  105.     private $_formats = array();
  106.  
  107.     /**
  108.      * Format Count
  109.      *
  110.      * @var int 
  111.      */
  112.     private $_format = 0;
  113.  
  114.     /**
  115.      * PHPExcel_Reader_IReadFilter instance
  116.      *
  117.      * @var PHPExcel_Reader_IReadFilter 
  118.      */
  119.     private $_readFilter = null;
  120.  
  121.     /**
  122.      * Create a new PHPExcel_Reader_SYLK
  123.      */
  124.     public function __construct({
  125.         $this->_inputEncoding = 'ANSI';
  126.         $this->_delimiter     = ';';
  127.         $this->_enclosure     = '"';
  128.         $this->_lineEnding     = PHP_EOL;
  129.         $this->_sheetIndex     = 0;
  130.         $this->_readFilter     = new PHPExcel_Reader_DefaultReadFilter();
  131.     }
  132.  
  133.     /**
  134.      * Can the current PHPExcel_Reader_IReader read the file?
  135.      *
  136.      * @param     string         $pFileName 
  137.      * @return     boolean 
  138.      */
  139.     public function canRead($pFilename)
  140.     {
  141.         // Check if file exists
  142.         if (!file_exists($pFilename)) {
  143.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  144.         }
  145.  
  146.         // Read sample data (first 2 KB will do)
  147.         $fh fopen($pFilename'r');
  148.         $data fread($fh2048);
  149.         fclose($fh);
  150.  
  151.         // Count delimiters in file
  152.         $delimiterCount substr_count($data';');
  153.         if ($delimiterCount 1{
  154.             return false;
  155.         }
  156.  
  157.         // Analyze first line looking for ID; signature
  158.         $lines explode("\n"$data);
  159.         if (substr($lines[0],0,4!= 'ID;P'{
  160.             return false;
  161.         }
  162.  
  163.         return true;
  164.     }
  165.  
  166.     /**
  167.      * Loads PHPExcel from file
  168.      *
  169.      * @param     string         $pFilename 
  170.      * @throws     Exception
  171.      */
  172.     public function load($pFilename)
  173.     {
  174.         // Create new PHPExcel
  175.         $objPHPExcel new PHPExcel();
  176.  
  177.         // Load into this instance
  178.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  179.     }
  180.  
  181.     /**
  182.      * Read filter
  183.      *
  184.      * @return PHPExcel_Reader_IReadFilter 
  185.      */
  186.     public function getReadFilter({
  187.         return $this->_readFilter;
  188.     }
  189.  
  190.     /**
  191.      * Set read filter
  192.      *
  193.      * @param PHPExcel_Reader_IReadFilter $pValue 
  194.      */
  195.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  196.         $this->_readFilter = $pValue;
  197.     }
  198.  
  199.     /**
  200.      * Set input encoding
  201.      *
  202.      * @param string $pValue Input encoding
  203.      */
  204.     public function setInputEncoding($pValue 'ANSI')
  205.     {
  206.         $this->_inputEncoding = $pValue;
  207.     }
  208.  
  209.     /**
  210.      * Get input encoding
  211.      *
  212.      * @return string 
  213.      */
  214.     public function getInputEncoding()
  215.     {
  216.         return $this->_inputEncoding;
  217.     }
  218.  
  219.     /**
  220.      * Loads PHPExcel from file into PHPExcel instance
  221.      *
  222.      * @param     string         $pFilename 
  223.      * @param    PHPExcel    $objPHPExcel 
  224.      * @throws     Exception
  225.      */
  226.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  227.     {
  228.         // Check if file exists
  229.         if (!file_exists($pFilename)) {
  230.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  231.         }
  232.  
  233.         // Create new PHPExcel
  234.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  235.             $objPHPExcel->createSheet();
  236.         }
  237.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  238.  
  239.         $fromFormats    array('\-',    '\ ');
  240.         $toFormats        array('-',    ' ');
  241.  
  242.         // Open file
  243.         $fileHandle fopen($pFilename'r');
  244.         if ($fileHandle === false{
  245.             throw new Exception("Could not open file $pFilename for reading.");
  246.         }
  247.  
  248.         // Loop through file
  249.         $rowData array();
  250.         $column $row '';
  251.         while (($rowData fgets($fileHandle)) !== FALSE{
  252.             $rowData explode("\t",str_replace('¤',';',str_replace(';',"\t",str_replace(';;','¤',rtrim($rowData)))));
  253.             $dataType array_shift($rowData);
  254.             //    Read shared styles
  255.             if ($dataType == 'P'{
  256.                 $formatArray array();
  257.                 foreach($rowData as $rowDatum{
  258.                     switch($rowDatum{0}{
  259.                         case 'P' :    $formatArray['numberformat']['code'str_replace($fromFormats,$toFormats,substr($rowDatum,1));
  260.                                     break;
  261.                         case 'E' :
  262.                         case 'F' :    $formatArray['font']['name'substr($rowDatum,1);
  263.                                     break;
  264.                         case 'L' :    $formatArray['font']['size'substr($rowDatum,1);
  265.                                     break;
  266.                         case 'S' :    $styleSettings substr($rowDatum,1);
  267.                                     for ($i=0;$i<strlen($styleSettings);++$i{
  268.                                         switch ($styleSettings{$i}{
  269.                                             case 'I' :    $formatArray['font']['italic'true;
  270.                                                         break;
  271.                                             case 'D' :    $formatArray['font']['bold'true;
  272.                                                         break;
  273.                                             case 'T' :    $formatArray['borders']['top']['style'PHPExcel_Style_Border::BORDER_THIN;
  274.                                                         break;
  275.                                             case 'B' :    $formatArray['borders']['bottom']['style'PHPExcel_Style_Border::BORDER_THIN;
  276.                                                         break;
  277.                                             case 'L' :    $formatArray['borders']['left']['style'PHPExcel_Style_Border::BORDER_THIN;
  278.                                                         break;
  279.                                             case 'R' :    $formatArray['borders']['right']['style'PHPExcel_Style_Border::BORDER_THIN;
  280.                                                         break;
  281.                                         }
  282.                                     }
  283.                                     break;
  284.                     }
  285.                 }
  286.                 $this->_formats['P'.$this->_format++$formatArray;
  287.             //    Read cell value data
  288.             elseif ($dataType == 'C'{
  289.                 $hasCalculatedValue false;
  290.                 $cellData $cellDataFormula '';
  291.                 foreach($rowData as $rowDatum{
  292.                     switch($rowDatum{0}{
  293.                         case 'C' :
  294.                         case 'X' :    $column substr($rowDatum,1);
  295.                                     break;
  296.                         case 'R' :
  297.                         case 'Y' :    $row substr($rowDatum,1);
  298.                                     break;
  299.                         case 'K' :    $cellData substr($rowDatum,1);
  300.                                     break;
  301.                         case 'E' :    $cellDataFormula '='.substr($rowDatum,1);
  302.                                     //    Convert R1C1 style references to A1 style references (but only when not quoted)
  303.                                     $temp explode('"',$cellDataFormula);
  304.                                     foreach($temp as $key => &$value{
  305.                                         //    Only count/replace in alternate array entries
  306.                                         if (($key 2== 0{
  307.                                             preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value$cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
  308.                                             //    Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  309.                                             //        through the formula from left to right. Reversing means that we work right to left.through
  310.                                             //        the formula
  311.                                             $cellReferences array_reverse($cellReferences);
  312.                                             //    Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  313.                                             //        then modify the formula to use that new reference
  314.                                             foreach($cellReferences as $cellReference{
  315.                                                 $rowReference $cellReference[2][0];
  316.                                                 //    Empty R reference is the current row
  317.                                                 if ($rowReference == ''$rowReference $row;
  318.                                                 //    Bracketed R references are relative to the current row
  319.                                                 if ($rowReference{0== '['$rowReference $row trim($rowReference,'[]');
  320.                                                 $columnReference $cellReference[4][0];
  321.                                                 //    Empty C reference is the current column
  322.                                                 if ($columnReference == ''$columnReference $column;
  323.                                                 //    Bracketed C references are relative to the current column
  324.                                                 if ($columnReference{0== '['$columnReference $column trim($columnReference,'[]');
  325.                                                 $A1CellReference PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
  326.  
  327.                                                 $value substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
  328.                                             }
  329.                                         }
  330.                                     }
  331.                                     unset($value);
  332.                                     //    Then rebuild the formula string
  333.                                     $cellDataFormula implode('"',$temp);
  334.                                     $hasCalculatedValue true;
  335.                                     break;
  336.                     }
  337.                 }
  338.                 $columnLetter PHPExcel_Cell::stringFromColumnIndex($column-1);
  339.                 $cellData PHPExcel_Calculation::_unwrapResult($cellData);
  340.                 // Set cell value
  341.                 $objPHPExcel->getActiveSheet()->setCellValue($columnLetter.$row(($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. }

Documentation generated on Mon, 11 Jan 2010 08:14:33 +0100 by phpDocumentor 1.4.1