1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SimpleExcel; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Style\Style; |
6
|
|
|
use Box\Spout\Writer\Common\Creator\WriterEntityFactory; |
7
|
|
|
use Box\Spout\Writer\WriterInterface; |
8
|
|
|
|
9
|
|
|
class SimpleExcelWriter |
10
|
|
|
{ |
11
|
|
|
private WriterInterface $writer; |
|
|
|
|
12
|
|
|
|
13
|
|
|
private string $path = ''; |
14
|
|
|
|
15
|
|
|
private bool $processHeader = true; |
16
|
|
|
|
17
|
|
|
private bool $processingFirstRow = true; |
18
|
|
|
|
19
|
|
|
private int $numberOfRows = 0; |
20
|
|
|
|
21
|
|
|
public static function create(string $file) |
22
|
|
|
{ |
23
|
|
|
$simpleExcelWriter = new static($file); |
24
|
|
|
|
25
|
|
|
$simpleExcelWriter->getWriter()->openToFile($file); |
26
|
|
|
|
27
|
|
|
return $simpleExcelWriter; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function streamDownload(string $downloadName) |
31
|
|
|
{ |
32
|
|
|
$simpleExcelWriter = new static($downloadName); |
33
|
|
|
|
34
|
|
|
$simpleExcelWriter->getWriter()->openToBrowser($downloadName); |
35
|
|
|
|
36
|
|
|
return $simpleExcelWriter; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function __construct(string $path) |
40
|
|
|
{ |
41
|
|
|
$this->writer = WriterEntityFactory::createWriterFromFile($path); |
42
|
|
|
|
43
|
|
|
$this->path = $path; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getPath(): string |
47
|
|
|
{ |
48
|
|
|
return $this->path; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getWriter(): WriterInterface |
52
|
|
|
{ |
53
|
|
|
return $this->writer; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getNumberOfRows(): int |
57
|
|
|
{ |
58
|
|
|
return $this->numberOfRows; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function noHeaderRow() |
62
|
|
|
{ |
63
|
|
|
$this->processHeader = false; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Box\Spout\Common\Entity\Row|array $row |
70
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style|null $style |
71
|
|
|
*/ |
72
|
|
|
public function addRow($row, Style $style = null) |
73
|
|
|
{ |
74
|
|
|
if (is_array($row)) { |
75
|
|
|
if ($this->processHeader && $this->processingFirstRow) { |
76
|
|
|
$this->writeHeaderFromRow($row); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$row = WriterEntityFactory::createRowFromArray($row, $style); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->writer->addRow($row); |
83
|
|
|
$this->numberOfRows++; |
84
|
|
|
|
85
|
|
|
$this->processingFirstRow = false; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function writeHeaderFromRow(array $row) |
91
|
|
|
{ |
92
|
|
|
$headerValues = array_keys($row); |
93
|
|
|
|
94
|
|
|
$headerRow = WriterEntityFactory::createRowFromArray($headerValues); |
95
|
|
|
|
96
|
|
|
$this->writer->addRow($headerRow); |
97
|
|
|
$this->numberOfRows++; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function toBrowser() |
101
|
|
|
{ |
102
|
|
|
$this->writer->close(); |
103
|
|
|
|
104
|
|
|
exit; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function close() |
108
|
|
|
{ |
109
|
|
|
$this->writer->close(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function useDelimiter(string $delimiter): self |
113
|
|
|
{ |
114
|
|
|
$this->writer->setFieldDelimiter($delimiter); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function __destruct() |
120
|
|
|
{ |
121
|
|
|
$this->close(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|