| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Writer;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
8 |
use Stringable;
|
|
|
9 |
|
|
|
10 |
class Csv extends BaseWriter
|
|
|
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* PhpSpreadsheet object.
|
|
|
14 |
*/
|
|
|
15 |
private Spreadsheet $spreadsheet;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Delimiter.
|
|
|
19 |
*/
|
|
|
20 |
private string $delimiter = ',';
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Enclosure.
|
|
|
24 |
*/
|
|
|
25 |
private string $enclosure = '"';
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Line ending.
|
|
|
29 |
*/
|
|
|
30 |
private string $lineEnding = PHP_EOL;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Sheet index to write.
|
|
|
34 |
*/
|
|
|
35 |
private int $sheetIndex = 0;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Whether to write a UTF8 BOM.
|
|
|
39 |
*/
|
|
|
40 |
private bool $useBOM = false;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Whether to write a Separator line as the first line of the file
|
|
|
44 |
* sep=x.
|
|
|
45 |
*/
|
|
|
46 |
private bool $includeSeparatorLine = false;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Whether to write a fully Excel compatible CSV file.
|
|
|
50 |
*/
|
|
|
51 |
private bool $excelCompatibility = false;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Output encoding.
|
|
|
55 |
*/
|
|
|
56 |
private string $outputEncoding = '';
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Whether number of columns should be allowed to vary
|
|
|
60 |
* between rows, or use a fixed range based on the max
|
|
|
61 |
* column overall.
|
|
|
62 |
*/
|
|
|
63 |
private bool $variableColumns = false;
|
|
|
64 |
|
|
|
65 |
private bool $preferHyperlinkToLabel = false;
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Create a new CSV.
|
|
|
69 |
*/
|
|
|
70 |
public function __construct(Spreadsheet $spreadsheet)
|
|
|
71 |
{
|
|
|
72 |
$this->spreadsheet = $spreadsheet;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Save PhpSpreadsheet to file.
|
|
|
77 |
*
|
|
|
78 |
* @param resource|string $filename
|
|
|
79 |
*/
|
|
|
80 |
public function save($filename, int $flags = 0): void
|
|
|
81 |
{
|
|
|
82 |
$this->processFlags($flags);
|
|
|
83 |
|
|
|
84 |
// Fetch sheet
|
|
|
85 |
$sheet = $this->spreadsheet->getSheet($this->sheetIndex);
|
|
|
86 |
|
|
|
87 |
$saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog();
|
|
|
88 |
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false);
|
|
|
89 |
$sheet->calculateArrays($this->preCalculateFormulas);
|
|
|
90 |
|
|
|
91 |
// Open file
|
|
|
92 |
$this->openFileHandle($filename);
|
|
|
93 |
|
|
|
94 |
if ($this->excelCompatibility) {
|
|
|
95 |
$this->setUseBOM(true); // Enforce UTF-8 BOM Header
|
|
|
96 |
$this->setIncludeSeparatorLine(true); // Set separator line
|
|
|
97 |
$this->setEnclosure('"'); // Set enclosure to "
|
|
|
98 |
$this->setDelimiter(';'); // Set delimiter to a semi-colon
|
|
|
99 |
$this->setLineEnding("\r\n");
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if ($this->useBOM) {
|
|
|
103 |
// Write the UTF-8 BOM code if required
|
|
|
104 |
fwrite($this->fileHandle, "\xEF\xBB\xBF");
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if ($this->includeSeparatorLine) {
|
|
|
108 |
// Write the separator line if required
|
|
|
109 |
fwrite($this->fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Identify the range that we need to extract from the worksheet
|
|
|
113 |
$maxCol = $sheet->getHighestDataColumn();
|
|
|
114 |
$maxRow = $sheet->getHighestDataRow();
|
|
|
115 |
|
|
|
116 |
// Write rows to file
|
|
|
117 |
$row = 0;
|
|
|
118 |
foreach ($sheet->rangeToArrayYieldRows("A1:$maxCol$maxRow", '', $this->preCalculateFormulas) as $cellsArray) {
|
|
|
119 |
++$row;
|
|
|
120 |
if ($this->variableColumns) {
|
|
|
121 |
$column = $sheet->getHighestDataColumn($row);
|
|
|
122 |
if ($column === 'A' && !$sheet->cellExists("A$row")) {
|
|
|
123 |
$cellsArray = [];
|
|
|
124 |
} else {
|
|
|
125 |
array_splice($cellsArray, Coordinate::columnIndexFromString($column));
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
if ($this->preferHyperlinkToLabel) {
|
|
|
129 |
foreach ($cellsArray as $key => $value) {
|
|
|
130 |
$url = $sheet->getCell([$key + 1, $row])->getHyperlink()->getUrl();
|
|
|
131 |
if ($url !== '') {
|
|
|
132 |
$cellsArray[$key] = $url;
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
$this->writeLine($this->fileHandle, $cellsArray);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
$this->maybeCloseFileHandle();
|
|
|
140 |
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public function getDelimiter(): string
|
|
|
144 |
{
|
|
|
145 |
return $this->delimiter;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public function setDelimiter(string $delimiter): self
|
|
|
149 |
{
|
|
|
150 |
$this->delimiter = $delimiter;
|
|
|
151 |
|
|
|
152 |
return $this;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public function getEnclosure(): string
|
|
|
156 |
{
|
|
|
157 |
return $this->enclosure;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
public function setEnclosure(string $enclosure = '"'): self
|
|
|
161 |
{
|
|
|
162 |
$this->enclosure = $enclosure;
|
|
|
163 |
|
|
|
164 |
return $this;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public function getLineEnding(): string
|
|
|
168 |
{
|
|
|
169 |
return $this->lineEnding;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
public function setLineEnding(string $lineEnding): self
|
|
|
173 |
{
|
|
|
174 |
$this->lineEnding = $lineEnding;
|
|
|
175 |
|
|
|
176 |
return $this;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Get whether BOM should be used.
|
|
|
181 |
*/
|
|
|
182 |
public function getUseBOM(): bool
|
|
|
183 |
{
|
|
|
184 |
return $this->useBOM;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Set whether BOM should be used, typically when non-ASCII characters are used.
|
|
|
189 |
*/
|
|
|
190 |
public function setUseBOM(bool $useBOM): self
|
|
|
191 |
{
|
|
|
192 |
$this->useBOM = $useBOM;
|
|
|
193 |
|
|
|
194 |
return $this;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Get whether a separator line should be included.
|
|
|
199 |
*/
|
|
|
200 |
public function getIncludeSeparatorLine(): bool
|
|
|
201 |
{
|
|
|
202 |
return $this->includeSeparatorLine;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Set whether a separator line should be included as the first line of the file.
|
|
|
207 |
*/
|
|
|
208 |
public function setIncludeSeparatorLine(bool $includeSeparatorLine): self
|
|
|
209 |
{
|
|
|
210 |
$this->includeSeparatorLine = $includeSeparatorLine;
|
|
|
211 |
|
|
|
212 |
return $this;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Get whether the file should be saved with full Excel Compatibility.
|
|
|
217 |
*/
|
|
|
218 |
public function getExcelCompatibility(): bool
|
|
|
219 |
{
|
|
|
220 |
return $this->excelCompatibility;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
* Set whether the file should be saved with full Excel Compatibility.
|
|
|
225 |
*
|
|
|
226 |
* @param bool $excelCompatibility Set the file to be written as a fully Excel compatible csv file
|
|
|
227 |
* Note that this overrides other settings such as useBOM, enclosure and delimiter
|
|
|
228 |
*/
|
|
|
229 |
public function setExcelCompatibility(bool $excelCompatibility): self
|
|
|
230 |
{
|
|
|
231 |
$this->excelCompatibility = $excelCompatibility;
|
|
|
232 |
|
|
|
233 |
return $this;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
public function getSheetIndex(): int
|
|
|
237 |
{
|
|
|
238 |
return $this->sheetIndex;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public function setSheetIndex(int $sheetIndex): self
|
|
|
242 |
{
|
|
|
243 |
$this->sheetIndex = $sheetIndex;
|
|
|
244 |
|
|
|
245 |
return $this;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
public function getOutputEncoding(): string
|
|
|
249 |
{
|
|
|
250 |
return $this->outputEncoding;
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
public function setOutputEncoding(string $outputEnconding): self
|
|
|
254 |
{
|
|
|
255 |
$this->outputEncoding = $outputEnconding;
|
|
|
256 |
|
|
|
257 |
return $this;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
private bool $enclosureRequired = true;
|
|
|
261 |
|
|
|
262 |
public function setEnclosureRequired(bool $value): self
|
|
|
263 |
{
|
|
|
264 |
$this->enclosureRequired = $value;
|
|
|
265 |
|
|
|
266 |
return $this;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
public function getEnclosureRequired(): bool
|
|
|
270 |
{
|
|
|
271 |
return $this->enclosureRequired;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Convert boolean to TRUE/FALSE; otherwise return element cast to string.
|
|
|
276 |
*
|
|
|
277 |
* @param null|bool|float|int|string|Stringable $element element to be converted
|
|
|
278 |
*/
|
|
|
279 |
private static function elementToString(mixed $element): string
|
|
|
280 |
{
|
|
|
281 |
if (is_bool($element)) {
|
|
|
282 |
return $element ? 'TRUE' : 'FALSE';
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
return (string) $element;
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Write line to CSV file.
|
|
|
290 |
*
|
|
|
291 |
* @param resource $fileHandle PHP filehandle
|
|
|
292 |
* @param array $values Array containing values in a row
|
|
|
293 |
*/
|
|
|
294 |
private function writeLine($fileHandle, array $values): void
|
|
|
295 |
{
|
|
|
296 |
// No leading delimiter
|
|
|
297 |
$delimiter = '';
|
|
|
298 |
|
|
|
299 |
// Build the line
|
|
|
300 |
$line = '';
|
|
|
301 |
|
|
|
302 |
/** @var null|bool|float|int|string|Stringable $element */
|
|
|
303 |
foreach ($values as $element) {
|
|
|
304 |
$element = self::elementToString($element);
|
|
|
305 |
// Add delimiter
|
|
|
306 |
$line .= $delimiter;
|
|
|
307 |
$delimiter = $this->delimiter;
|
|
|
308 |
// Escape enclosures
|
|
|
309 |
$enclosure = $this->enclosure;
|
|
|
310 |
if ($enclosure) {
|
|
|
311 |
// If enclosure is not required, use enclosure only if
|
|
|
312 |
// element contains newline, delimiter, or enclosure.
|
|
|
313 |
if (!$this->enclosureRequired && strpbrk($element, "$delimiter$enclosure\n") === false) {
|
|
|
314 |
$enclosure = '';
|
|
|
315 |
} else {
|
|
|
316 |
$element = str_replace($enclosure, $enclosure . $enclosure, $element);
|
|
|
317 |
}
|
|
|
318 |
}
|
|
|
319 |
// Add enclosed string
|
|
|
320 |
$line .= $enclosure . $element . $enclosure;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
// Add line ending
|
|
|
324 |
$line .= $this->lineEnding;
|
|
|
325 |
|
|
|
326 |
// Write to file
|
|
|
327 |
if ($this->outputEncoding != '') {
|
|
|
328 |
$line = mb_convert_encoding($line, $this->outputEncoding);
|
|
|
329 |
}
|
|
|
330 |
fwrite($fileHandle, $line);
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Get whether number of columns should be allowed to vary
|
|
|
335 |
* between rows, or use a fixed range based on the max
|
|
|
336 |
* column overall.
|
|
|
337 |
*/
|
|
|
338 |
public function getVariableColumns(): bool
|
|
|
339 |
{
|
|
|
340 |
return $this->variableColumns;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Set whether number of columns should be allowed to vary
|
|
|
345 |
* between rows, or use a fixed range based on the max
|
|
|
346 |
* column overall.
|
|
|
347 |
*/
|
|
|
348 |
public function setVariableColumns(bool $pValue): self
|
|
|
349 |
{
|
|
|
350 |
$this->variableColumns = $pValue;
|
|
|
351 |
|
|
|
352 |
return $this;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Get whether hyperlink or label should be output.
|
|
|
357 |
*/
|
|
|
358 |
public function getPreferHyperlinkToLabel(): bool
|
|
|
359 |
{
|
|
|
360 |
return $this->preferHyperlinkToLabel;
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* Set whether hyperlink or label should be output.
|
|
|
365 |
*/
|
|
|
366 |
public function setPreferHyperlinkToLabel(bool $preferHyperlinkToLabel): self
|
|
|
367 |
{
|
|
|
368 |
$this->preferHyperlinkToLabel = $preferHyperlinkToLabel;
|
|
|
369 |
|
|
|
370 |
return $this;
|
|
|
371 |
}
|
|
|
372 |
}
|