2010-08-26 19:14:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPExcel
|
|
|
|
*
|
2015-05-02 22:50:37 +00:00
|
|
|
* Copyright (c) 2006 - 2015 PHPExcel
|
2010-08-26 19:14:53 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @category PHPExcel
|
|
|
|
* @package PHPExcel_Reader
|
2015-05-02 22:50:37 +00:00
|
|
|
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
|
2015-05-11 21:38:52 +00:00
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2010-08-26 19:14:53 +00:00
|
|
|
* @version ##VERSION##, ##DATE##
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** PHPExcel root directory */
|
|
|
|
if (!defined('PHPEXCEL_ROOT')) {
|
2015-05-11 21:38:52 +00:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
|
|
|
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHPExcel_Reader_CSV
|
|
|
|
*
|
|
|
|
* @category PHPExcel
|
|
|
|
* @package PHPExcel_Reader
|
2015-05-02 22:50:37 +00:00
|
|
|
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
|
2010-08-26 19:14:53 +00:00
|
|
|
*/
|
2012-11-05 21:23:38 +00:00
|
|
|
class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
2010-08-26 19:14:53 +00:00
|
|
|
{
|
2015-05-11 21:38:52 +00:00
|
|
|
/**
|
|
|
|
* Input encoding
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $inputEncoding = 'UTF-8';
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delimiter
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $delimiter = ',';
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enclosure
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $enclosure = '"';
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sheet index to read
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $sheetIndex = 0;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load rows contiguously
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $contiguous = false;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Row counter for loading rows contiguously
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $contiguousRow = -1;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new PHPExcel_Reader_CSV
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that the current file is a CSV file
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function _isValidFormat()
|
|
|
|
{
|
2015-05-17 09:22:28 +00:00
|
|
|
return true;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set input encoding
|
|
|
|
*
|
|
|
|
* @param string $pValue Input encoding
|
|
|
|
*/
|
|
|
|
public function setInputEncoding($pValue = 'UTF-8')
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->inputEncoding = $pValue;
|
2015-05-11 21:38:52 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get input encoding
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInputEncoding()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->inputEncoding;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move filepointer past any BOM marker
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function _skipBOM()
|
|
|
|
{
|
|
|
|
rewind($this->_fileHandle);
|
|
|
|
|
2015-05-18 15:39:04 +00:00
|
|
|
switch ($this->inputEncoding) {
|
2015-05-11 21:38:52 +00:00
|
|
|
case 'UTF-8':
|
|
|
|
fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ?
|
|
|
|
fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0);
|
|
|
|
break;
|
|
|
|
case 'UTF-16LE':
|
|
|
|
fgets($this->_fileHandle, 3) == "\xFF\xFE" ?
|
|
|
|
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
|
|
|
|
break;
|
|
|
|
case 'UTF-16BE':
|
|
|
|
fgets($this->_fileHandle, 3) == "\xFE\xFF" ?
|
|
|
|
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
|
|
|
|
break;
|
|
|
|
case 'UTF-32LE':
|
|
|
|
fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ?
|
|
|
|
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
|
|
|
|
break;
|
|
|
|
case 'UTF-32BE':
|
|
|
|
fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ?
|
|
|
|
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
|
|
|
|
*
|
|
|
|
* @param string $pFilename
|
|
|
|
* @throws PHPExcel_Reader_Exception
|
|
|
|
*/
|
|
|
|
public function listWorksheetInfo($pFilename)
|
|
|
|
{
|
|
|
|
// Open file
|
|
|
|
$this->_openFile($pFilename);
|
|
|
|
if (!$this->_isValidFormat()) {
|
2015-05-17 09:22:28 +00:00
|
|
|
fclose($this->_fileHandle);
|
2015-05-11 21:38:52 +00:00
|
|
|
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
|
|
|
}
|
|
|
|
$fileHandle = $this->_fileHandle;
|
|
|
|
|
|
|
|
// Skip BOM, if any
|
|
|
|
$this->_skipBOM();
|
|
|
|
|
2015-05-18 15:39:04 +00:00
|
|
|
$escapeEnclosures = array( "\\" . $this->enclosure, $this->enclosure . $this->enclosure );
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
$worksheetInfo = array();
|
|
|
|
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
|
|
|
|
$worksheetInfo[0]['lastColumnLetter'] = 'A';
|
|
|
|
$worksheetInfo[0]['lastColumnIndex'] = 0;
|
|
|
|
$worksheetInfo[0]['totalRows'] = 0;
|
|
|
|
$worksheetInfo[0]['totalColumns'] = 0;
|
|
|
|
|
|
|
|
// Loop through each line of the file in turn
|
2015-05-18 15:39:04 +00:00
|
|
|
while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) {
|
2015-05-11 21:38:52 +00:00
|
|
|
$worksheetInfo[0]['totalRows']++;
|
|
|
|
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
|
|
|
|
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
fclose($fileHandle);
|
|
|
|
|
|
|
|
return $worksheetInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads PHPExcel from file
|
|
|
|
*
|
|
|
|
* @param string $pFilename
|
|
|
|
* @return PHPExcel
|
|
|
|
* @throws PHPExcel_Reader_Exception
|
|
|
|
*/
|
|
|
|
public function load($pFilename)
|
|
|
|
{
|
|
|
|
// Create new PHPExcel
|
|
|
|
$objPHPExcel = new PHPExcel();
|
|
|
|
|
|
|
|
// Load into this instance
|
|
|
|
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads PHPExcel from file into PHPExcel instance
|
|
|
|
*
|
|
|
|
* @param string $pFilename
|
|
|
|
* @param PHPExcel $objPHPExcel
|
|
|
|
* @return PHPExcel
|
|
|
|
* @throws PHPExcel_Reader_Exception
|
|
|
|
*/
|
|
|
|
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
|
|
|
{
|
|
|
|
$lineEnding = ini_get('auto_detect_line_endings');
|
|
|
|
ini_set('auto_detect_line_endings', true);
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
$this->_openFile($pFilename);
|
|
|
|
if (!$this->_isValidFormat()) {
|
2015-05-17 09:22:28 +00:00
|
|
|
fclose($this->_fileHandle);
|
2015-05-11 21:38:52 +00:00
|
|
|
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
|
|
|
}
|
|
|
|
$fileHandle = $this->_fileHandle;
|
|
|
|
|
|
|
|
// Skip BOM, if any
|
|
|
|
$this->_skipBOM();
|
|
|
|
|
|
|
|
// Create new PHPExcel object
|
2015-05-18 15:39:04 +00:00
|
|
|
while ($objPHPExcel->getSheetCount() <= $this->sheetIndex) {
|
2015-05-11 21:38:52 +00:00
|
|
|
$objPHPExcel->createSheet();
|
|
|
|
}
|
2015-05-18 15:39:04 +00:00
|
|
|
$sheet = $objPHPExcel->setActiveSheetIndex($this->sheetIndex);
|
2015-05-11 21:38:52 +00:00
|
|
|
|
2015-05-18 15:39:04 +00:00
|
|
|
$escapeEnclosures = array( "\\" . $this->enclosure,
|
|
|
|
$this->enclosure . $this->enclosure
|
2015-05-11 21:38:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Set our starting row based on whether we're in contiguous mode or not
|
|
|
|
$currentRow = 1;
|
2015-05-18 15:39:04 +00:00
|
|
|
if ($this->contiguous) {
|
|
|
|
$currentRow = ($this->contiguousRow == -1) ? $sheet->getHighestRow(): $this->contiguousRow;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through each line of the file in turn
|
2015-05-18 15:39:04 +00:00
|
|
|
while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) {
|
2015-05-11 21:38:52 +00:00
|
|
|
$columnLetter = 'A';
|
2015-05-13 10:27:01 +00:00
|
|
|
foreach ($rowData as $rowDatum) {
|
2015-05-11 21:38:52 +00:00
|
|
|
if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
|
|
|
|
// Unescape enclosures
|
2015-05-18 15:39:04 +00:00
|
|
|
$rowDatum = str_replace($escapeEnclosures, $this->enclosure, $rowDatum);
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
// Convert encoding if necessary
|
2015-05-18 15:39:04 +00:00
|
|
|
if ($this->inputEncoding !== 'UTF-8') {
|
|
|
|
$rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->inputEncoding);
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set cell value
|
|
|
|
$sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
|
|
|
|
}
|
|
|
|
++$columnLetter;
|
|
|
|
}
|
|
|
|
++$currentRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
fclose($fileHandle);
|
|
|
|
|
2015-05-18 15:39:04 +00:00
|
|
|
if ($this->contiguous) {
|
|
|
|
$this->contiguousRow = $currentRow;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ini_set('auto_detect_line_endings', $lineEnding);
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return $objPHPExcel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get delimiter
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function getDelimiter()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->delimiter;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set delimiter
|
|
|
|
*
|
|
|
|
* @param string $pValue Delimiter, defaults to ,
|
|
|
|
* @return PHPExcel_Reader_CSV
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function setDelimiter($pValue = ',')
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->delimiter = $pValue;
|
2015-05-11 21:38:52 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get enclosure
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function getEnclosure()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->enclosure;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set enclosure
|
|
|
|
*
|
|
|
|
* @param string $pValue Enclosure, defaults to "
|
|
|
|
* @return PHPExcel_Reader_CSV
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function setEnclosure($pValue = '"')
|
|
|
|
{
|
2015-05-11 21:38:52 +00:00
|
|
|
if ($pValue == '') {
|
|
|
|
$pValue = '"';
|
|
|
|
}
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->enclosure = $pValue;
|
2015-05-11 21:38:52 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sheet index
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function getSheetIndex()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->sheetIndex;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set sheet index
|
|
|
|
*
|
|
|
|
* @param integer $pValue Sheet index
|
|
|
|
* @return PHPExcel_Reader_CSV
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function setSheetIndex($pValue = 0)
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->sheetIndex = $pValue;
|
2015-05-11 21:38:52 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Contiguous
|
|
|
|
*
|
|
|
|
* @param boolean $contiguous
|
|
|
|
*/
|
2015-05-17 09:22:28 +00:00
|
|
|
public function setContiguous($contiguous = false)
|
2015-05-11 21:38:52 +00:00
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->contiguous = (bool) $contiguous;
|
2015-05-11 21:38:52 +00:00
|
|
|
if (!$contiguous) {
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->contiguousRow = -1;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Contiguous
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-05-17 13:00:02 +00:00
|
|
|
public function getContiguous()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->contiguous;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|