3 namespace PhpOffice\PhpSpreadsheet\Writer
;
5 use PhpOffice\PhpSpreadsheet\Calculation\Calculation
;
6 use PhpOffice\PhpSpreadsheet\Spreadsheet
;
8 class Csv
extends BaseWriter
11 * PhpSpreadsheet object.
22 private $delimiter = ',';
29 private $enclosure = '"';
36 private $lineEnding = PHP_EOL
;
39 * Sheet index to write.
43 private $sheetIndex = 0;
46 * Whether to write a BOM (for UTF8).
50 private $useBOM = false;
53 * Whether to write a Separator line as the first line of the file
58 private $includeSeparatorLine = false;
61 * Whether to write a fully Excel compatible CSV file.
65 private $excelCompatibility = false;
70 * @param Spreadsheet $spreadsheet Spreadsheet object
72 public function __construct(Spreadsheet
$spreadsheet)
74 $this->spreadsheet
= $spreadsheet;
78 * Save PhpSpreadsheet to file.
80 * @param string $pFilename
84 public function save($pFilename)
87 $sheet = $this->spreadsheet
->getSheet($this->sheetIndex
);
89 $saveDebugLog = Calculation
::getInstance($this->spreadsheet
)->getDebugLog()->getWriteDebugLog();
90 Calculation
::getInstance($this->spreadsheet
)->getDebugLog()->setWriteDebugLog(false);
91 $saveArrayReturnType = Calculation
::getArrayReturnType();
92 Calculation
::setArrayReturnType(Calculation
::RETURN_ARRAY_AS_VALUE
);
95 $fileHandle = fopen($pFilename, 'wb+');
96 if ($fileHandle === false) {
97 throw new Exception("Could not open file $pFilename for writing.");
100 if ($this->excelCompatibility
) {
101 $this->setUseBOM(true); // Enforce UTF-8 BOM Header
102 $this->setIncludeSeparatorLine(true); // Set separator line
103 $this->setEnclosure('"'); // Set enclosure to "
104 $this->setDelimiter(';'); // Set delimiter to a semi-colon
105 $this->setLineEnding("\r\n");
108 // Write the UTF-8 BOM code if required
109 fwrite($fileHandle, "\xEF\xBB\xBF");
111 if ($this->includeSeparatorLine
) {
112 // Write the separator line if required
113 fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding
);
116 // Identify the range that we need to extract from the worksheet
117 $maxCol = $sheet->getHighestDataColumn();
118 $maxRow = $sheet->getHighestDataRow();
120 // Write rows to file
121 for ($row = 1; $row <= $maxRow; ++
$row) {
122 // Convert the row to an array...
123 $cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas
);
124 // ... and write to the file
125 $this->writeLine($fileHandle, $cellsArray[0]);
131 Calculation
::setArrayReturnType($saveArrayReturnType);
132 Calculation
::getInstance($this->spreadsheet
)->getDebugLog()->setWriteDebugLog($saveDebugLog);
140 public function getDelimiter()
142 return $this->delimiter
;
148 * @param string $pValue Delimiter, defaults to ','
152 public function setDelimiter($pValue)
154 $this->delimiter
= $pValue;
164 public function getEnclosure()
166 return $this->enclosure
;
172 * @param string $pValue Enclosure, defaults to "
176 public function setEnclosure($pValue)
181 $this->enclosure
= $pValue;
191 public function getLineEnding()
193 return $this->lineEnding
;
199 * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
203 public function setLineEnding($pValue)
205 $this->lineEnding
= $pValue;
211 * Get whether BOM should be used.
215 public function getUseBOM()
217 return $this->useBOM
;
221 * Set whether BOM should be used.
223 * @param bool $pValue Use UTF-8 byte-order mark? Defaults to false
227 public function setUseBOM($pValue)
229 $this->useBOM
= $pValue;
235 * Get whether a separator line should be included.
239 public function getIncludeSeparatorLine()
241 return $this->includeSeparatorLine
;
245 * Set whether a separator line should be included as the first line of the file.
247 * @param bool $pValue Use separator line? Defaults to false
251 public function setIncludeSeparatorLine($pValue)
253 $this->includeSeparatorLine
= $pValue;
259 * Get whether the file should be saved with full Excel Compatibility.
263 public function getExcelCompatibility()
265 return $this->excelCompatibility
;
269 * Set whether the file should be saved with full Excel Compatibility.
271 * @param bool $pValue Set the file to be written as a fully Excel compatible csv file
272 * Note that this overrides other settings such as useBOM, enclosure and delimiter
276 public function setExcelCompatibility($pValue)
278 $this->excelCompatibility
= $pValue;
288 public function getSheetIndex()
290 return $this->sheetIndex
;
296 * @param int $pValue Sheet index
300 public function setSheetIndex($pValue)
302 $this->sheetIndex
= $pValue;
308 * Write line to CSV file.
310 * @param resource $pFileHandle PHP filehandle
311 * @param array $pValues Array containing values in a row
313 private function writeLine($pFileHandle, array $pValues)
315 // No leading delimiter
316 $writeDelimiter = false;
321 foreach ($pValues as $element) {
323 $element = str_replace($this->enclosure
, $this->enclosure
. $this->enclosure
, $element);
326 if ($writeDelimiter) {
327 $line .= $this->delimiter
;
329 $writeDelimiter = true;
332 // Add enclosed string
333 $line .= $this->enclosure
. $element . $this->enclosure
;
337 $line .= $this->lineEnding
;
340 fwrite($pFileHandle, $line);