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