ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
CSV.php
Go to the documentation of this file.
1<?php
30if (!defined('PHPEXCEL_ROOT')) {
34 define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35 require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36}
37
46{
53 private $_inputEncoding = 'UTF-8';
54
61 private $_delimiter = ',';
62
69 private $_enclosure = '"';
70
77 private $_sheetIndex = 0;
78
85 private $_contiguous = false;
86
92 private $_contiguousRow = -1;
93
94
98 public function __construct() {
99 $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
100 }
101
107 protected function _isValidFormat()
108 {
109 return TRUE;
110 }
111
117 public function setInputEncoding($pValue = 'UTF-8')
118 {
119 $this->_inputEncoding = $pValue;
120 return $this;
121 }
122
128 public function getInputEncoding()
129 {
131 }
132
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
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
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
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
305 public function getDelimiter() {
306 return $this->_delimiter;
307 }
308
315 public function setDelimiter($pValue = ',') {
316 $this->_delimiter = $pValue;
317 return $this;
318 }
319
325 public function getEnclosure() {
326 return $this->_enclosure;
327 }
328
335 public function setEnclosure($pValue = '"') {
336 if ($pValue == '') {
337 $pValue = '"';
338 }
339 $this->_enclosure = $pValue;
340 return $this;
341 }
342
348 public function getSheetIndex() {
349 return $this->_sheetIndex;
350 }
351
358 public function setSheetIndex($pValue = 0) {
359 $this->_sheetIndex = $pValue;
360 return $this;
361 }
362
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
383 public function getContiguous() {
384 return $this->_contiguous;
385 }
386
387}
$objPHPExcel
An exception for terminatinating execution or to throw for unit testing.
static stringFromColumnIndex($pColumnIndex=0)
String from columnindex.
Definition: Cell.php:825
_openFile($pFilename)
Open file for reading.
Definition: Abstract.php:195
loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
Loads PHPExcel from file into PHPExcel instance.
Definition: CSV.php:234
getEnclosure()
Get enclosure.
Definition: CSV.php:325
setDelimiter($pValue=',')
Set delimiter.
Definition: CSV.php:315
__construct()
Create a new PHPExcel_Reader_CSV.
Definition: CSV.php:98
_isValidFormat()
Validate that the current file is a CSV file.
Definition: CSV.php:107
getContiguous()
Get Contiguous.
Definition: CSV.php:383
load($pFilename)
Loads PHPExcel from file.
Definition: CSV.php:217
getInputEncoding()
Get input encoding.
Definition: CSV.php:128
setSheetIndex($pValue=0)
Set sheet index.
Definition: CSV.php:358
setInputEncoding($pValue='UTF-8')
Set input encoding.
Definition: CSV.php:117
setContiguous($contiguous=FALSE)
Set Contiguous.
Definition: CSV.php:368
listWorksheetInfo($pFilename)
Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
Definition: CSV.php:173
getSheetIndex()
Get sheet index.
Definition: CSV.php:348
_skipBOM()
Move filepointer past any BOM marker.
Definition: CSV.php:137
setEnclosure($pValue='"')
Set enclosure.
Definition: CSV.php:335
getDelimiter()
Get delimiter.
Definition: CSV.php:305
static ConvertEncoding($value, $to, $from)
Convert string from one encoding to another.
Definition: String.php:493
getSheetCount()
Get sheet count.
Definition: PHPExcel.php:661
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27