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

Source for file CSV.php

Documentation is available at CSV.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  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. }
  37.  
  38. /**
  39.  * PHPExcel_Reader_CSV
  40.  *
  41.  * @category   PHPExcel
  42.  * @package    PHPExcel_Reader
  43.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  44.  */
  45. class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
  46. {
  47.     /**
  48.      *    Input encoding
  49.      *
  50.      *    @access    private
  51.      *    @var    string 
  52.      */
  53.     private $_inputEncoding    'UTF-8';
  54.  
  55.     /**
  56.      *    Delimiter
  57.      *
  58.      *    @access    private
  59.      *    @var string 
  60.      */
  61.     private $_delimiter        ',';
  62.  
  63.     /**
  64.      *    Enclosure
  65.      *
  66.      *    @access    private
  67.      *    @var    string 
  68.      */
  69.     private $_enclosure        '"';
  70.  
  71.     /**
  72.      *    Line ending
  73.      *
  74.      *    @access    private
  75.      *    @var    string 
  76.      */
  77.     private $_lineEnding    PHP_EOL;
  78.  
  79.     /**
  80.      *    Sheet index to read
  81.      *
  82.      *    @access    private
  83.      *    @var    int 
  84.      */
  85.     private $_sheetIndex    0;
  86.  
  87.     /**
  88.      *    Load rows contiguously
  89.      *
  90.      *    @access    private
  91.      *    @var    int 
  92.      */
  93.     private $_contiguous    false;
  94.  
  95.  
  96.     /**
  97.      *    Row counter for loading rows contiguously
  98.      *
  99.      *    @access    private
  100.      *    @var    int 
  101.      */
  102.     private $_contiguousRow    = -1;
  103.  
  104.     /**
  105.      *    PHPExcel_Reader_IReadFilter instance
  106.      *
  107.      *    @access    private
  108.      *    @var    PHPExcel_Reader_IReadFilter 
  109.      */
  110.     private $_readFilter null;
  111.  
  112.     /**
  113.      *    Create a new PHPExcel_Reader_CSV
  114.      */
  115.     public function __construct({
  116.         $this->_readFilter        new PHPExcel_Reader_DefaultReadFilter();
  117.     }    //    function __construct()
  118.  
  119.     /**
  120.      *    Can the current PHPExcel_Reader_IReader read the file?
  121.      *
  122.      *    @access    public
  123.      *    @param     string         $pFileName 
  124.      *    @return boolean 
  125.      *    @throws Exception
  126.      */
  127.     public function canRead($pFilename)
  128.     {
  129.         // Check if file exists
  130.         if (!file_exists($pFilename)) {
  131.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  132.         }
  133.  
  134.         return true;
  135.     }    //    function canRead()
  136.  
  137.     /**
  138.      *    Loads PHPExcel from file
  139.      *
  140.      *    @access    public
  141.      *    @param     string         $pFilename 
  142.      *    @return PHPExcel 
  143.      *    @throws Exception
  144.      */
  145.     public function load($pFilename)
  146.     {
  147.         // Create new PHPExcel
  148.         $objPHPExcel new PHPExcel();
  149.  
  150.         // Load into this instance
  151.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  152.     }    //    function load()
  153.  
  154.     /**
  155.      *    Read filter
  156.      *
  157.      *    @access    public
  158.      *    @return PHPExcel_Reader_IReadFilter 
  159.      */
  160.     public function getReadFilter({
  161.         return $this->_readFilter;
  162.     }    //    function getReadFilter()
  163.  
  164.     /**
  165.      *    Set read filter
  166.      *
  167.      *    @access    public
  168.      *    @param    PHPExcel_Reader_IReadFilter $pValue 
  169.      */
  170.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  171.         $this->_readFilter $pValue;
  172.         return $this;
  173.     }    //    function setReadFilter()
  174.  
  175.     /**
  176.      *    Set input encoding
  177.      *
  178.      *    @access    public
  179.      *    @param string $pValue Input encoding
  180.      */
  181.     public function setInputEncoding($pValue 'UTF-8')
  182.     {
  183.         $this->_inputEncoding $pValue;
  184.         return $this;
  185.     }    //    function setInputEncoding()
  186.  
  187.     /**
  188.      *    Get input encoding
  189.      *
  190.      *    @access    public
  191.      *    @return string 
  192.      */
  193.     public function getInputEncoding()
  194.     {
  195.         return $this->_inputEncoding;
  196.     }    //    function getInputEncoding()
  197.  
  198.     /**
  199.      *    Loads PHPExcel from file into PHPExcel instance
  200.      *
  201.      *    @access    public
  202.      *    @param     string         $pFilename 
  203.      *    @param    PHPExcel    $objPHPExcel 
  204.      *    @return     PHPExcel 
  205.      *    @throws     Exception
  206.      */
  207.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  208.     {
  209.         // Check if file exists
  210.         if (!file_exists($pFilename)) {
  211.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  212.         }
  213.  
  214.         // Create new PHPExcel
  215.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  216.             $objPHPExcel->createSheet();
  217.         }
  218.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  219.  
  220.         // Open file
  221.         $fileHandle fopen($pFilename'r');
  222.         if ($fileHandle === false{
  223.             throw new Exception("Could not open file $pFilename for reading.");
  224.         }
  225.  
  226.         // Skip BOM, if any
  227.         switch ($this->_inputEncoding{
  228.             case 'UTF-8':
  229.                 fgets($fileHandle4== "\xEF\xBB\xBF" ?
  230.                     fseek($fileHandle3fseek($fileHandle0);
  231.                 break;
  232.             case 'UTF-16LE':
  233.                 fgets($fileHandle3== "\xFF\xFE" ?
  234.                     fseek($fileHandle2fseek($fileHandle0);
  235.                 break;
  236.             case 'UTF-16BE':
  237.                 fgets($fileHandle3== "\xFE\xFF" ?
  238.                     fseek($fileHandle2fseek($fileHandle0);
  239.                 break;
  240.             case 'UTF-32LE':
  241.                 fgets($fileHandle5== "\xFF\xFE\x00\x00" ?
  242.                     fseek($fileHandle4fseek($fileHandle0);
  243.                 break;
  244.             case 'UTF-32BE':
  245.                 fgets($fileHandle5== "\x00\x00\xFE\xFF" ?
  246.                     fseek($fileHandle4fseek($fileHandle0);
  247.                 break;
  248.             default:
  249.                 break;
  250.         }
  251.  
  252.         $escapeEnclosures array"\\" $this->_enclosure,
  253.                                    $this->_enclosure $this->_enclosure
  254.                                  );
  255.  
  256.         // Set our starting row based on whether we're in contiguous mode or not
  257.         $currentRow 1;
  258.         if ($this->_contiguous{
  259.             $currentRow ($this->_contiguousRow == -1$objPHPExcel->getActiveSheet()->getHighestRow()$this->_contiguousRow;
  260.         }
  261.  
  262.         // Loop through each line of the file in turn
  263.         while (($rowData fgetcsv($fileHandle0$this->_delimiter$this->_enclosure)) !== FALSE{
  264.             $columnLetter 'A';
  265.             foreach($rowData as $rowDatum{
  266.                 if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter$currentRow)) {
  267.                     // Unescape enclosures
  268.                     $rowDatum str_replace($escapeEnclosures$this->_enclosure$rowDatum);
  269.  
  270.                     // Convert encoding if necessary
  271.                     if ($this->_inputEncoding !== 'UTF-8'{
  272.                         $rowDatum PHPExcel_Shared_String::ConvertEncoding($rowDatum'UTF-8'$this->_inputEncoding);
  273.                     }
  274.  
  275.                     // Set cell value
  276.                     $objPHPExcel->getActiveSheet()->getCell($columnLetter $currentRow)->setValue($rowDatum);
  277.                 }
  278.                 ++$columnLetter;
  279.             }
  280.             ++$currentRow;
  281.         }
  282.  
  283.         // Close file
  284.         fclose($fileHandle);
  285.  
  286.         if ($this->_contiguous{
  287.             $this->_contiguousRow $currentRow;
  288.         }
  289.  
  290.         // Return
  291.         return $objPHPExcel;
  292.     }    //    function loadIntoExisting()
  293.  
  294.     /**
  295.      *    Get delimiter
  296.      *
  297.      *    @access    public
  298.      *    @return string 
  299.      */
  300.     public function getDelimiter({
  301.         return $this->_delimiter;
  302.     }    //    function getDelimiter()
  303.  
  304.     /**
  305.      *    Set delimiter
  306.      *
  307.      *    @access    public
  308.      *    @param    string    $pValue        Delimiter, defaults to ,
  309.      *    @return    PHPExcel_Reader_CSV 
  310.      */
  311.     public function setDelimiter($pValue ','{
  312.         $this->_delimiter $pValue;
  313.         return $this;
  314.     }    //    function setDelimiter()
  315.  
  316.     /**
  317.      *    Get enclosure
  318.      *
  319.      *    @access    public
  320.      *    @return string 
  321.      */
  322.     public function getEnclosure({
  323.         return $this->_enclosure;
  324.     }    //    function getEnclosure()
  325.  
  326.     /**
  327.      *    Set enclosure
  328.      *
  329.      *    @access    public
  330.      *    @param    string    $pValue        Enclosure, defaults to "
  331.      *    @return PHPExcel_Reader_CSV 
  332.      */
  333.     public function setEnclosure($pValue '"'{
  334.         if ($pValue == ''{
  335.             $pValue '"';
  336.         }
  337.         $this->_enclosure $pValue;
  338.         return $this;
  339.     }    //    function setEnclosure()
  340.  
  341.     /**
  342.      *    Get line ending
  343.      *
  344.      *    @access    public
  345.      *    @return string 
  346.      */
  347.     public function getLineEnding({
  348.         return $this->_lineEnding;
  349.     }    //    function getLineEnding()
  350.  
  351.     /**
  352.      *    Set line ending
  353.      *
  354.      *    @access    public
  355.      *    @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  356.      *    @return PHPExcel_Reader_CSV 
  357.      */
  358.     public function setLineEnding($pValue PHP_EOL{
  359.         $this->_lineEnding $pValue;
  360.         return $this;
  361.     }    //    function setLineEnding()
  362.  
  363.     /**
  364.      *    Get sheet index
  365.      *
  366.      *    @access    public
  367.      *    @return int 
  368.      */
  369.     public function getSheetIndex({
  370.         return $this->_sheetIndex;
  371.     }    //    function getSheetIndex()
  372.  
  373.     /**
  374.      *    Set sheet index
  375.      *
  376.      *    @access    public
  377.      *    @param    int        $pValue        Sheet index
  378.      *    @return PHPExcel_Reader_CSV 
  379.      */
  380.     public function setSheetIndex($pValue 0{
  381.         $this->_sheetIndex $pValue;
  382.         return $this;
  383.     }    //    function setSheetIndex()
  384.  
  385.     /**
  386.      *    Set Contiguous
  387.      *
  388.      *    @access    public
  389.      *    @param string $pValue Input encoding
  390.      */
  391.     public function setContiguous($contiguous false)
  392.     {
  393.         $this->_contiguous = (bool)$contiguous;
  394.         if (!$contiguous{
  395.             $this->_contiguousRow    = -1;
  396.         }
  397.  
  398.         return $this;
  399.     }    //    function setInputEncoding()
  400.  
  401.     /**
  402.      *    Get Contiguous
  403.      *
  404.      *    @access    public
  405.      *    @return boolean 
  406.      */
  407.     public function getContiguous({
  408.         return $this->_contiguous;
  409.     }    //    function getSheetIndex()
  410.  
  411. }

Documentation generated on Sun, 27 Feb 2011 16:29:16 -0800 by phpDocumentor 1.4.3