updated a couple packages (#1567)
[openemr.git] / vendor / phpoffice / phpspreadsheet / src / PhpSpreadsheet / Writer / Csv.php
blobae38ab7321862fc751b720c3572250505ebbfd54
1 <?php
3 namespace PhpOffice\PhpSpreadsheet\Writer;
5 use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6 use PhpOffice\PhpSpreadsheet\Spreadsheet;
8 class Csv extends BaseWriter
10 /**
11 * PhpSpreadsheet object.
13 * @var Spreadsheet
15 private $spreadsheet;
17 /**
18 * Delimiter.
20 * @var string
22 private $delimiter = ',';
24 /**
25 * Enclosure.
27 * @var string
29 private $enclosure = '"';
31 /**
32 * Line ending.
34 * @var string
36 private $lineEnding = PHP_EOL;
38 /**
39 * Sheet index to write.
41 * @var int
43 private $sheetIndex = 0;
45 /**
46 * Whether to write a BOM (for UTF8).
48 * @var bool
50 private $useBOM = false;
52 /**
53 * Whether to write a Separator line as the first line of the file
54 * sep=x.
56 * @var bool
58 private $includeSeparatorLine = false;
60 /**
61 * Whether to write a fully Excel compatible CSV file.
63 * @var bool
65 private $excelCompatibility = false;
67 /**
68 * Create a new CSV.
70 * @param Spreadsheet $spreadsheet Spreadsheet object
72 public function __construct(Spreadsheet $spreadsheet)
74 $this->spreadsheet = $spreadsheet;
77 /**
78 * Save PhpSpreadsheet to file.
80 * @param string $pFilename
82 * @throws Exception
84 public function save($pFilename)
86 // Fetch sheet
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);
94 // Open file
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");
107 if ($this->useBOM) {
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]);
128 // Close file
129 fclose($fileHandle);
131 Calculation::setArrayReturnType($saveArrayReturnType);
132 Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
136 * Get delimiter.
138 * @return string
140 public function getDelimiter()
142 return $this->delimiter;
146 * Set delimiter.
148 * @param string $pValue Delimiter, defaults to ','
150 * @return CSV
152 public function setDelimiter($pValue)
154 $this->delimiter = $pValue;
156 return $this;
160 * Get enclosure.
162 * @return string
164 public function getEnclosure()
166 return $this->enclosure;
170 * Set enclosure.
172 * @param string $pValue Enclosure, defaults to "
174 * @return CSV
176 public function setEnclosure($pValue)
178 if ($pValue == '') {
179 $pValue = null;
181 $this->enclosure = $pValue;
183 return $this;
187 * Get line ending.
189 * @return string
191 public function getLineEnding()
193 return $this->lineEnding;
197 * Set line ending.
199 * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
201 * @return CSV
203 public function setLineEnding($pValue)
205 $this->lineEnding = $pValue;
207 return $this;
211 * Get whether BOM should be used.
213 * @return bool
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
225 * @return CSV
227 public function setUseBOM($pValue)
229 $this->useBOM = $pValue;
231 return $this;
235 * Get whether a separator line should be included.
237 * @return bool
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
249 * @return CSV
251 public function setIncludeSeparatorLine($pValue)
253 $this->includeSeparatorLine = $pValue;
255 return $this;
259 * Get whether the file should be saved with full Excel Compatibility.
261 * @return bool
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
274 * @return CSV
276 public function setExcelCompatibility($pValue)
278 $this->excelCompatibility = $pValue;
280 return $this;
284 * Get sheet index.
286 * @return int
288 public function getSheetIndex()
290 return $this->sheetIndex;
294 * Set sheet index.
296 * @param int $pValue Sheet index
298 * @return CSV
300 public function setSheetIndex($pValue)
302 $this->sheetIndex = $pValue;
304 return $this;
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;
318 // Build the line
319 $line = '';
321 foreach ($pValues as $element) {
322 // Escape enclosures
323 $element = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $element);
325 // Add delimiter
326 if ($writeDelimiter) {
327 $line .= $this->delimiter;
328 } else {
329 $writeDelimiter = true;
332 // Add enclosed string
333 $line .= $this->enclosure . $element . $this->enclosure;
336 // Add line ending
337 $line .= $this->lineEnding;
339 // Write to file
340 fwrite($pFileHandle, $line);