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.5, 2009-01-05
  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.      * Loads PHPExcel from file
  91.      *
  92.      * @param     string         $pFilename 
  93.      * @throws     Exception
  94.      */
  95.     public function load($pFilename)
  96.     {
  97.         // Create new PHPExcel
  98.         $objPHPExcel new PHPExcel();
  99.  
  100.         // Load into this instance
  101.         return $this->loadIntoExisting($pFilename$objPHPExcel);
  102.     }
  103.  
  104.     /**
  105.      * Loads PHPExcel from file into PHPExcel instance
  106.      *
  107.      * @param     string         $pFilename 
  108.      * @param    PHPExcel    $objPHPExcel 
  109.      * @throws     Exception
  110.      */
  111.     public function loadIntoExisting($pFilenamePHPExcel $objPHPExcel)
  112.     {
  113.         // Check if file exists
  114.         if (!file_exists($pFilename)) {
  115.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  116.         }
  117.  
  118.         // Create new PHPExcel
  119.         while ($objPHPExcel->getSheetCount(<= $this->_sheetIndex{
  120.             $objPHPExcel->createSheet();
  121.         }
  122.         $objPHPExcel->setActiveSheetIndex$this->_sheetIndex );
  123.  
  124.         // Open file
  125.         $fileHandle fopen($pFilename'r');
  126.         if ($fileHandle === false{
  127.             throw new Exception("Could not open file $pFilename for reading.");
  128.         }
  129.  
  130.         // Loop trough file
  131.         $currentRow 0;
  132.         $rowData array();
  133.         while (($rowData fgetcsv($fileHandle0$this->_delimiter$this->_enclosure)) !== FALSE{
  134.             ++$currentRow;
  135.             $rowDataCount count($rowData);
  136.             for ($i 0$i $rowDataCount++$i{
  137.                 if ($rowData[$i!= ''{
  138.                     // Unescape enclosures
  139.                     $rowData[$istr_replace("\\" $this->_enclosure$this->_enclosure$rowData[$i]);
  140.                     $rowData[$istr_replace($this->_enclosure . $this->_enclosure$this->_enclosure$rowData[$i]);
  141.  
  142.                     // Set cell value
  143.                     $objPHPExcel->getActiveSheet()->setCellValue(
  144.                         PHPExcel_Cell::stringFromColumnIndex($i$currentRow$rowData[$i]
  145.                     );
  146.                 }
  147.             }
  148.         }
  149.  
  150.         // Close file
  151.         fclose($fileHandle);
  152.  
  153.         // Return
  154.         return $objPHPExcel;
  155.     }
  156.  
  157.     /**
  158.      * Get delimiter
  159.      *
  160.      * @return string 
  161.      */
  162.     public function getDelimiter({
  163.         return $this->_delimiter;
  164.     }
  165.  
  166.     /**
  167.      * Set delimiter
  168.      *
  169.      * @param    string    $pValue        Delimiter, defaults to ,
  170.      */
  171.     public function setDelimiter($pValue ','{
  172.         $this->_delimiter = $pValue;
  173.     }
  174.  
  175.     /**
  176.      * Get enclosure
  177.      *
  178.      * @return string 
  179.      */
  180.     public function getEnclosure({
  181.         return $this->_enclosure;
  182.     }
  183.  
  184.     /**
  185.      * Set enclosure
  186.      *
  187.      * @param    string    $pValue        Enclosure, defaults to "
  188.      */
  189.     public function setEnclosure($pValue '"'{
  190.         if ($pValue == ''{
  191.             $pValue '"';
  192.         }
  193.         $this->_enclosure = $pValue;
  194.     }
  195.  
  196.     /**
  197.      * Get line ending
  198.      *
  199.      * @return string 
  200.      */
  201.     public function getLineEnding({
  202.         return $this->_lineEnding;
  203.     }
  204.  
  205.     /**
  206.      * Set line ending
  207.      *
  208.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  209.      */
  210.     public function setLineEnding($pValue PHP_EOL{
  211.         $this->_lineEnding = $pValue;
  212.     }
  213.  
  214.     /**
  215.      * Get sheet index
  216.      *
  217.      * @return int 
  218.      */
  219.     public function getSheetIndex({
  220.         return $this->_sheetIndex;
  221.     }
  222.  
  223.     /**
  224.      * Set sheet index
  225.      *
  226.      * @param    int        $pValue        Sheet index
  227.      */
  228.     public function setSheetIndex($pValue 0{
  229.         $this->_sheetIndex = $pValue;
  230.     }
  231. }

Documentation generated on Mon, 05 Jan 2009 20:36:44 +0100 by phpDocumentor 1.4.1