ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
CSV.php
Go to the documentation of this file.
1 <?php
42  private $_phpExcel;
43 
49  private $_delimiter = ',';
50 
56  private $_enclosure = '"';
57 
63  private $_lineEnding = PHP_EOL;
64 
70  private $_sheetIndex = 0;
71 
77  private $_preCalculateFormulas = true;
78 
84  private $_useBOM = false;
85 
91  public function __construct(PHPExcel $phpExcel) {
92  $this->_phpExcel = $phpExcel;
93  }
94 
101  public function save($pFilename = null) {
102  // Fetch sheet
103  $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
104 
105  $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
106  PHPExcel_Calculation::getInstance()->writeDebugLog = false;
107  $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
109 
110  // Open file
111  $fileHandle = fopen($pFilename, 'wb+');
112  if ($fileHandle === false) {
113  throw new Exception("Could not open file $pFilename for writing.");
114  }
115 
116  if ($this->_useBOM) {
117  // Write the UTF-8 BOM code
118  fwrite($fileHandle, "\xEF\xBB\xBF");
119  }
120 
121  // Convert sheet to array
122  $cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
123 
124  // Write rows to file
125  foreach ($cellsArray as $row) {
126  $this->_writeLine($fileHandle, $row);
127  }
128 
129  // Close file
130  fclose($fileHandle);
131 
132  PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
133  PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
134  }
135 
141  public function getDelimiter() {
142  return $this->_delimiter;
143  }
144 
151  public function setDelimiter($pValue = ',') {
152  $this->_delimiter = $pValue;
153  return $this;
154  }
155 
161  public function getEnclosure() {
162  return $this->_enclosure;
163  }
164 
171  public function setEnclosure($pValue = '"') {
172  if ($pValue == '') {
173  $pValue = null;
174  }
175  $this->_enclosure = $pValue;
176  return $this;
177  }
178 
184  public function getLineEnding() {
185  return $this->_lineEnding;
186  }
187 
194  public function setLineEnding($pValue = PHP_EOL) {
195  $this->_lineEnding = $pValue;
196  return $this;
197  }
198 
204  public function getUseBOM() {
205  return $this->_useBOM;
206  }
207 
214  public function setUseBOM($pValue = false) {
215  $this->_useBOM = $pValue;
216  return $this;
217  }
218 
224  public function getSheetIndex() {
225  return $this->_sheetIndex;
226  }
227 
234  public function setSheetIndex($pValue = 0) {
235  $this->_sheetIndex = $pValue;
236  return $this;
237  }
238 
246  private function _writeLine($pFileHandle = null, $pValues = null) {
247  if (is_array($pValues)) {
248  // No leading delimiter
249  $writeDelimiter = false;
250 
251  // Build the line
252  $line = '';
253 
254  foreach ($pValues as $element) {
255  // Escape enclosures
256  $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
257 
258  // Add delimiter
259  if ($writeDelimiter) {
260  $line .= $this->_delimiter;
261  } else {
262  $writeDelimiter = true;
263  }
264 
265  // Add enclosed string
266  $line .= $this->_enclosure . $element . $this->_enclosure;
267  }
268 
269  // Add line ending
270  $line .= $this->_lineEnding;
271 
272  // Write to file
273  fwrite($pFileHandle, $line);
274  } else {
275  throw new Exception("Invalid parameters passed.");
276  }
277  }
278 
284  public function getPreCalculateFormulas() {
286  }
287 
294  public function setPreCalculateFormulas($pValue = true) {
295  $this->_preCalculateFormulas = $pValue;
296  return $this;
297  }
298 }