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/Writer/IWriter.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/RichText.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
48 
49 
63  private $_phpExcel;
64 
70  private $_delimiter;
71 
77  private $_enclosure;
78 
84  private $_lineEnding;
85 
91  private $_sheetIndex;
92 
98  private $_preCalculateFormulas = true;
99 
105  private $_useBOM = false;
106 
112  public function __construct(PHPExcel $phpExcel) {
113  $this->_phpExcel = $phpExcel;
114  $this->_delimiter = ',';
115  $this->_enclosure = '"';
116  $this->_lineEnding = PHP_EOL;
117  $this->_sheetIndex = 0;
118  }
119 
126  public function save($pFilename = null) {
127  // Fetch sheet
128  $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
129 
130  $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
132 
133  // Open file
134  $fileHandle = fopen($pFilename, 'w');
135  if ($fileHandle === false) {
136  throw new Exception("Could not open file $pFilename for writing.");
137  }
138 
139  if ($this->_useBOM) {
140  // Write the UTF-8 BOM code
141  fwrite($fileHandle, "\xEF\xBB\xBF");
142  }
143 
144  // Convert sheet to array
145  $cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
146 
147  // Write rows to file
148  foreach ($cellsArray as $row) {
149  $this->_writeLine($fileHandle, $row);
150  }
151 
152  // Close file
153  fclose($fileHandle);
154 
155  PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
156  }
157 
163  public function getDelimiter() {
164  return $this->_delimiter;
165  }
166 
173  public function setDelimiter($pValue = ',') {
174  $this->_delimiter = $pValue;
175  return $this;
176  }
177 
183  public function getEnclosure() {
184  return $this->_enclosure;
185  }
186 
193  public function setEnclosure($pValue = '"') {
194  if ($pValue == '') {
195  $pValue = null;
196  }
197  $this->_enclosure = $pValue;
198  return $this;
199  }
200 
206  public function getLineEnding() {
207  return $this->_lineEnding;
208  }
209 
216  public function setLineEnding($pValue = PHP_EOL) {
217  $this->_lineEnding = $pValue;
218  return $this;
219  }
220 
226  public function getUseBOM() {
227  return $this->_useBOM;
228  }
229 
236  public function setUseBOM($pValue = false) {
237  $this->_useBOM = $pValue;
238  return $this;
239  }
240 
246  public function getSheetIndex() {
247  return $this->_sheetIndex;
248  }
249 
256  public function setSheetIndex($pValue = 0) {
257  $this->_sheetIndex = $pValue;
258  return $this;
259  }
260 
268  private function _writeLine($pFileHandle = null, $pValues = null) {
269  if (!is_null($pFileHandle) && is_array($pValues)) {
270  // No leading delimiter
271  $writeDelimiter = false;
272 
273  // Build the line
274  $line = '';
275 
276  foreach ($pValues as $element) {
277  // Escape enclosures
278  $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
279 
280  // Add delimiter
281  if ($writeDelimiter) {
282  $line .= $this->_delimiter;
283  } else {
284  $writeDelimiter = true;
285  }
286 
287  // Add enclosed string
288  $line .= $this->_enclosure . $element . $this->_enclosure;
289  }
290 
291  // Add line ending
292  $line .= $this->_lineEnding;
293 
294  // Write to file
295  fwrite($pFileHandle, $line);
296  } else {
297  throw new Exception("Invalid parameters passed.");
298  }
299  }
300 
306  public function getPreCalculateFormulas() {
308  }
309 
316  public function setPreCalculateFormulas($pValue = true) {
317  $this->_preCalculateFormulas = $pValue;
318  return $this;
319  }
320 }