Source for file CSV.php
Documentation is available at CSV.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
private $_inputEncoding = 'UTF-8';
private $_delimiter = ',';
private $_enclosure = '"';
private $_lineEnding = PHP_EOL;
private $_sheetIndex = 0;
private $_contiguous = false;
* Row counter for loading rows contiguously
private $_contiguousRow = - 1;
* PHPExcel_Reader_IReadFilter instance
* @var PHPExcel_Reader_IReadFilter
private $_readFilter = null;
* Create a new PHPExcel_Reader_CSV
} // function __construct()
* Can the current PHPExcel_Reader_IReader read the file?
* @param string $pFileName
public function canRead($pFilename)
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
* Loads PHPExcel from file
* @param string $pFilename
public function load($pFilename)
// Load into this instance
* @return PHPExcel_Reader_IReadFilter
return $this->_readFilter;
} // function getReadFilter()
* @param PHPExcel_Reader_IReadFilter $pValue
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
} // function setReadFilter()
* @param string $pValue Input encoding
$this->_inputEncoding = $pValue;
} // function setInputEncoding()
return $this->_inputEncoding;
} // function getInputEncoding()
* Loads PHPExcel from file into PHPExcel instance
* @param string $pFilename
* @param PHPExcel $objPHPExcel
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
$fileHandle = fopen($pFilename, 'r');
if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for reading.");
switch ($this->_inputEncoding) {
fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
fgets($fileHandle, 3) == "\xFF\xFE" ?
fgets($fileHandle, 3) == "\xFE\xFF" ?
fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
$escapeEnclosures = array( "\\" . $this->_enclosure,
$this->_enclosure . $this->_enclosure
// Set our starting row based on whether we're in contiguous mode or not
if ($this->_contiguous) {
$currentRow = ($this->_contiguousRow == - 1) ? $objPHPExcel->getActiveSheet()->getHighestRow(): $this->_contiguousRow;
// Loop through each line of the file in turn
while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
foreach($rowData as $rowDatum) {
if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
$rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
// Convert encoding if necessary
if ($this->_inputEncoding !== 'UTF-8') {
$objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowDatum);
if ($this->_contiguous) {
$this->_contiguousRow = $currentRow;
} // function loadIntoExisting()
return $this->_delimiter;
} // function getDelimiter()
* @param string $pValue Delimiter, defaults to ,
* @return PHPExcel_Reader_CSV
$this->_delimiter = $pValue;
} // function setDelimiter()
return $this->_enclosure;
} // function getEnclosure()
* @param string $pValue Enclosure, defaults to "
* @return PHPExcel_Reader_CSV
$this->_enclosure = $pValue;
} // function setEnclosure()
return $this->_lineEnding;
} // function getLineEnding()
* @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
* @return PHPExcel_Reader_CSV
$this->_lineEnding = $pValue;
} // function setLineEnding()
return $this->_sheetIndex;
} // function getSheetIndex()
* @param int $pValue Sheet index
* @return PHPExcel_Reader_CSV
$this->_sheetIndex = $pValue;
} // function setSheetIndex()
* @param string $pValue Input encoding
$this->_contiguous = (bool) $contiguous;
$this->_contiguousRow = - 1;
} // function setInputEncoding()
return $this->_contiguous;
} // function getSheetIndex()
|