ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
CSV.php
Go to the documentation of this file.
1 <?php
30 if (!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 $_lineEnding = PHP_EOL;
78 
85  private $_sheetIndex = 0;
86 
93  private $_contiguous = false;
94 
95 
102  private $_contiguousRow = -1;
103 
110  private $_readFilter = null;
111 
115  public function __construct() {
116  $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
117  } // function __construct()
118 
127  public function canRead($pFilename)
128  {
129  // Check if file exists
130  if (!file_exists($pFilename)) {
131  throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
132  }
133 
134  return true;
135  } // function canRead()
136 
145  public function load($pFilename)
146  {
147  // Create new PHPExcel
148  $objPHPExcel = new PHPExcel();
149 
150  // Load into this instance
151  return $this->loadIntoExisting($pFilename, $objPHPExcel);
152  } // function load()
153 
160  public function getReadFilter() {
161  return $this->_readFilter;
162  } // function getReadFilter()
163 
170  public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
171  $this->_readFilter = $pValue;
172  return $this;
173  } // function setReadFilter()
174 
181  public function setInputEncoding($pValue = 'UTF-8')
182  {
183  $this->_inputEncoding = $pValue;
184  return $this;
185  } // function setInputEncoding()
186 
193  public function getInputEncoding()
194  {
195  return $this->_inputEncoding;
196  } // function getInputEncoding()
197 
207  public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
208  {
209  // Check if file exists
210  if (!file_exists($pFilename)) {
211  throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
212  }
213 
214  // Create new PHPExcel
215  while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
216  $objPHPExcel->createSheet();
217  }
218  $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
219 
220  // Open file
221  $fileHandle = fopen($pFilename, 'r');
222  if ($fileHandle === false) {
223  throw new Exception("Could not open file $pFilename for reading.");
224  }
225 
226  // Skip BOM, if any
227  switch ($this->_inputEncoding) {
228  case 'UTF-8':
229  fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
230  fseek($fileHandle, 3) : fseek($fileHandle, 0);
231  break;
232  case 'UTF-16LE':
233  fgets($fileHandle, 3) == "\xFF\xFE" ?
234  fseek($fileHandle, 2) : fseek($fileHandle, 0);
235  break;
236  case 'UTF-16BE':
237  fgets($fileHandle, 3) == "\xFE\xFF" ?
238  fseek($fileHandle, 2) : fseek($fileHandle, 0);
239  break;
240  case 'UTF-32LE':
241  fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
242  fseek($fileHandle, 4) : fseek($fileHandle, 0);
243  break;
244  case 'UTF-32BE':
245  fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
246  fseek($fileHandle, 4) : fseek($fileHandle, 0);
247  break;
248  default:
249  break;
250  }
251 
252  $escapeEnclosures = array( "\\" . $this->_enclosure,
253  $this->_enclosure . $this->_enclosure
254  );
255 
256  // Set our starting row based on whether we're in contiguous mode or not
257  $currentRow = 1;
258  if ($this->_contiguous) {
259  $currentRow = ($this->_contiguousRow == -1) ? $objPHPExcel->getActiveSheet()->getHighestRow(): $this->_contiguousRow;
260  }
261 
262  // Loop through each line of the file in turn
263  while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
264  $columnLetter = 'A';
265  foreach($rowData as $rowDatum) {
266  if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
267  // Unescape enclosures
268  $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
269 
270  // Convert encoding if necessary
271  if ($this->_inputEncoding !== 'UTF-8') {
272  $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
273  }
274 
275  // Set cell value
276  $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowDatum);
277  }
278  ++$columnLetter;
279  }
280  ++$currentRow;
281  }
282 
283  // Close file
284  fclose($fileHandle);
285 
286  if ($this->_contiguous) {
287  $this->_contiguousRow = $currentRow;
288  }
289 
290  // Return
291  return $objPHPExcel;
292  } // function loadIntoExisting()
293 
300  public function getDelimiter() {
301  return $this->_delimiter;
302  } // function getDelimiter()
303 
311  public function setDelimiter($pValue = ',') {
312  $this->_delimiter = $pValue;
313  return $this;
314  } // function setDelimiter()
315 
322  public function getEnclosure() {
323  return $this->_enclosure;
324  } // function getEnclosure()
325 
333  public function setEnclosure($pValue = '"') {
334  if ($pValue == '') {
335  $pValue = '"';
336  }
337  $this->_enclosure = $pValue;
338  return $this;
339  } // function setEnclosure()
340 
347  public function getLineEnding() {
348  return $this->_lineEnding;
349  } // function getLineEnding()
350 
358  public function setLineEnding($pValue = PHP_EOL) {
359  $this->_lineEnding = $pValue;
360  return $this;
361  } // function setLineEnding()
362 
369  public function getSheetIndex() {
370  return $this->_sheetIndex;
371  } // function getSheetIndex()
372 
380  public function setSheetIndex($pValue = 0) {
381  $this->_sheetIndex = $pValue;
382  return $this;
383  } // function setSheetIndex()
384 
391  public function setContiguous($contiguous = false)
392  {
393  $this->_contiguous = (bool)$contiguous;
394  if (!$contiguous) {
395  $this->_contiguousRow = -1;
396  }
397 
398  return $this;
399  } // function setInputEncoding()
400 
407  public function getContiguous() {
408  return $this->_contiguous;
409  } // function getSheetIndex()
410 
411 }