[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** PHPExcel root directory */ 4 if (!defined('PHPEXCEL_ROOT')) { 5 /** 6 * @ignore 7 */ 8 define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../'); 9 require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); 10 } 11 12 /** 13 * PHPExcel_Reader_CSV 14 * 15 * Copyright (c) 2006 - 2015 PHPExcel 16 * 17 * This library is free software; you can redistribute it and/or 18 * modify it under the terms of the GNU Lesser General Public 19 * License as published by the Free Software Foundation; either 20 * version 2.1 of the License, or (at your option) any later version. 21 * 22 * This library is distributed in the hope that it will be useful, 23 * but WITHOUT ANY WARRANTY; without even the implied warranty of 24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 * Lesser General Public License for more details. 26 * 27 * You should have received a copy of the GNU Lesser General Public 28 * License along with this library; if not, write to the Free Software 29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 30 * 31 * @category PHPExcel 32 * @package PHPExcel_Reader 33 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel) 34 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL 35 * @version ##VERSION##, ##DATE## 36 */ 37 class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader 38 { 39 /** 40 * Input encoding 41 * 42 * @access private 43 * @var string 44 */ 45 private $inputEncoding = 'UTF-8'; 46 47 /** 48 * Delimiter 49 * 50 * @access private 51 * @var string 52 */ 53 private $delimiter = ','; 54 55 /** 56 * Enclosure 57 * 58 * @access private 59 * @var string 60 */ 61 private $enclosure = '"'; 62 63 /** 64 * Sheet index to read 65 * 66 * @access private 67 * @var int 68 */ 69 private $sheetIndex = 0; 70 71 /** 72 * Load rows contiguously 73 * 74 * @access private 75 * @var int 76 */ 77 private $contiguous = false; 78 79 /** 80 * Row counter for loading rows contiguously 81 * 82 * @var int 83 */ 84 private $contiguousRow = -1; 85 86 87 /** 88 * Create a new PHPExcel_Reader_CSV 89 */ 90 public function __construct() 91 { 92 $this->readFilter = new PHPExcel_Reader_DefaultReadFilter(); 93 } 94 95 /** 96 * Validate that the current file is a CSV file 97 * 98 * @return boolean 99 */ 100 protected function isValidFormat() 101 { 102 return true; 103 } 104 105 /** 106 * Set input encoding 107 * 108 * @param string $pValue Input encoding 109 */ 110 public function setInputEncoding($pValue = 'UTF-8') 111 { 112 $this->inputEncoding = $pValue; 113 return $this; 114 } 115 116 /** 117 * Get input encoding 118 * 119 * @return string 120 */ 121 public function getInputEncoding() 122 { 123 return $this->inputEncoding; 124 } 125 126 /** 127 * Move filepointer past any BOM marker 128 * 129 */ 130 protected function skipBOM() 131 { 132 rewind($this->fileHandle); 133 134 switch ($this->inputEncoding) { 135 case 'UTF-8': 136 fgets($this->fileHandle, 4) == "\xEF\xBB\xBF" ? 137 fseek($this->fileHandle, 3) : fseek($this->fileHandle, 0); 138 break; 139 case 'UTF-16LE': 140 fgets($this->fileHandle, 3) == "\xFF\xFE" ? 141 fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0); 142 break; 143 case 'UTF-16BE': 144 fgets($this->fileHandle, 3) == "\xFE\xFF" ? 145 fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0); 146 break; 147 case 'UTF-32LE': 148 fgets($this->fileHandle, 5) == "\xFF\xFE\x00\x00" ? 149 fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0); 150 break; 151 case 'UTF-32BE': 152 fgets($this->fileHandle, 5) == "\x00\x00\xFE\xFF" ? 153 fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0); 154 break; 155 default: 156 break; 157 } 158 } 159 160 /** 161 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) 162 * 163 * @param string $pFilename 164 * @throws PHPExcel_Reader_Exception 165 */ 166 public function listWorksheetInfo($pFilename) 167 { 168 // Open file 169 $this->openFile($pFilename); 170 if (!$this->isValidFormat()) { 171 fclose($this->fileHandle); 172 throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file."); 173 } 174 $fileHandle = $this->fileHandle; 175 176 // Skip BOM, if any 177 $this->skipBOM(); 178 179 $escapeEnclosures = array( "\\" . $this->enclosure, $this->enclosure . $this->enclosure ); 180 181 $worksheetInfo = array(); 182 $worksheetInfo[0]['worksheetName'] = 'Worksheet'; 183 $worksheetInfo[0]['lastColumnLetter'] = 'A'; 184 $worksheetInfo[0]['lastColumnIndex'] = 0; 185 $worksheetInfo[0]['totalRows'] = 0; 186 $worksheetInfo[0]['totalColumns'] = 0; 187 188 // Loop through each line of the file in turn 189 while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) { 190 $worksheetInfo[0]['totalRows']++; 191 $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1); 192 } 193 194 $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']); 195 $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1; 196 197 // Close file 198 fclose($fileHandle); 199 200 return $worksheetInfo; 201 } 202 203 /** 204 * Loads PHPExcel from file 205 * 206 * @param string $pFilename 207 * @return PHPExcel 208 * @throws PHPExcel_Reader_Exception 209 */ 210 public function load($pFilename) 211 { 212 // Create new PHPExcel 213 $objPHPExcel = new PHPExcel(); 214 215 // Load into this instance 216 return $this->loadIntoExisting($pFilename, $objPHPExcel); 217 } 218 219 /** 220 * Loads PHPExcel from file into PHPExcel instance 221 * 222 * @param string $pFilename 223 * @param PHPExcel $objPHPExcel 224 * @return PHPExcel 225 * @throws PHPExcel_Reader_Exception 226 */ 227 public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) 228 { 229 $lineEnding = ini_get('auto_detect_line_endings'); 230 ini_set('auto_detect_line_endings', true); 231 232 // Open file 233 $this->openFile($pFilename); 234 if (!$this->isValidFormat()) { 235 fclose($this->fileHandle); 236 throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file."); 237 } 238 $fileHandle = $this->fileHandle; 239 240 // Skip BOM, if any 241 $this->skipBOM(); 242 243 // Create new PHPExcel object 244 while ($objPHPExcel->getSheetCount() <= $this->sheetIndex) { 245 $objPHPExcel->createSheet(); 246 } 247 $sheet = $objPHPExcel->setActiveSheetIndex($this->sheetIndex); 248 249 $escapeEnclosures = array( "\\" . $this->enclosure, 250 $this->enclosure . $this->enclosure 251 ); 252 253 // Set our starting row based on whether we're in contiguous mode or not 254 $currentRow = 1; 255 if ($this->contiguous) { 256 $currentRow = ($this->contiguousRow == -1) ? $sheet->getHighestRow(): $this->contiguousRow; 257 } 258 259 // Loop through each line of the file in turn 260 while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) { 261 $columnLetter = 'A'; 262 foreach ($rowData as $rowDatum) { 263 if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) { 264 // Unescape enclosures 265 $rowDatum = str_replace($escapeEnclosures, $this->enclosure, $rowDatum); 266 267 // Convert encoding if necessary 268 if ($this->inputEncoding !== 'UTF-8') { 269 $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->inputEncoding); 270 } 271 272 // Set cell value 273 $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum); 274 } 275 ++$columnLetter; 276 } 277 ++$currentRow; 278 } 279 280 // Close file 281 fclose($fileHandle); 282 283 if ($this->contiguous) { 284 $this->contiguousRow = $currentRow; 285 } 286 287 ini_set('auto_detect_line_endings', $lineEnding); 288 289 // Return 290 return $objPHPExcel; 291 } 292 293 /** 294 * Get delimiter 295 * 296 * @return string 297 */ 298 public function getDelimiter() 299 { 300 return $this->delimiter; 301 } 302 303 /** 304 * Set delimiter 305 * 306 * @param string $pValue Delimiter, defaults to , 307 * @return PHPExcel_Reader_CSV 308 */ 309 public function setDelimiter($pValue = ',') 310 { 311 $this->delimiter = $pValue; 312 return $this; 313 } 314 315 /** 316 * Get enclosure 317 * 318 * @return string 319 */ 320 public function getEnclosure() 321 { 322 return $this->enclosure; 323 } 324 325 /** 326 * Set enclosure 327 * 328 * @param string $pValue Enclosure, defaults to " 329 * @return PHPExcel_Reader_CSV 330 */ 331 public function setEnclosure($pValue = '"') 332 { 333 if ($pValue == '') { 334 $pValue = '"'; 335 } 336 $this->enclosure = $pValue; 337 return $this; 338 } 339 340 /** 341 * Get sheet index 342 * 343 * @return integer 344 */ 345 public function getSheetIndex() 346 { 347 return $this->sheetIndex; 348 } 349 350 /** 351 * Set sheet index 352 * 353 * @param integer $pValue Sheet index 354 * @return PHPExcel_Reader_CSV 355 */ 356 public function setSheetIndex($pValue = 0) 357 { 358 $this->sheetIndex = $pValue; 359 return $this; 360 } 361 362 /** 363 * Set Contiguous 364 * 365 * @param boolean $contiguous 366 */ 367 public function setContiguous($contiguous = false) 368 { 369 $this->contiguous = (bool) $contiguous; 370 if (!$contiguous) { 371 $this->contiguousRow = -1; 372 } 373 374 return $this; 375 } 376 377 /** 378 * Get Contiguous 379 * 380 * @return boolean 381 */ 382 public function getContiguous() 383 { 384 return $this->contiguous; 385 } 386 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |