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 - 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_CSV
  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_CSV implements PHPExcel_Reader_IReader
  50. {
  51.     /**
  52.      *    Input encoding
  53.      *
  54.      *    @access    private
  55.      *    @var    string 
  56.      */
  57.     private $_inputEncoding;
  58.  
  59.     /**
  60.      *    Delimiter
  61.      *
  62.      *    @access    private
  63.      *    @var string 
  64.      */
  65.     private $_delimiter;
  66.  
  67.     /**
  68.      *    Enclosure
  69.      *
  70.      *    @access    private
  71.      *    @var    string 
  72.      */
  73.     private $_enclosure;
  74.  
  75.     /**
  76.      *    Line ending
  77.      *
  78.      *    @access    private
  79.      *    @var    string 
  80.      */
  81.     private $_lineEnding;
  82.  
  83.     /**
  84.      *    Sheet index to read
  85.      *
  86.      *    @access    private
  87.      *    @var    int 
  88.      */
  89.     private $_sheetIndex;
  90.  
  91.     /**
  92.      *    PHPExcel_Reader_IReadFilter instance
  93.      *
  94.      *    @access    private
  95.      *    @var    PHPExcel_Reader_IReadFilter 
  96.      */
  97.     private $_readFilter null;
  98.  
  99.     /**
  100.      *    Create a new PHPExcel_Reader_CSV
  101.      */
  102.     public function __construct({
  103.         $this->_inputEncoding 'UTF-8';
  104.         $this->_delimiter     ',';
  105.         $this->_enclosure     '"';
  106.         $this->_lineEnding     PHP_EOL;
  107.         $this->_sheetIndex     0;
  108.         $this->_readFilter     new PHPExcel_Reader_DefaultReadFilter();
  109.     }    //    function __construct()
  110.  
  111.     /**
  112.      *    Can the current PHPExcel_Reader_IReader read the file?
  113.      *
  114.      *    @access    public
  115.      *    @param     string         $pFileName 
  116.      *    @return boolean 
  117.      *    @throws Exception
  118.      */
  119.     public function canRead($pFilename)
  120.     {
  121.         // Check if file exists
  122.         if (!file_exists($pFilename)) {
  123.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  124.         }
  125.  
  126.         return true;
  127.     }    //    function canRead()
  128.  
  129.     /**
  130.      *    Loads PHPExcel from file
  131.      *
  132.      *    @access    public
  133.      *    @param     string         $pFilename 
  134.      *    @return PHPExcel 
  135.      *    @throws Exception
  136.      */
  137.     public function load($pFilename)
  138.     {
  139.         // Create new PHPExcel
  140.         $objPHPExcel new PHPExcel();
  141.  
  142.         // Load into this instance
  143.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  144.     }    //    function load()
  145.  
  146.     /**
  147.      *    Read filter
  148.      *
  149.      *    @access    public
  150.      *    @return PHPExcel_Reader_IReadFilter 
  151.      */
  152.     public function getReadFilter({
  153.         return $this->_readFilter;
  154.     }    //    function getReadFilter()
  155.  
  156.     /**
  157.      *    Set read filter
  158.      *
  159.      *    @access    public
  160.      *    @param    PHPExcel_Reader_IReadFilter $pValue 
  161.      */
  162.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  163.         $this->_readFilter $pValue;
  164.         return $this;
  165.     }    //    function setReadFilter()
  166.  
  167.     /**
  168.      *    Set input encoding
  169.      *
  170.      *    @access    public
  171.      *    @param string $pValue Input encoding
  172.      */
  173.     public function setInputEncoding($pValue 'UTF-8')
  174.     {
  175.         $this->_inputEncoding $pValue;
  176.         return $this;
  177.     }    //    function setInputEncoding()
  178.  
  179.     /**
  180.      *    Get input encoding
  181.      *
  182.      *    @access    public
  183.      *    @return string 
  184.      */
  185.     public function getInputEncoding()
  186.     {
  187.         return $this->_inputEncoding;
  188.     }    //    function getInputEncoding()
  189.  
  190.     /**
  191.      *    Loads PHPExcel from file into PHPExcel instance
  192.      *
  193.      *    @access    public
  194.      *    @param     string         $pFilename 
  195.      *    @param    PHPExcel    $objPHPExcel 
  196.      *    @return     PHPExcel 
  197.      *    @throws     Exception
  198.      */
  199.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  200.     {
  201.         // Check if file exists
  202.         if (!file_exists($pFilename)) {
  203.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  204.         }
  205.  
  206.         // Create new PHPExcel
  207.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  208.             $objPHPExcel->createSheet();
  209.         }
  210.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  211.  
  212.         // Open file
  213.         $fileHandle fopen($pFilename'r');
  214.         if ($fileHandle === false{
  215.             throw new Exception("Could not open file $pFilename for reading.");
  216.         }
  217.  
  218.         // Skip BOM, if any
  219.         switch ($this->_inputEncoding{
  220.             case 'UTF-8':
  221.                 fgets($fileHandle4== "\xEF\xBB\xBF" ?
  222.                     fseek($fileHandle3fseek($fileHandle0);
  223.                 break;
  224.             default:
  225.                 break;
  226.         }
  227.  
  228.         // Loop through file
  229.         $currentRow 0;
  230.         $rowData array();
  231.         while (($rowData fgetcsv($fileHandle0$this->_delimiter$this->_enclosure)) !== FALSE{
  232.             ++$currentRow;
  233.             $rowDataCount count($rowData);
  234.             for ($i 0$i $rowDataCount++$i{
  235.                 $columnLetter PHPExcel_Cell::stringFromColumnIndex($i);
  236.                 if ($rowData[$i!= '' && $this->_readFilter->readCell($columnLetter$currentRow)) {
  237.                     // Unescape enclosures
  238.                     $rowData[$istr_replace("\\" $this->_enclosure$this->_enclosure$rowData[$i]);
  239.                     $rowData[$istr_replace($this->_enclosure $this->_enclosure$this->_enclosure$rowData[$i]);
  240.  
  241.                     // Convert encoding if necessary
  242.                     if ($this->_inputEncoding !== 'UTF-8'{
  243.                         $rowData[$iPHPExcel_Shared_String::ConvertEncoding($rowData[$i]'UTF-8'$this->_inputEncoding);
  244.                     }
  245.  
  246.                     // Set cell value
  247.                     $objPHPExcel->getActiveSheet()->getCell($columnLetter $currentRow)->setValue($rowData[$i]);
  248.                 }
  249.             }
  250.         }
  251.  
  252.         // Close file
  253.         fclose($fileHandle);
  254.  
  255.         // Return
  256.         return $objPHPExcel;
  257.     }    //    function loadIntoExisting()
  258.  
  259.     /**
  260.      *    Get delimiter
  261.      *
  262.      *    @access    public
  263.      *    @return string 
  264.      */
  265.     public function getDelimiter({
  266.         return $this->_delimiter;
  267.     }    //    function getDelimiter()
  268.  
  269.     /**
  270.      *    Set delimiter
  271.      *
  272.      *    @access    public
  273.      *    @param    string    $pValue        Delimiter, defaults to ,
  274.      *    @return    PHPExcel_Reader_CSV 
  275.      */
  276.     public function setDelimiter($pValue ','{
  277.         $this->_delimiter $pValue;
  278.         return $this;
  279.     }    //    function setDelimiter()
  280.  
  281.     /**
  282.      *    Get enclosure
  283.      *
  284.      *    @access    public
  285.      *    @return string 
  286.      */
  287.     public function getEnclosure({
  288.         return $this->_enclosure;
  289.     }    //    function getEnclosure()
  290.  
  291.     /**
  292.      *    Set enclosure
  293.      *
  294.      *    @access    public
  295.      *    @param    string    $pValue        Enclosure, defaults to "
  296.      *    @return PHPExcel_Reader_CSV 
  297.      */
  298.     public function setEnclosure($pValue '"'{
  299.         if ($pValue == ''{
  300.             $pValue '"';
  301.         }
  302.         $this->_enclosure $pValue;
  303.         return $this;
  304.     }    //    function setEnclosure()
  305.  
  306.     /**
  307.      *    Get line ending
  308.      *
  309.      *    @access    public
  310.      *    @return string 
  311.      */
  312.     public function getLineEnding({
  313.         return $this->_lineEnding;
  314.     }    //    function getLineEnding()
  315.  
  316.     /**
  317.      *    Set line ending
  318.      *
  319.      *    @access    public
  320.      *    @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  321.      *    @return PHPExcel_Reader_CSV 
  322.      */
  323.     public function setLineEnding($pValue PHP_EOL{
  324.         $this->_lineEnding $pValue;
  325.         return $this;
  326.     }    //    function setLineEnding()
  327.  
  328.     /**
  329.      *    Get sheet index
  330.      *
  331.      *    @access    public
  332.      *    @return int 
  333.      */
  334.     public function getSheetIndex({
  335.         return $this->_sheetIndex;
  336.     }    //    function getSheetIndex()
  337.  
  338.     /**
  339.      *    Set sheet index
  340.      *
  341.      *    @access    public
  342.      *    @param    int        $pValue        Sheet index
  343.      *    @return PHPExcel_Reader_CSV 
  344.      */
  345.     public function setSheetIndex($pValue 0{
  346.         $this->_sheetIndex $pValue;
  347.         return $this;
  348.     }    //    function setSheetIndex()
  349. }

Documentation generated on Tue, 01 Jun 2010 17:02:39 +0200 by phpDocumentor 1.4.3