From b9ae7d1873cd63b6547d6018e7b10419f4a8b7b7 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 17 Mar 2016 23:04:34 +0000 Subject: [PATCH] Allow inclusion of a sep=; line when creating csv files --- src/PhpSpreadsheet/Writer/CSV.php | 45 +++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/CSV.php b/src/PhpSpreadsheet/Writer/CSV.php index 3bcee0ca..5036b43b 100644 --- a/src/PhpSpreadsheet/Writer/CSV.php +++ b/src/PhpSpreadsheet/Writer/CSV.php @@ -71,6 +71,14 @@ class CSV extends BaseWriter implements IWriter */ private $useBOM = false; + /** + * Whether to write a Separator line as the first line of the file + * sep=x + * + * @var boolean + */ + private $includeSeparatorLine = false; + /** * Whether to write a fully Excel compatible CSV file. * @@ -111,15 +119,20 @@ class CSV extends BaseWriter implements IWriter } if ($this->excelCompatibility) { - fwrite($fileHandle, "\xEF\xBB\xBF"); // Enforce UTF-8 BOM Header - $this->setEnclosure('"'); // Set enclosure to " - $this->setDelimiter(";"); // Set delimiter to a semi-colon + $this->setUseBOM(true); // Enforce UTF-8 BOM Header + $this->setIncludeSeparatorLine(true); // Set separator line + $this->setEnclosure('"'); // Set enclosure to " + $this->setDelimiter(";"); // Set delimiter to a semi-colon $this->setLineEnding("\r\n"); - fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding); - } elseif ($this->useBOM) { + } + if ($this->useBOM) { // Write the UTF-8 BOM code if required fwrite($fileHandle, "\xEF\xBB\xBF"); } + if ($this->includeSeparatorLine) { + // Write the separator line if required + fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding); + } // Identify the range that we need to extract from the worksheet $maxCol = $sheet->getHighestDataColumn(); @@ -231,6 +244,28 @@ class CSV extends BaseWriter implements IWriter return $this; } + /** + * Get whether a separator line should be included + * + * @return boolean + */ + public function getIncludeSeparatorLine() + { + return $this->includeSeparatorLine; + } + + /** + * Set whether a separator line should be included as the first line of the file + * + * @param boolean $pValue Use separator line? Defaults to false + * @return PHPExcel_Writer_CSV + */ + public function setIncludeSeparatorLine($pValue = false) + { + $this->includeSeparatorLine = $pValue; + return $this; + } + /** * Get whether the file should be saved with full Excel Compatibility *