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

Documentation generated on Mon, 11 Jan 2010 08:07:30 +0100 by phpDocumentor 1.4.1