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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.0, 2009-08-10
  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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  59.  */
  60. class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
  61. {
  62.     /**
  63.      * Delimiter
  64.      *
  65.      * @var string 
  66.      */
  67.     private $_delimiter;
  68.  
  69.     /**
  70.      * Enclosure
  71.      *
  72.      * @var string 
  73.      */
  74.     private $_enclosure;
  75.  
  76.     /**
  77.      * Line ending
  78.      *
  79.      * @var string 
  80.      */
  81.     private $_lineEnding;
  82.  
  83.     /**
  84.      * Sheet index to read
  85.      *
  86.      * @var int 
  87.      */
  88.     private $_sheetIndex;
  89.  
  90.     /**
  91.      * PHPExcel_Reader_IReadFilter instance
  92.      *
  93.      * @var PHPExcel_Reader_IReadFilter 
  94.      */
  95.     private $_readFilter = null;
  96.  
  97.     /**
  98.      * Create a new PHPExcel_Reader_CSV
  99.      */
  100.     public function __construct({
  101.         $this->_delimiter     = ',';
  102.         $this->_enclosure     = '"';
  103.         $this->_lineEnding     = PHP_EOL;
  104.         $this->_sheetIndex     = 0;
  105.         $this->_readFilter     = new PHPExcel_Reader_DefaultReadFilter();
  106.     }
  107.     
  108.     /**
  109.      * Can the current PHPExcel_Reader_IReader read the file?
  110.      *
  111.      * @param     string         $pFileName 
  112.      * @return     boolean 
  113.      */    
  114.     public function canRead($pFilename
  115.     {
  116.         // Check if file exists
  117.         if (!file_exists($pFilename)) {
  118.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  119.         }
  120.         
  121.         // Check if it is a CSV file (using file name)
  122.         return (substr(strtolower($pFilename)-3== 'csv');
  123.     }
  124.  
  125.     /**
  126.      * Loads PHPExcel from file
  127.      *
  128.      * @param     string         $pFilename 
  129.      * @throws     Exception
  130.      */
  131.     public function load($pFilename)
  132.     {
  133.         // Create new PHPExcel
  134.         $objPHPExcel new PHPExcel();
  135.  
  136.         // Load into this instance
  137.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  138.     }
  139.  
  140.     /**
  141.      * Read filter
  142.      *
  143.      * @return PHPExcel_Reader_IReadFilter 
  144.      */
  145.     public function getReadFilter({
  146.         return $this->_readFilter;
  147.     }
  148.  
  149.     /**
  150.      * Set read filter
  151.      *
  152.      * @param PHPExcel_Reader_IReadFilter $pValue 
  153.      */
  154.     public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue{
  155.         $this->_readFilter = $pValue;
  156.     }
  157.  
  158.     /**
  159.      * Loads PHPExcel from file into PHPExcel instance
  160.      *
  161.      * @param     string         $pFilename 
  162.      * @param    PHPExcel    $objPHPExcel 
  163.      * @throws     Exception
  164.      */
  165.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  166.     {
  167.         // Check if file exists
  168.         if (!file_exists($pFilename)) {
  169.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  170.         }
  171.  
  172.         // Create new PHPExcel
  173.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  174.             $objPHPExcel->createSheet();
  175.         }
  176.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  177.  
  178.         // Open file
  179.         $fileHandle fopen($pFilename'r');
  180.         if ($fileHandle === false{
  181.             throw new Exception("Could not open file $pFilename for reading.");
  182.         }
  183.  
  184.         // Loop trough file
  185.         $currentRow 0;
  186.         $rowData array();
  187.         while (($rowData fgetcsv($fileHandle0$this->_delimiter$this->_enclosure)) !== FALSE{
  188.             ++$currentRow;
  189.             $rowDataCount count($rowData);
  190.             for ($i 0$i $rowDataCount++$i{
  191.                 $columnLetter PHPExcel_Cell::stringFromColumnIndex($i);
  192.                 if ($rowData[$i!= '' && $this->_readFilter->readCell($columnLetter$currentRow)) {
  193.                     // Unescape enclosures
  194.                     $rowData[$istr_replace("\\" $this->_enclosure$this->_enclosure$rowData[$i]);
  195.                     $rowData[$istr_replace($this->_enclosure . $this->_enclosure$this->_enclosure$rowData[$i]);
  196.  
  197.                     // Set cell value
  198.                     $objPHPExcel->getActiveSheet()->setCellValue(
  199.                          $columnLetter $currentRow$rowData[$i]
  200.                     );
  201.                 }
  202.             }
  203.         }
  204.  
  205.         // Close file
  206.         fclose($fileHandle);
  207.  
  208.         // Return
  209.         return $objPHPExcel;
  210.     }
  211.  
  212.     /**
  213.      * Get delimiter
  214.      *
  215.      * @return string 
  216.      */
  217.     public function getDelimiter({
  218.         return $this->_delimiter;
  219.     }
  220.  
  221.     /**
  222.      * Set delimiter
  223.      *
  224.      * @param    string    $pValue        Delimiter, defaults to ,
  225.      * @return PHPExcel_Reader_CSV 
  226.      */
  227.     public function setDelimiter($pValue ','{
  228.         $this->_delimiter = $pValue;
  229.         return $this;
  230.     }
  231.  
  232.     /**
  233.      * Get enclosure
  234.      *
  235.      * @return string 
  236.      */
  237.     public function getEnclosure({
  238.         return $this->_enclosure;
  239.     }
  240.  
  241.     /**
  242.      * Set enclosure
  243.      *
  244.      * @param    string    $pValue        Enclosure, defaults to "
  245.      * @return PHPExcel_Reader_CSV 
  246.      */
  247.     public function setEnclosure($pValue '"'{
  248.         if ($pValue == ''{
  249.             $pValue '"';
  250.         }
  251.         $this->_enclosure = $pValue;
  252.         return $this;
  253.     }
  254.  
  255.     /**
  256.      * Get line ending
  257.      *
  258.      * @return string 
  259.      */
  260.     public function getLineEnding({
  261.         return $this->_lineEnding;
  262.     }
  263.  
  264.     /**
  265.      * Set line ending
  266.      *
  267.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  268.      * @return PHPExcel_Reader_CSV 
  269.      */
  270.     public function setLineEnding($pValue PHP_EOL{
  271.         $this->_lineEnding = $pValue;
  272.         return $this;
  273.     }
  274.  
  275.     /**
  276.      * Get sheet index
  277.      *
  278.      * @return int 
  279.      */
  280.     public function getSheetIndex({
  281.         return $this->_sheetIndex;
  282.     }
  283.  
  284.     /**
  285.      * Set sheet index
  286.      *
  287.      * @param    int        $pValue        Sheet index
  288.      * @return PHPExcel_Reader_CSV 
  289.      */
  290.     public function setSheetIndex($pValue 0{
  291.         $this->_sheetIndex = $pValue;
  292.         return $this;
  293.     }
  294. }

Documentation generated on Mon, 10 Aug 2009 08:02:57 +0200 by phpDocumentor 1.4.1