ILIAS  Release_4_0_x_branch Revision 61816
 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 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/IReader.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/DefaultReadFilter.php';
51 
52 
61 {
67  private $_delimiter;
68 
74  private $_enclosure;
75 
81  private $_lineEnding;
82 
88  private $_sheetIndex;
89 
95  private $_readFilter = null;
96 
100  public function __construct() {
101  $this->_delimiter = ',';
102  $this->_enclosure = '"';
103  $this->_lineEnding = PHP_EOL;
104  $this->_sheetIndex = 0;
105  $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
106  }
107 
114  public function canRead($pFilename)
115  {
116  // Check if file exists
117  if (!file_exists($pFilename)) {
118  throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
119  }
120 
121  // Check if it is a CSV file (using file name)
122  return (substr(strtolower($pFilename), -3) == 'csv');
123  }
124 
131  public function load($pFilename)
132  {
133  // Create new PHPExcel
134  $objPHPExcel = new PHPExcel();
135 
136  // Load into this instance
137  return $this->loadIntoExisting($pFilename, $objPHPExcel);
138  }
139 
145  public function getReadFilter() {
146  return $this->_readFilter;
147  }
148 
154  public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
155  $this->_readFilter = $pValue;
156  }
157 
165  public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
166  {
167  // Check if file exists
168  if (!file_exists($pFilename)) {
169  throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
170  }
171 
172  // Create new PHPExcel
173  while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
174  $objPHPExcel->createSheet();
175  }
176  $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
177 
178  // Open file
179  $fileHandle = fopen($pFilename, 'r');
180  if ($fileHandle === false) {
181  throw new Exception("Could not open file $pFilename for reading.");
182  }
183 
184  // Loop trough file
185  $currentRow = 0;
186  $rowData = array();
187  while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
188  ++$currentRow;
189  $rowDataCount = count($rowData);
190  for ($i = 0; $i < $rowDataCount; ++$i) {
191  $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
192  if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
193  // Unescape enclosures
194  $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]);
195  $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]);
196 
197  // Set cell value
198  $objPHPExcel->getActiveSheet()->setCellValue(
199  $columnLetter . $currentRow, $rowData[$i]
200  );
201  }
202  }
203  }
204 
205  // Close file
206  fclose($fileHandle);
207 
208  // Return
209  return $objPHPExcel;
210  }
211 
217  public function getDelimiter() {
218  return $this->_delimiter;
219  }
220 
227  public function setDelimiter($pValue = ',') {
228  $this->_delimiter = $pValue;
229  return $this;
230  }
231 
237  public function getEnclosure() {
238  return $this->_enclosure;
239  }
240 
247  public function setEnclosure($pValue = '"') {
248  if ($pValue == '') {
249  $pValue = '"';
250  }
251  $this->_enclosure = $pValue;
252  return $this;
253  }
254 
260  public function getLineEnding() {
261  return $this->_lineEnding;
262  }
263 
270  public function setLineEnding($pValue = PHP_EOL) {
271  $this->_lineEnding = $pValue;
272  return $this;
273  }
274 
280  public function getSheetIndex() {
281  return $this->_sheetIndex;
282  }
283 
290  public function setSheetIndex($pValue = 0) {
291  $this->_sheetIndex = $pValue;
292  return $this;
293  }
294 }