Documentation

Csv extends BaseWriter
in package

Table of Contents

Properties

$fileHandle  : resource
$includeCharts  : bool
Write charts that are defined in the workbook? Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object.
$preCalculateFormulas  : bool
Pre-calculate formulas Forces PhpSpreadsheet to 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.
$delimiter  : string
Delimiter.
$diskCachingDirectory  : string
Disk caching directory.
$enclosure  : string
Enclosure.
$enclosureRequired  : bool
$excelCompatibility  : bool
Whether to write a fully Excel compatible CSV file.
$includeSeparatorLine  : bool
Whether to write a Separator line as the first line of the file sep=x.
$lineEnding  : string
Line ending.
$outputEncoding  : string
Output encoding.
$preferHyperlinkToLabel  : bool
$sheetIndex  : int
Sheet index to write.
$shouldCloseFile  : bool
$spreadsheet  : Spreadsheet
PhpSpreadsheet object.
$useBOM  : bool
Whether to write a UTF8 BOM.
$useDiskCaching  : bool
Use disk caching where possible?
$variableColumns  : bool
Whether number of columns should be allowed to vary between rows, or use a fixed range based on the max column overall.

Methods

__construct()  : mixed
Create a new CSV.
getDelimiter()  : string
getDiskCachingDirectory()  : string
Get disk caching directory.
getEnclosure()  : string
getEnclosureRequired()  : bool
getExcelCompatibility()  : bool
Get whether the file should be saved with full Excel Compatibility.
getIncludeCharts()  : bool
Write charts in workbook? If this is true, then the Writer will write definitions for any charts that exist in the PhpSpreadsheet object.
getIncludeSeparatorLine()  : bool
Get whether a separator line should be included.
getLineEnding()  : string
getOutputEncoding()  : string
getPreCalculateFormulas()  : bool
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. This is faster for saving in PhpSpreadsheet, but slower when opening the resulting file in MS Excel, because Excel has to recalculate the formulae itself.
getPreferHyperlinkToLabel()  : bool
Get whether hyperlink or label should be output.
getSheetIndex()  : int
getUseBOM()  : bool
Get whether BOM should be used.
getUseDiskCaching()  : bool
Get use disk caching where possible?
getVariableColumns()  : bool
Get whether number of columns should be allowed to vary between rows, or use a fixed range based on the max column overall.
openFileHandle()  : void
Open file handle.
save()  : void
Save PhpSpreadsheet to file.
setDelimiter()  : self
setEnclosure()  : self
setEnclosureRequired()  : self
setExcelCompatibility()  : self
Set whether the file should be saved with full Excel Compatibility.
setIncludeCharts()  : $this
Set write charts in workbook Set to true, to advise the Writer to include any charts that exist in the PhpSpreadsheet object.
setIncludeSeparatorLine()  : self
Set whether a separator line should be included as the first line of the file.
setLineEnding()  : self
setOutputEncoding()  : self
setPreCalculateFormulas()  : $this
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.
setPreferHyperlinkToLabel()  : self
Set whether hyperlink or label should be output.
setSheetIndex()  : self
setUseBOM()  : self
Set whether BOM should be used, typically when non-ASCII characters are used.
setUseDiskCaching()  : $this
Set use disk caching where possible?
setVariableColumns()  : self
Set whether number of columns should be allowed to vary between rows, or use a fixed range based on the max column overall.
maybeCloseFileHandle()  : void
Close file handle only if we opened it ourselves.
processFlags()  : void
elementToString()  : string
Convert boolean to TRUE/FALSE; otherwise return element cast to string.
writeLine()  : void
Write line to CSV file.

Properties

$includeCharts

Write charts that are defined in the workbook? Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object.

protected bool $includeCharts = false

$preCalculateFormulas

Pre-calculate formulas Forces PhpSpreadsheet to 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.

protected bool $preCalculateFormulas = true

$delimiter

Delimiter.

private string $delimiter = ','

$diskCachingDirectory

Disk caching directory.

private string $diskCachingDirectory = './'

$enclosure

Enclosure.

private string $enclosure = '"'

$enclosureRequired

private bool $enclosureRequired = true

$excelCompatibility

Whether to write a fully Excel compatible CSV file.

private bool $excelCompatibility = false

$includeSeparatorLine

Whether to write a Separator line as the first line of the file sep=x.

private bool $includeSeparatorLine = false

$lineEnding

Line ending.

private string $lineEnding = PHP_EOL

$outputEncoding

Output encoding.

private string $outputEncoding = ''

$preferHyperlinkToLabel

private bool $preferHyperlinkToLabel = false

$sheetIndex

Sheet index to write.

private int $sheetIndex = 0

$useBOM

Whether to write a UTF8 BOM.

private bool $useBOM = false

$useDiskCaching

Use disk caching where possible?

private bool $useDiskCaching = false

$variableColumns

Whether number of columns should be allowed to vary between rows, or use a fixed range based on the max column overall.

private bool $variableColumns = false

Methods

__construct()

Create a new CSV.

public __construct(Spreadsheet $spreadsheet) : mixed
Parameters
$spreadsheet : Spreadsheet

The spreadsheet that we want to save using this Writer

getDelimiter()

public getDelimiter() : string
Return values
string

getDiskCachingDirectory()

Get disk caching directory.

public getDiskCachingDirectory() : string
Return values
string

getEnclosure()

public getEnclosure() : string
Return values
string

getEnclosureRequired()

public getEnclosureRequired() : bool
Return values
bool

getExcelCompatibility()

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

public getExcelCompatibility() : bool
Return values
bool

getIncludeCharts()

Write charts in workbook? If this is true, then the Writer will write definitions for any charts that exist in the PhpSpreadsheet object.

public getIncludeCharts() : bool

If false (the default) it will ignore any charts defined in the PhpSpreadsheet object.

Return values
bool

getIncludeSeparatorLine()

Get whether a separator line should be included.

public getIncludeSeparatorLine() : bool
Return values
bool

getLineEnding()

public getLineEnding() : string
Return values
string

getOutputEncoding()

public getOutputEncoding() : string
Return values
string

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. This is faster for saving in PhpSpreadsheet, but slower when opening the resulting file in MS Excel, because Excel has to recalculate the formulae itself.

public getPreCalculateFormulas() : bool
Return values
bool

getPreferHyperlinkToLabel()

Get whether hyperlink or label should be output.

public getPreferHyperlinkToLabel() : bool
Return values
bool

getSheetIndex()

public getSheetIndex() : int
Return values
int

getUseBOM()

Get whether BOM should be used.

public getUseBOM() : bool
Return values
bool

getUseDiskCaching()

Get use disk caching where possible?

public getUseDiskCaching() : bool
Return values
bool

getVariableColumns()

Get whether number of columns should be allowed to vary between rows, or use a fixed range based on the max column overall.

public getVariableColumns() : bool
Return values
bool

openFileHandle()

Open file handle.

public openFileHandle(resource|string $filename) : void
Parameters
$filename : resource|string

save()

Save PhpSpreadsheet to file.

public save(resource|string $filename[, int $flags = 0 ]) : void
Parameters
$filename : resource|string
$flags : int = 0

Flags that can change the behaviour of the Writer: self::SAVE_WITH_CHARTS Save any charts that are defined (if the Writer supports Charts) self::DISABLE_PRECALCULATE_FORMULAE Don't Precalculate formulae before saving the file

setDelimiter()

public setDelimiter(string $delimiter) : self
Parameters
$delimiter : string
Return values
self

setEnclosure()

public setEnclosure([string $enclosure = '"' ]) : self
Parameters
$enclosure : string = '"'
Return values
self

setEnclosureRequired()

public setEnclosureRequired(bool $value) : self
Parameters
$value : bool
Return values
self

setExcelCompatibility()

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

public setExcelCompatibility(bool $excelCompatibility) : self
Parameters
$excelCompatibility : bool

Set the file to be written as a fully Excel compatible csv file Note that this overrides other settings such as useBOM, enclosure and delimiter

Return values
self

setIncludeCharts()

Set write charts in workbook Set to true, to advise the Writer to include any charts that exist in the PhpSpreadsheet object.

public setIncludeCharts(bool $includeCharts) : $this

Set to false (the default) to ignore charts.

Parameters
$includeCharts : bool
Return values
$this

setIncludeSeparatorLine()

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

public setIncludeSeparatorLine(bool $includeSeparatorLine) : self
Parameters
$includeSeparatorLine : bool
Return values
self

setLineEnding()

public setLineEnding(string $lineEnding) : self
Parameters
$lineEnding : string
Return values
self

setOutputEncoding()

public setOutputEncoding(string $outputEnconding) : self
Parameters
$outputEnconding : string
Return values
self

setPreCalculateFormulas()

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.

public setPreCalculateFormulas(bool $precalculateFormulas) : $this
Parameters
$precalculateFormulas : bool

Pre-Calculate Formulas?

Return values
$this

setPreferHyperlinkToLabel()

Set whether hyperlink or label should be output.

public setPreferHyperlinkToLabel(bool $preferHyperlinkToLabel) : self
Parameters
$preferHyperlinkToLabel : bool
Return values
self

setSheetIndex()

public setSheetIndex(int $sheetIndex) : self
Parameters
$sheetIndex : int
Return values
self

setUseBOM()

Set whether BOM should be used, typically when non-ASCII characters are used.

public setUseBOM(bool $useBOM) : self
Parameters
$useBOM : bool
Return values
self

setUseDiskCaching()

Set use disk caching where possible?

public setUseDiskCaching(bool $useDiskCache[, string|null $cacheDirectory = null ]) : $this
Parameters
$useDiskCache : bool
$cacheDirectory : string|null = null

Disk caching directory

Return values
$this

setVariableColumns()

Set whether number of columns should be allowed to vary between rows, or use a fixed range based on the max column overall.

public setVariableColumns(bool $pValue) : self
Parameters
$pValue : bool
Return values
self

maybeCloseFileHandle()

Close file handle only if we opened it ourselves.

protected maybeCloseFileHandle() : void

processFlags()

protected processFlags(int $flags) : void
Parameters
$flags : int

elementToString()

Convert boolean to TRUE/FALSE; otherwise return element cast to string.

private static elementToString(null|bool|float|int|string|Stringable $element) : string
Parameters
$element : null|bool|float|int|string|Stringable

element to be converted

Return values
string

writeLine()

Write line to CSV file.

private writeLine(resource $fileHandle, array<string|int, mixed> $values) : void
Parameters
$fileHandle : resource

PHP filehandle

$values : array<string|int, mixed>

Array containing values in a row


        
On this page

Search results