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.6.7, 2009-04-22
  26.  */
  27.  
  28.  
  29. /** PHPExcel */
  30. require_once 'PHPExcel.php';
  31.  
  32. /** PHPExcel_Reader_IReader */
  33. require_once 'PHPExcel/Reader/IReader.php';
  34.  
  35. /** PHPExcel_Worksheet */
  36. require_once 'PHPExcel/Worksheet.php';
  37.  
  38. /** PHPExcel_Cell */
  39. require_once 'PHPExcel/Cell.php';
  40.  
  41.  
  42. /**
  43.  * PHPExcel_Reader_CSV
  44.  *
  45.  * @category   PHPExcel
  46.  * @package    PHPExcel_Reader
  47.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  48.  */
  49. class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
  50. {
  51.     /**
  52.      * Delimiter
  53.      *
  54.      * @var string 
  55.      */
  56.     private $_delimiter;
  57.  
  58.     /**
  59.      * Enclosure
  60.      *
  61.      * @var string 
  62.      */
  63.     private $_enclosure;
  64.  
  65.     /**
  66.      * Line ending
  67.      *
  68.      * @var string 
  69.      */
  70.     private $_lineEnding;
  71.  
  72.     /**
  73.      * Sheet index to read
  74.      *
  75.      * @var int 
  76.      */
  77.     private $_sheetIndex;
  78.  
  79.     /**
  80.      * Create a new PHPExcel_Reader_CSV
  81.      */
  82.     public function __construct({
  83.         $this->_delimiter     = ',';
  84.         $this->_enclosure     = '"';
  85.         $this->_lineEnding     = PHP_EOL;
  86.         $this->_sheetIndex     = 0;
  87.     }
  88.     
  89.     /**
  90.      * Can the current PHPExcel_Reader_IReader read the file?
  91.      *
  92.      * @param     string         $pFileName 
  93.      * @return     boolean 
  94.      */    
  95.     public function canRead($pFilename
  96.     {
  97.         // Check if file exists
  98.         if (!file_exists($pFilename)) {
  99.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  100.         }
  101.         
  102.         // Check if it is a CSV file (using file name)
  103.         return (substr(strtolower($pFilename)-3== 'csv');
  104.     }
  105.  
  106.     /**
  107.      * Loads PHPExcel from file
  108.      *
  109.      * @param     string         $pFilename 
  110.      * @throws     Exception
  111.      */
  112.     public function load($pFilename)
  113.     {
  114.         // Create new PHPExcel
  115.         $objPHPExcel new PHPExcel();
  116.  
  117.         // Load into this instance
  118.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  119.     }
  120.  
  121.     /**
  122.      * Loads PHPExcel from file into PHPExcel instance
  123.      *
  124.      * @param     string         $pFilename 
  125.      * @param    PHPExcel    $objPHPExcel 
  126.      * @throws     Exception
  127.      */
  128.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  129.     {
  130.         // Check if file exists
  131.         if (!file_exists($pFilename)) {
  132.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  133.         }
  134.  
  135.         // Create new PHPExcel
  136.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  137.             $objPHPExcel->createSheet();
  138.         }
  139.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  140.  
  141.         // Open file
  142.         $fileHandle fopen($pFilename'r');
  143.         if ($fileHandle === false{
  144.             throw new Exception("Could not open file $pFilename for reading.");
  145.         }
  146.  
  147.         // Loop trough file
  148.         $currentRow 0;
  149.         $rowData array();
  150.         while (($rowData fgetcsv($fileHandle0$this->_delimiter$this->_enclosure)) !== FALSE{
  151.             ++$currentRow;
  152.             $rowDataCount count($rowData);
  153.             for ($i 0$i $rowDataCount++$i{
  154.                 if ($rowData[$i!= ''{
  155.                     // Unescape enclosures
  156.                     $rowData[$istr_replace("\\" $this->_enclosure$this->_enclosure$rowData[$i]);
  157.                     $rowData[$istr_replace($this->_enclosure . $this->_enclosure$this->_enclosure$rowData[$i]);
  158.  
  159.                     // Set cell value
  160.                     $objPHPExcel->getActiveSheet()->setCellValue(
  161.                         PHPExcel_Cell::stringFromColumnIndex($i$currentRow$rowData[$i]
  162.                     );
  163.                 }
  164.             }
  165.         }
  166.  
  167.         // Close file
  168.         fclose($fileHandle);
  169.  
  170.         // Return
  171.         return $objPHPExcel;
  172.     }
  173.  
  174.     /**
  175.      * Get delimiter
  176.      *
  177.      * @return string 
  178.      */
  179.     public function getDelimiter({
  180.         return $this->_delimiter;
  181.     }
  182.  
  183.     /**
  184.      * Set delimiter
  185.      *
  186.      * @param    string    $pValue        Delimiter, defaults to ,
  187.      */
  188.     public function setDelimiter($pValue ','{
  189.         $this->_delimiter = $pValue;
  190.     }
  191.  
  192.     /**
  193.      * Get enclosure
  194.      *
  195.      * @return string 
  196.      */
  197.     public function getEnclosure({
  198.         return $this->_enclosure;
  199.     }
  200.  
  201.     /**
  202.      * Set enclosure
  203.      *
  204.      * @param    string    $pValue        Enclosure, defaults to "
  205.      */
  206.     public function setEnclosure($pValue '"'{
  207.         if ($pValue == ''{
  208.             $pValue '"';
  209.         }
  210.         $this->_enclosure = $pValue;
  211.     }
  212.  
  213.     /**
  214.      * Get line ending
  215.      *
  216.      * @return string 
  217.      */
  218.     public function getLineEnding({
  219.         return $this->_lineEnding;
  220.     }
  221.  
  222.     /**
  223.      * Set line ending
  224.      *
  225.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  226.      */
  227.     public function setLineEnding($pValue PHP_EOL{
  228.         $this->_lineEnding = $pValue;
  229.     }
  230.  
  231.     /**
  232.      * Get sheet index
  233.      *
  234.      * @return int 
  235.      */
  236.     public function getSheetIndex({
  237.         return $this->_sheetIndex;
  238.     }
  239.  
  240.     /**
  241.      * Set sheet index
  242.      *
  243.      * @param    int        $pValue        Sheet index
  244.      */
  245.     public function setSheetIndex($pValue 0{
  246.         $this->_sheetIndex = $pValue;
  247.     }
  248. }

Documentation generated on Wed, 22 Apr 2009 08:56:54 +0200 by phpDocumentor 1.4.1