ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Csv.php
Go to the documentation of this file.
1<?php
2
4
7
8class Csv extends BaseWriter
9{
15 private $spreadsheet;
16
22 private $delimiter = ',';
23
29 private $enclosure = '"';
30
37
43 private $sheetIndex = 0;
44
50 private $useBOM = false;
51
58 private $includeSeparatorLine = false;
59
65 private $excelCompatibility = false;
66
72 private $outputEncoding = '';
73
80 {
81 $this->spreadsheet = $spreadsheet;
82 }
83
89 public function save($pFilename): void
90 {
91 // Fetch sheet
92 $sheet = $this->spreadsheet->getSheet($this->sheetIndex);
93
94 $saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog();
95 Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false);
96 $saveArrayReturnType = Calculation::getArrayReturnType();
98
99 // Open file
100 $this->openFileHandle($pFilename);
101
102 if ($this->excelCompatibility) {
103 $this->setUseBOM(true); // Enforce UTF-8 BOM Header
104 $this->setIncludeSeparatorLine(true); // Set separator line
105 $this->setEnclosure('"'); // Set enclosure to "
106 $this->setDelimiter(';'); // Set delimiter to a semi-colon
107 $this->setLineEnding("\r\n");
108 }
109
110 if ($this->useBOM) {
111 // Write the UTF-8 BOM code if required
112 fwrite($this->fileHandle, "\xEF\xBB\xBF");
113 }
114
115 if ($this->includeSeparatorLine) {
116 // Write the separator line if required
117 fwrite($this->fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
118 }
119
120 // Identify the range that we need to extract from the worksheet
121 $maxCol = $sheet->getHighestDataColumn();
122 $maxRow = $sheet->getHighestDataRow();
123
124 // Write rows to file
125 for ($row = 1; $row <= $maxRow; ++$row) {
126 // Convert the row to an array...
127 $cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas);
128 // ... and write to the file
129 $this->writeLine($this->fileHandle, $cellsArray[0]);
130 }
131
132 $this->maybeCloseFileHandle();
133 Calculation::setArrayReturnType($saveArrayReturnType);
134 Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
135 }
136
142 public function getDelimiter()
143 {
144 return $this->delimiter;
145 }
146
154 public function setDelimiter($pValue)
155 {
156 $this->delimiter = $pValue;
157
158 return $this;
159 }
160
166 public function getEnclosure()
167 {
168 return $this->enclosure;
169 }
170
178 public function setEnclosure($pValue = '"')
179 {
180 $this->enclosure = $pValue;
181
182 return $this;
183 }
184
190 public function getLineEnding()
191 {
192 return $this->lineEnding;
193 }
194
202 public function setLineEnding($pValue)
203 {
204 $this->lineEnding = $pValue;
205
206 return $this;
207 }
208
214 public function getUseBOM()
215 {
216 return $this->useBOM;
217 }
218
226 public function setUseBOM($pValue)
227 {
228 $this->useBOM = $pValue;
229
230 return $this;
231 }
232
238 public function getIncludeSeparatorLine()
239 {
241 }
242
250 public function setIncludeSeparatorLine($pValue)
251 {
252 $this->includeSeparatorLine = $pValue;
253
254 return $this;
255 }
256
262 public function getExcelCompatibility()
263 {
265 }
266
275 public function setExcelCompatibility($pValue)
276 {
277 $this->excelCompatibility = $pValue;
278
279 return $this;
280 }
281
287 public function getSheetIndex()
288 {
289 return $this->sheetIndex;
290 }
291
299 public function setSheetIndex($pValue)
300 {
301 $this->sheetIndex = $pValue;
302
303 return $this;
304 }
305
311 public function getOutputEncoding()
312 {
314 }
315
323 public function setOutputEncoding($pValue)
324 {
325 $this->outputEncoding = $pValue;
326
327 return $this;
328 }
329
330 private $enclosureRequired = true;
331
332 public function setEnclosureRequired(bool $value): self
333 {
334 $this->enclosureRequired = $value;
335
336 return $this;
337 }
338
339 public function getEnclosureRequired(): bool
340 {
342 }
343
350 private function writeLine($pFileHandle, array $pValues): void
351 {
352 // No leading delimiter
353 $delimiter = '';
354
355 // Build the line
356 $line = '';
357
358 foreach ($pValues as $element) {
359 // Add delimiter
360 $line .= $delimiter;
362 // Escape enclosures
364 if ($enclosure) {
365 // If enclosure is not required, use enclosure only if
366 // element contains newline, delimiter, or enclosure.
367 if (!$this->enclosureRequired && strpbrk($element, "$delimiter$enclosure\n") === false) {
368 $enclosure = '';
369 } else {
370 $element = str_replace($enclosure, $enclosure . $enclosure, $element);
371 }
372 }
373 // Add enclosed string
374 $line .= $enclosure . $element . $enclosure;
375 }
376
377 // Add line ending
378 $line .= $this->lineEnding;
379
380 // Write to file
381 if ($this->outputEncoding != '') {
382 $line = mb_convert_encoding($line, $this->outputEncoding);
383 }
384 fwrite($pFileHandle, $line);
385 }
386}
An exception for terminatinating execution or to throw for unit testing.
static setArrayReturnType($returnType)
Set the Array Return Type (Array or Value of first element in the array).
static getArrayReturnType()
Return the Array Return Type (Array or Value of first element in the array).
static getInstance(?Spreadsheet $spreadsheet=null)
Get an instance of this class.
openFileHandle($filename)
Open file handle.
Definition: BaseWriter.php:102
maybeCloseFileHandle()
Close file handle only if we opened it ourselves.
Definition: BaseWriter.php:123
getDelimiter()
Get delimiter.
Definition: Csv.php:142
setExcelCompatibility($pValue)
Set whether the file should be saved with full Excel Compatibility.
Definition: Csv.php:275
writeLine($pFileHandle, array $pValues)
Write line to CSV file.
Definition: Csv.php:350
setOutputEncoding($pValue)
Set output encoding.
Definition: Csv.php:323
__construct(Spreadsheet $spreadsheet)
Create a new CSV.
Definition: Csv.php:79
setSheetIndex($pValue)
Set sheet index.
Definition: Csv.php:299
getOutputEncoding()
Get output encoding.
Definition: Csv.php:311
save($pFilename)
Save PhpSpreadsheet to file.
Definition: Csv.php:89
getEnclosure()
Get enclosure.
Definition: Csv.php:166
setUseBOM($pValue)
Set whether BOM should be used.
Definition: Csv.php:226
setIncludeSeparatorLine($pValue)
Set whether a separator line should be included as the first line of the file.
Definition: Csv.php:250
setEnclosureRequired(bool $value)
Definition: Csv.php:332
getIncludeSeparatorLine()
Get whether a separator line should be included.
Definition: Csv.php:238
setLineEnding($pValue)
Set line ending.
Definition: Csv.php:202
getLineEnding()
Get line ending.
Definition: Csv.php:190
getExcelCompatibility()
Get whether the file should be saved with full Excel Compatibility.
Definition: Csv.php:262
setDelimiter($pValue)
Set delimiter.
Definition: Csv.php:154
getUseBOM()
Get whether BOM should be used.
Definition: Csv.php:214
setEnclosure($pValue='"')
Set enclosure.
Definition: Csv.php:178
getSheetIndex()
Get sheet index.
Definition: Csv.php:287
PHP_EOL
Definition: complexTest.php:7
$row