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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  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 - 2008 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($pFilename, PHPExcel $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($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  134.             $currentRow++;
  135.             for ($i = 0; $i < count($rowData); $i++) {
  136.                 if ($rowData[$i] != '') {
  137.                     // Unescape enclosures
  138.                     $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  139.                     $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  140.                 
  141.                     // Set cell value
  142.                     $objPHPExcel->getActiveSheet()->setCellValue(
  143.                         PHPExcel_Cell::stringFromColumnIndex($i) . $currentRow, $rowData[$i]
  144.                     );
  145.                 }
  146.             }
  147.         }
  148.  
  149.         // Close file
  150.         fclose($fileHandle);
  151.         
  152.         // Return
  153.         return $objPHPExcel;
  154.     }
  155.  
  156.     /**
  157.      * Get delimiter
  158.      * 
  159.      * @return string 
  160.      */
  161.     public function getDelimiter() {
  162.         return $this->_delimiter;
  163.     }
  164.     
  165.     /**
  166.      * Set delimiter
  167.      * 
  168.      * @param    string    $pValue        Delimiter, defaults to ,
  169.      */
  170.     public function setDelimiter($pValue = ',') {
  171.         $this->_delimiter = $pValue;
  172.     }
  173.     
  174.     /**
  175.      * Get enclosure
  176.      * 
  177.      * @return string 
  178.      */
  179.     public function getEnclosure() {
  180.         return $this->_enclosure;
  181.     }
  182.     
  183.     /**
  184.      * Set enclosure
  185.      * 
  186.      * @param    string    $pValue        Enclosure, defaults to "
  187.      */
  188.     public function setEnclosure($pValue = '"') {
  189.         if ($pValue == '') {
  190.             $pValue = '"';
  191.         }
  192.         $this->_enclosure = $pValue;
  193.     }
  194.     
  195.     /**
  196.      * Get line ending
  197.      * 
  198.      * @return string 
  199.      */
  200.     public function getLineEnding() {
  201.         return $this->_lineEnding;
  202.     }
  203.     
  204.     /**
  205.      * Set line ending
  206.      * 
  207.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  208.      */
  209.     public function setLineEnding($pValue = PHP_EOL) {
  210.         $this->_lineEnding = $pValue;
  211.     }
  212.     
  213.     /**
  214.      * Get sheet index
  215.      * 
  216.      * @return int 
  217.      */
  218.     public function getSheetIndex() {
  219.         return $this->_sheetIndex;
  220.     }
  221.     
  222.     /**
  223.      * Set sheet index
  224.      * 
  225.      * @param    int        $pValue        Sheet index
  226.      */
  227.     public function setSheetIndex($pValue = 0) {
  228.         $this->_sheetIndex = $pValue;
  229.     }
  230. }

Documentation generated on Mon, 25 Aug 2008 13:33:58 +0200 by phpDocumentor 1.4.1