5 * Copyright (c) 2006 - 2014 PHPExcel
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.
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.
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
22 * @package PHPExcel_Reader
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
29 /** PHPExcel root directory */
30 if (!defined('PHPEXCEL_ROOT')) {
34 define('PHPEXCEL_ROOT', dirname(__FILE__
) . '/../../');
35 require(PHPEXCEL_ROOT
. 'PHPExcel/Autoloader.php');
42 * @package PHPExcel_Reader
43 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
45 class PHPExcel_Reader_CSV
extends PHPExcel_Reader_Abstract
implements PHPExcel_Reader_IReader
53 private $_inputEncoding = 'UTF-8';
61 private $_delimiter = ',';
69 private $_enclosure = '"';
77 private $_sheetIndex = 0;
80 * Load rows contiguously
85 private $_contiguous = false;
88 * Row counter for loading rows contiguously
92 private $_contiguousRow = -1;
96 * Create a new PHPExcel_Reader_CSV
98 public function __construct() {
99 $this->_readFilter
= new PHPExcel_Reader_DefaultReadFilter();
103 * Validate that the current file is a CSV file
107 protected function _isValidFormat()
115 * @param string $pValue Input encoding
117 public function setInputEncoding($pValue = 'UTF-8')
119 $this->_inputEncoding
= $pValue;
128 public function getInputEncoding()
130 return $this->_inputEncoding
;
134 * Move filepointer past any BOM marker
137 protected function _skipBOM()
139 rewind($this->_fileHandle
);
141 switch ($this->_inputEncoding
) {
143 fgets($this->_fileHandle
, 4) == "\xEF\xBB\xBF" ?
144 fseek($this->_fileHandle
, 3) : fseek($this->_fileHandle
, 0);
147 fgets($this->_fileHandle
, 3) == "\xFF\xFE" ?
148 fseek($this->_fileHandle
, 2) : fseek($this->_fileHandle
, 0);
151 fgets($this->_fileHandle
, 3) == "\xFE\xFF" ?
152 fseek($this->_fileHandle
, 2) : fseek($this->_fileHandle
, 0);
155 fgets($this->_fileHandle
, 5) == "\xFF\xFE\x00\x00" ?
156 fseek($this->_fileHandle
, 4) : fseek($this->_fileHandle
, 0);
159 fgets($this->_fileHandle
, 5) == "\x00\x00\xFE\xFF" ?
160 fseek($this->_fileHandle
, 4) : fseek($this->_fileHandle
, 0);
168 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
170 * @param string $pFilename
171 * @throws PHPExcel_Reader_Exception
173 public function listWorksheetInfo($pFilename)
176 $this->_openFile($pFilename);
177 if (!$this->_isValidFormat()) {
178 fclose ($this->_fileHandle
);
179 throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
181 $fileHandle = $this->_fileHandle
;
186 $escapeEnclosures = array( "\\" . $this->_enclosure
, $this->_enclosure
. $this->_enclosure
);
188 $worksheetInfo = array();
189 $worksheetInfo[0]['worksheetName'] = 'Worksheet';
190 $worksheetInfo[0]['lastColumnLetter'] = 'A';
191 $worksheetInfo[0]['lastColumnIndex'] = 0;
192 $worksheetInfo[0]['totalRows'] = 0;
193 $worksheetInfo[0]['totalColumns'] = 0;
195 // Loop through each line of the file in turn
196 while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter
, $this->_enclosure
)) !== FALSE) {
197 $worksheetInfo[0]['totalRows']++
;
198 $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
201 $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell
::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
202 $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] +
1;
207 return $worksheetInfo;
211 * Loads PHPExcel from file
213 * @param string $pFilename
215 * @throws PHPExcel_Reader_Exception
217 public function load($pFilename)
219 // Create new PHPExcel
220 $objPHPExcel = new PHPExcel();
222 // Load into this instance
223 return $this->loadIntoExisting($pFilename, $objPHPExcel);
227 * Loads PHPExcel from file into PHPExcel instance
229 * @param string $pFilename
230 * @param PHPExcel $objPHPExcel
232 * @throws PHPExcel_Reader_Exception
234 public function loadIntoExisting($pFilename, PHPExcel
$objPHPExcel)
236 $lineEnding = ini_get('auto_detect_line_endings');
237 ini_set('auto_detect_line_endings', true);
240 $this->_openFile($pFilename);
241 if (!$this->_isValidFormat()) {
242 fclose ($this->_fileHandle
);
243 throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
245 $fileHandle = $this->_fileHandle
;
250 // Create new PHPExcel object
251 while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex
) {
252 $objPHPExcel->createSheet();
254 $sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex
);
256 $escapeEnclosures = array( "\\" . $this->_enclosure
,
257 $this->_enclosure
. $this->_enclosure
260 // Set our starting row based on whether we're in contiguous mode or not
262 if ($this->_contiguous
) {
263 $currentRow = ($this->_contiguousRow
== -1) ?
$sheet->getHighestRow(): $this->_contiguousRow
;
266 // Loop through each line of the file in turn
267 while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter
, $this->_enclosure
)) !== FALSE) {
269 foreach($rowData as $rowDatum) {
270 if ($rowDatum != '' && $this->_readFilter
->readCell($columnLetter, $currentRow)) {
271 // Unescape enclosures
272 $rowDatum = str_replace($escapeEnclosures, $this->_enclosure
, $rowDatum);
274 // Convert encoding if necessary
275 if ($this->_inputEncoding
!== 'UTF-8') {
276 $rowDatum = PHPExcel_Shared_String
::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding
);
280 $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
290 if ($this->_contiguous
) {
291 $this->_contiguousRow
= $currentRow;
294 ini_set('auto_detect_line_endings', $lineEnding);
305 public function getDelimiter() {
306 return $this->_delimiter
;
312 * @param string $pValue Delimiter, defaults to ,
313 * @return PHPExcel_Reader_CSV
315 public function setDelimiter($pValue = ',') {
316 $this->_delimiter
= $pValue;
325 public function getEnclosure() {
326 return $this->_enclosure
;
332 * @param string $pValue Enclosure, defaults to "
333 * @return PHPExcel_Reader_CSV
335 public function setEnclosure($pValue = '"') {
339 $this->_enclosure
= $pValue;
348 public function getSheetIndex() {
349 return $this->_sheetIndex
;
355 * @param integer $pValue Sheet index
356 * @return PHPExcel_Reader_CSV
358 public function setSheetIndex($pValue = 0) {
359 $this->_sheetIndex
= $pValue;
366 * @param boolean $contiguous
368 public function setContiguous($contiguous = FALSE)
370 $this->_contiguous
= (bool) $contiguous;
372 $this->_contiguousRow
= -1;
383 public function getContiguous() {
384 return $this->_contiguous
;