ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PhpOffice\PhpSpreadsheet\Writer\Csv Class Reference
+ Inheritance diagram for PhpOffice\PhpSpreadsheet\Writer\Csv:
+ Collaboration diagram for PhpOffice\PhpSpreadsheet\Writer\Csv:

Public Member Functions

 __construct (Spreadsheet $spreadsheet)
 Create a new CSV. More...
 
 save ($pFilename)
 Save PhpSpreadsheet to file. More...
 
 getDelimiter ()
 Get delimiter. More...
 
 setDelimiter ($pValue)
 Set delimiter. More...
 
 getEnclosure ()
 Get enclosure. More...
 
 setEnclosure ($pValue='"')
 Set enclosure. More...
 
 getLineEnding ()
 Get line ending. More...
 
 setLineEnding ($pValue)
 Set line ending. More...
 
 getUseBOM ()
 Get whether BOM should be used. More...
 
 setUseBOM ($pValue)
 Set whether BOM should be used. More...
 
 getIncludeSeparatorLine ()
 Get whether a separator line should be included. More...
 
 setIncludeSeparatorLine ($pValue)
 Set whether a separator line should be included as the first line of the file. More...
 
 getExcelCompatibility ()
 Get whether the file should be saved with full Excel Compatibility. More...
 
 setExcelCompatibility ($pValue)
 Set whether the file should be saved with full Excel Compatibility. More...
 
 getSheetIndex ()
 Get sheet index. More...
 
 setSheetIndex ($pValue)
 Set sheet index. More...
 
 getOutputEncoding ()
 Get output encoding. More...
 
 setOutputEncoding ($pValue)
 Set output encoding. More...
 
 setEnclosureRequired (bool $value)
 
 getEnclosureRequired ()
 
- Public Member Functions inherited from PhpOffice\PhpSpreadsheet\Writer\BaseWriter
 getIncludeCharts ()
 Write charts in workbook? If this is true, then the Writer will write definitions for any charts that exist in the PhpSpreadsheet object. More...
 
 setIncludeCharts ($pValue)
 Set write charts in workbook Set to true, to advise the Writer to include any charts that exist in the PhpSpreadsheet object. More...
 
 getPreCalculateFormulas ()
 Get Pre-Calculate Formulas flag If this is true (the default), then the writer will recalculate all formulae in a workbook when saving, so that the pre-calculated values are immediately available to MS Excel or other office spreadsheet viewer when opening the file If false, then formulae are not calculated on save. More...
 
 setPreCalculateFormulas ($pValue)
 Set Pre-Calculate Formulas Set to true (the default) to advise the Writer to calculate all formulae on save Set to false to prevent precalculation of formulae on save. More...
 
 getUseDiskCaching ()
 Get use disk caching where possible? More...
 
 setUseDiskCaching ($pValue, $pDirectory=null)
 Set use disk caching where possible? More...
 
 getDiskCachingDirectory ()
 Get disk caching directory. More...
 
 openFileHandle ($filename)
 Open file handle. More...
 

Private Member Functions

 writeLine ($pFileHandle, array $pValues)
 Write line to CSV file. More...
 

Private Attributes

 $spreadsheet
 
 $delimiter = ','
 
 $enclosure = '"'
 
 $lineEnding = PHP_EOL
 
 $sheetIndex = 0
 
 $useBOM = false
 
 $includeSeparatorLine = false
 
 $excelCompatibility = false
 
 $outputEncoding = ''
 
 $enclosureRequired = true
 

Additional Inherited Members

- Protected Member Functions inherited from PhpOffice\PhpSpreadsheet\Writer\BaseWriter
 maybeCloseFileHandle ()
 Close file handle only if we opened it ourselves. More...
 
- Protected Attributes inherited from PhpOffice\PhpSpreadsheet\Writer\BaseWriter
 $includeCharts = false
 
 $preCalculateFormulas = true
 
 $fileHandle
 

Detailed Description

Definition at line 8 of file Csv.php.

Constructor & Destructor Documentation

◆ __construct()

PhpOffice\PhpSpreadsheet\Writer\Csv::__construct ( Spreadsheet  $spreadsheet)

Create a new CSV.

Parameters
Spreadsheet$spreadsheetSpreadsheet object

Implements PhpOffice\PhpSpreadsheet\Writer\IWriter.

Definition at line 79 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$spreadsheet.

80  {
81  $this->spreadsheet = $spreadsheet;
82  }

Member Function Documentation

◆ getDelimiter()

PhpOffice\PhpSpreadsheet\Writer\Csv::getDelimiter ( )

Get delimiter.

Returns
string

Definition at line 142 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$delimiter.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

143  {
144  return $this->delimiter;
145  }
+ Here is the caller graph for this function:

◆ getEnclosure()

PhpOffice\PhpSpreadsheet\Writer\Csv::getEnclosure ( )

Get enclosure.

Returns
string

Definition at line 166 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$enclosure.

167  {
168  return $this->enclosure;
169  }

◆ getEnclosureRequired()

PhpOffice\PhpSpreadsheet\Writer\Csv::getEnclosureRequired ( )

Definition at line 339 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$enclosureRequired.

339  : bool
340  {
342  }

◆ getExcelCompatibility()

PhpOffice\PhpSpreadsheet\Writer\Csv::getExcelCompatibility ( )

Get whether the file should be saved with full Excel Compatibility.

Returns
bool

Definition at line 262 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$excelCompatibility.

263  {
265  }

◆ getIncludeSeparatorLine()

PhpOffice\PhpSpreadsheet\Writer\Csv::getIncludeSeparatorLine ( )

Get whether a separator line should be included.

Returns
bool

Definition at line 238 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$includeSeparatorLine.

239  {
241  }

◆ getLineEnding()

PhpOffice\PhpSpreadsheet\Writer\Csv::getLineEnding ( )

Get line ending.

Returns
string

Definition at line 190 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$lineEnding.

191  {
192  return $this->lineEnding;
193  }

◆ getOutputEncoding()

PhpOffice\PhpSpreadsheet\Writer\Csv::getOutputEncoding ( )

Get output encoding.

Returns
string

Definition at line 311 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$outputEncoding.

312  {
313  return $this->outputEncoding;
314  }

◆ getSheetIndex()

PhpOffice\PhpSpreadsheet\Writer\Csv::getSheetIndex ( )

Get sheet index.

Returns
int

Definition at line 287 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$sheetIndex.

288  {
289  return $this->sheetIndex;
290  }

◆ getUseBOM()

PhpOffice\PhpSpreadsheet\Writer\Csv::getUseBOM ( )

Get whether BOM should be used.

Returns
bool

Definition at line 214 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$useBOM.

215  {
216  return $this->useBOM;
217  }

◆ save()

PhpOffice\PhpSpreadsheet\Writer\Csv::save (   $pFilename)

Save PhpSpreadsheet to file.

Parameters
resource | string$pFilename

Implements PhpOffice\PhpSpreadsheet\Writer\IWriter.

Definition at line 89 of file Csv.php.

References $row, PhpOffice\PhpSpreadsheet\Calculation\Calculation\getArrayReturnType(), PhpOffice\PhpSpreadsheet\Writer\Csv\getDelimiter(), PhpOffice\PhpSpreadsheet\Calculation\Calculation\getInstance(), PhpOffice\PhpSpreadsheet\Writer\BaseWriter\maybeCloseFileHandle(), PhpOffice\PhpSpreadsheet\Writer\BaseWriter\openFileHandle(), PhpOffice\PhpSpreadsheet\Calculation\Calculation\RETURN_ARRAY_AS_VALUE, PhpOffice\PhpSpreadsheet\Calculation\Calculation\setArrayReturnType(), PhpOffice\PhpSpreadsheet\Writer\Csv\setDelimiter(), PhpOffice\PhpSpreadsheet\Writer\Csv\setEnclosure(), PhpOffice\PhpSpreadsheet\Writer\Csv\setIncludeSeparatorLine(), PhpOffice\PhpSpreadsheet\Writer\Csv\setLineEnding(), PhpOffice\PhpSpreadsheet\Writer\Csv\setUseBOM(), and PhpOffice\PhpSpreadsheet\Writer\Csv\writeLine().

89  : 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  }
setIncludeSeparatorLine($pValue)
Set whether a separator line should be included as the first line of the file.
Definition: Csv.php:250
static getArrayReturnType()
Return the Array Return Type (Array or Value of first element in the array).
setDelimiter($pValue)
Set delimiter.
Definition: Csv.php:154
writeLine($pFileHandle, array $pValues)
Write line to CSV file.
Definition: Csv.php:350
setEnclosure($pValue='"')
Set enclosure.
Definition: Csv.php:178
setUseBOM($pValue)
Set whether BOM should be used.
Definition: Csv.php:226
static setArrayReturnType($returnType)
Set the Array Return Type (Array or Value of first element in the array).
$row
static getInstance(?Spreadsheet $spreadsheet=null)
Get an instance of this class.
setLineEnding($pValue)
Set line ending.
Definition: Csv.php:202
getDelimiter()
Get delimiter.
Definition: Csv.php:142
openFileHandle($filename)
Open file handle.
Definition: BaseWriter.php:102
maybeCloseFileHandle()
Close file handle only if we opened it ourselves.
Definition: BaseWriter.php:123
+ Here is the call graph for this function:

◆ setDelimiter()

PhpOffice\PhpSpreadsheet\Writer\Csv::setDelimiter (   $pValue)

Set delimiter.

Parameters
string$pValueDelimiter, defaults to ','
Returns
$this

Definition at line 154 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

155  {
156  $this->delimiter = $pValue;
157 
158  return $this;
159  }
+ Here is the caller graph for this function:

◆ setEnclosure()

PhpOffice\PhpSpreadsheet\Writer\Csv::setEnclosure (   $pValue = '"')

Set enclosure.

Parameters
string$pValueEnclosure, defaults to "
Returns
$this

Definition at line 178 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

179  {
180  $this->enclosure = $pValue;
181 
182  return $this;
183  }
+ Here is the caller graph for this function:

◆ setEnclosureRequired()

PhpOffice\PhpSpreadsheet\Writer\Csv::setEnclosureRequired ( bool  $value)

Definition at line 332 of file Csv.php.

332  : self
333  {
334  $this->enclosureRequired = $value;
335 
336  return $this;
337  }

◆ setExcelCompatibility()

PhpOffice\PhpSpreadsheet\Writer\Csv::setExcelCompatibility (   $pValue)

Set whether the file should be saved with full Excel Compatibility.

Parameters
bool$pValueSet the file to be written as a fully Excel compatible csv file Note that this overrides other settings such as useBOM, enclosure and delimiter
Returns
$this

Definition at line 275 of file Csv.php.

276  {
277  $this->excelCompatibility = $pValue;
278 
279  return $this;
280  }

◆ setIncludeSeparatorLine()

PhpOffice\PhpSpreadsheet\Writer\Csv::setIncludeSeparatorLine (   $pValue)

Set whether a separator line should be included as the first line of the file.

Parameters
bool$pValueUse separator line? Defaults to false
Returns
$this

Definition at line 250 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

251  {
252  $this->includeSeparatorLine = $pValue;
253 
254  return $this;
255  }
+ Here is the caller graph for this function:

◆ setLineEnding()

PhpOffice\PhpSpreadsheet\Writer\Csv::setLineEnding (   $pValue)

Set line ending.

Parameters
string$pValueLine ending, defaults to OS line ending (PHP_EOL)
Returns
$this

Definition at line 202 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

203  {
204  $this->lineEnding = $pValue;
205 
206  return $this;
207  }
+ Here is the caller graph for this function:

◆ setOutputEncoding()

PhpOffice\PhpSpreadsheet\Writer\Csv::setOutputEncoding (   $pValue)

Set output encoding.

Parameters
string$pValueOutput encoding
Returns
$this

Definition at line 323 of file Csv.php.

324  {
325  $this->outputEncoding = $pValue;
326 
327  return $this;
328  }

◆ setSheetIndex()

PhpOffice\PhpSpreadsheet\Writer\Csv::setSheetIndex (   $pValue)

Set sheet index.

Parameters
int$pValueSheet index
Returns
$this

Definition at line 299 of file Csv.php.

300  {
301  $this->sheetIndex = $pValue;
302 
303  return $this;
304  }

◆ setUseBOM()

PhpOffice\PhpSpreadsheet\Writer\Csv::setUseBOM (   $pValue)

Set whether BOM should be used.

Parameters
bool$pValueUse UTF-8 byte-order mark? Defaults to false
Returns
$this

Definition at line 226 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

227  {
228  $this->useBOM = $pValue;
229 
230  return $this;
231  }
+ Here is the caller graph for this function:

◆ writeLine()

PhpOffice\PhpSpreadsheet\Writer\Csv::writeLine (   $pFileHandle,
array  $pValues 
)
private

Write line to CSV file.

Parameters
resource$pFileHandlePHP filehandle
array$pValuesArray containing values in a row

Definition at line 350 of file Csv.php.

References PhpOffice\PhpSpreadsheet\Writer\Csv\$delimiter, PhpOffice\PhpSpreadsheet\Writer\Csv\$enclosure, and PhpOffice\PhpSpreadsheet\Writer\Csv\$lineEnding.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\save().

350  : 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  }
+ Here is the caller graph for this function:

Field Documentation

◆ $delimiter

PhpOffice\PhpSpreadsheet\Writer\Csv::$delimiter = ','
private

◆ $enclosure

PhpOffice\PhpSpreadsheet\Writer\Csv::$enclosure = '"'
private

◆ $enclosureRequired

PhpOffice\PhpSpreadsheet\Writer\Csv::$enclosureRequired = true
private

Definition at line 330 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\getEnclosureRequired().

◆ $excelCompatibility

PhpOffice\PhpSpreadsheet\Writer\Csv::$excelCompatibility = false
private

Definition at line 65 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\getExcelCompatibility().

◆ $includeSeparatorLine

PhpOffice\PhpSpreadsheet\Writer\Csv::$includeSeparatorLine = false
private

Definition at line 58 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\getIncludeSeparatorLine().

◆ $lineEnding

PhpOffice\PhpSpreadsheet\Writer\Csv::$lineEnding = PHP_EOL
private

◆ $outputEncoding

PhpOffice\PhpSpreadsheet\Writer\Csv::$outputEncoding = ''
private

Definition at line 72 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\getOutputEncoding().

◆ $sheetIndex

PhpOffice\PhpSpreadsheet\Writer\Csv::$sheetIndex = 0
private

Definition at line 43 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\getSheetIndex().

◆ $spreadsheet

PhpOffice\PhpSpreadsheet\Writer\Csv::$spreadsheet
private

Definition at line 15 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\__construct().

◆ $useBOM

PhpOffice\PhpSpreadsheet\Writer\Csv::$useBOM = false
private

Definition at line 50 of file Csv.php.

Referenced by PhpOffice\PhpSpreadsheet\Writer\Csv\getUseBOM().


The documentation for this class was generated from the following file: