| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHPExcel |
| 4 |
* |
| 5 |
* Copyright (c) 2006 - 2014 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 - 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## |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
/** PHPExcel root directory */ |
| 30 |
if (!defined('PHPEXCEL_ROOT')) { |
| 31 |
/** |
| 32 |
* @ignore |
| 33 |
*/ |
| 34 |
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../'); |
| 35 |
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* PHPExcel_Reader_CSV |
| 40 |
* |
| 41 |
* @category PHPExcel |
| 42 |
* @package PHPExcel_Reader |
| 43 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 44 |
*/ |
| 45 |
class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader |
| 46 |
{ |
| 47 |
/** |
| 48 |
* Input encoding |
| 49 |
* |
| 50 |
* @access private |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
private $_inputEncoding = 'UTF-8'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Delimiter |
| 57 |
* |
| 58 |
* @access private |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
private $_delimiter = ','; |
| 62 |
|
| 63 |
/** |
| 64 |
* Enclosure |
| 65 |
* |
| 66 |
* @access private |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
private $_enclosure = '"'; |
| 70 |
|
| 71 |
/** |
| 72 |
* Sheet index to read |
| 73 |
* |
| 74 |
* @access private |
| 75 |
* @var int |
| 76 |
*/ |
| 77 |
private $_sheetIndex = 0; |
| 78 |
|
| 79 |
/** |
| 80 |
* Load rows contiguously |
| 81 |
* |
| 82 |
* @access private |
| 83 |
* @var int |
| 84 |
*/ |
| 85 |
private $_contiguous = false; |
| 86 |
|
| 87 |
/** |
| 88 |
* Row counter for loading rows contiguously |
| 89 |
* |
| 90 |
* @var int |
| 91 |
*/ |
| 92 |
private $_contiguousRow = -1; |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Create a new PHPExcel_Reader_CSV |
| 97 |
*/ |
| 98 |
public function __construct() { |
| 99 |
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Validate that the current file is a CSV file |
| 104 |
* |
| 105 |
* @return boolean |
| 106 |
*/ |
| 107 |
protected function _isValidFormat() |
| 108 |
{ |
| 109 |
return TRUE; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Set input encoding |
| 114 |
* |
| 115 |
* @param string $pValue Input encoding |
| 116 |
*/ |
| 117 |
public function setInputEncoding($pValue = 'UTF-8') |
| 118 |
{ |
| 119 |
$this->_inputEncoding = $pValue; |
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get input encoding |
| 125 |
* |
| 126 |
* @return string |
| 127 |
*/ |
| 128 |
public function getInputEncoding() |
| 129 |
{ |
| 130 |
return $this->_inputEncoding; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Move filepointer past any BOM marker |
| 135 |
* |
| 136 |
*/ |
| 137 |
protected function _skipBOM() |
| 138 |
{ |
| 139 |
rewind($this->_fileHandle); |
| 140 |
|
| 141 |
switch ($this->_inputEncoding) { |
| 142 |
case 'UTF-8': |
| 143 |
fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ? |
| 144 |
fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0); |
| 145 |
break; |
| 146 |
case 'UTF-16LE': |
| 147 |
fgets($this->_fileHandle, 3) == "\xFF\xFE" ? |
| 148 |
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0); |
| 149 |
break; |
| 150 |
case 'UTF-16BE': |
| 151 |
fgets($this->_fileHandle, 3) == "\xFE\xFF" ? |
| 152 |
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0); |
| 153 |
break; |
| 154 |
case 'UTF-32LE': |
| 155 |
fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ? |
| 156 |
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0); |
| 157 |
break; |
| 158 |
case 'UTF-32BE': |
| 159 |
fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ? |
| 160 |
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0); |
| 161 |
break; |
| 162 |
default: |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) |
| 169 |
* |
| 170 |
* @param string $pFilename |
| 171 |
* @throws PHPExcel_Reader_Exception |
| 172 |
*/ |
| 173 |
public function listWorksheetInfo($pFilename) |
| 174 |
{ |
| 175 |
// Open file |
| 176 |
$this->_openFile($pFilename); |
| 177 |
if (!$this->_isValidFormat()) { |
| 178 |
fclose ($this->_fileHandle); |
| 179 |
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file."); |
| 180 |
} |
| 181 |
$fileHandle = $this->_fileHandle; |
| 182 |
|
| 183 |
// Skip BOM, if any |
| 184 |
$this->_skipBOM(); |
| 185 |
|
| 186 |
$escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure ); |
| 187 |
|
| 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; |
| 194 |
|
| 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); |
| 199 |
} |
| 200 |
|
| 201 |
$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']); |
| 202 |
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1; |
| 203 |
|
| 204 |
// Close file |
| 205 |
fclose($fileHandle); |
| 206 |
|
| 207 |
return $worksheetInfo; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Loads PHPExcel from file |
| 212 |
* |
| 213 |
* @param string $pFilename |
| 214 |
* @return PHPExcel |
| 215 |
* @throws PHPExcel_Reader_Exception |
| 216 |
*/ |
| 217 |
public function load($pFilename) |
| 218 |
{ |
| 219 |
// Create new PHPExcel |
| 220 |
$objPHPExcel = new PHPExcel(); |
| 221 |
|
| 222 |
// Load into this instance |
| 223 |
return $this->loadIntoExisting($pFilename, $objPHPExcel); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Loads PHPExcel from file into PHPExcel instance |
| 228 |
* |
| 229 |
* @param string $pFilename |
| 230 |
* @param PHPExcel $objPHPExcel |
| 231 |
* @return PHPExcel |
| 232 |
* @throws PHPExcel_Reader_Exception |
| 233 |
*/ |
| 234 |
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) |
| 235 |
{ |
| 236 |
$lineEnding = ini_get('auto_detect_line_endings'); |
| 237 |
ini_set('auto_detect_line_endings', true); |
| 238 |
|
| 239 |
// Open file |
| 240 |
$this->_openFile($pFilename); |
| 241 |
if (!$this->_isValidFormat()) { |
| 242 |
fclose ($this->_fileHandle); |
| 243 |
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file."); |
| 244 |
} |
| 245 |
$fileHandle = $this->_fileHandle; |
| 246 |
|
| 247 |
// Skip BOM, if any |
| 248 |
$this->_skipBOM(); |
| 249 |
|
| 250 |
// Create new PHPExcel object |
| 251 |
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) { |
| 252 |
$objPHPExcel->createSheet(); |
| 253 |
} |
| 254 |
$sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex); |
| 255 |
|
| 256 |
$escapeEnclosures = array( "\\" . $this->_enclosure, |
| 257 |
$this->_enclosure . $this->_enclosure |
| 258 |
); |
| 259 |
|
| 260 |
// Set our starting row based on whether we're in contiguous mode or not |
| 261 |
$currentRow = 1; |
| 262 |
if ($this->_contiguous) { |
| 263 |
$currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow; |
| 264 |
} |
| 265 |
|
| 266 |
// Loop through each line of the file in turn |
| 267 |
while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { |
| 268 |
$columnLetter = 'A'; |
| 269 |
foreach($rowData as $rowDatum) { |
| 270 |
if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) { |
| 271 |
// Unescape enclosures |
| 272 |
$rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum); |
| 273 |
|
| 274 |
// Convert encoding if necessary |
| 275 |
if ($this->_inputEncoding !== 'UTF-8') { |
| 276 |
$rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding); |
| 277 |
} |
| 278 |
|
| 279 |
// Set cell value |
| 280 |
$sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum); |
| 281 |
} |
| 282 |
++$columnLetter; |
| 283 |
} |
| 284 |
++$currentRow; |
| 285 |
} |
| 286 |
|
| 287 |
// Close file |
| 288 |
fclose($fileHandle); |
| 289 |
|
| 290 |
if ($this->_contiguous) { |
| 291 |
$this->_contiguousRow = $currentRow; |
| 292 |
} |
| 293 |
|
| 294 |
ini_set('auto_detect_line_endings', $lineEnding); |
| 295 |
|
| 296 |
// Return |
| 297 |
return $objPHPExcel; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Get delimiter |
| 302 |
* |
| 303 |
* @return string |
| 304 |
*/ |
| 305 |
public function getDelimiter() { |
| 306 |
return $this->_delimiter; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Set delimiter |
| 311 |
* |
| 312 |
* @param string $pValue Delimiter, defaults to , |
| 313 |
* @return PHPExcel_Reader_CSV |
| 314 |
*/ |
| 315 |
public function setDelimiter($pValue = ',') { |
| 316 |
$this->_delimiter = $pValue; |
| 317 |
return $this; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get enclosure |
| 322 |
* |
| 323 |
* @return string |
| 324 |
*/ |
| 325 |
public function getEnclosure() { |
| 326 |
return $this->_enclosure; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Set enclosure |
| 331 |
* |
| 332 |
* @param string $pValue Enclosure, defaults to " |
| 333 |
* @return PHPExcel_Reader_CSV |
| 334 |
*/ |
| 335 |
public function setEnclosure($pValue = '"') { |
| 336 |
if ($pValue == '') { |
| 337 |
$pValue = '"'; |
| 338 |
} |
| 339 |
$this->_enclosure = $pValue; |
| 340 |
return $this; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get sheet index |
| 345 |
* |
| 346 |
* @return integer |
| 347 |
*/ |
| 348 |
public function getSheetIndex() { |
| 349 |
return $this->_sheetIndex; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Set sheet index |
| 354 |
* |
| 355 |
* @param integer $pValue Sheet index |
| 356 |
* @return PHPExcel_Reader_CSV |
| 357 |
*/ |
| 358 |
public function setSheetIndex($pValue = 0) { |
| 359 |
$this->_sheetIndex = $pValue; |
| 360 |
return $this; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Set Contiguous |
| 365 |
* |
| 366 |
* @param boolean $contiguous |
| 367 |
*/ |
| 368 |
public function setContiguous($contiguous = FALSE) |
| 369 |
{ |
| 370 |
$this->_contiguous = (bool) $contiguous; |
| 371 |
if (!$contiguous) { |
| 372 |
$this->_contiguousRow = -1; |
| 373 |
} |
| 374 |
|
| 375 |
return $this; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Get Contiguous |
| 380 |
* |
| 381 |
* @return boolean |
| 382 |
*/ |
| 383 |
public function getContiguous() { |
| 384 |
return $this->_contiguous; |
| 385 |
} |
| 386 |
|
| 387 |
} |
| 388 |
|