1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSheet; |
4
|
|
|
|
5
|
|
|
use OneSheet\Size\SizeCalculator; |
6
|
|
|
use OneSheet\Size\SizeCollection; |
7
|
|
|
use OneSheet\Style\Style; |
8
|
|
|
use OneSheet\Style\Styler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Writer |
12
|
|
|
* |
13
|
|
|
* @package OneSheet |
14
|
|
|
*/ |
15
|
|
|
class Writer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var SheetFile |
19
|
|
|
*/ |
20
|
|
|
private $sheetFile; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Styler |
24
|
|
|
*/ |
25
|
|
|
private $styler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Sheet |
29
|
|
|
*/ |
30
|
|
|
private $sheet; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var |
34
|
|
|
*/ |
35
|
|
|
private $output; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Writer constructor. |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct() |
41
|
|
|
{ |
42
|
7 |
|
$this->initialize(); |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* All cells _above_ this cell will be frozen/fixed. |
47
|
|
|
* |
48
|
|
|
* @param int $cellId |
49
|
|
|
*/ |
50
|
1 |
|
public function setFreezePaneCellId($cellId) |
51
|
|
|
{ |
52
|
1 |
|
$this->sheet->setFreezePaneCellId($cellId); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set fixed column widths per cell (no ranges) and array index |
57
|
|
|
* 0 being the first column. |
58
|
|
|
* If used alongside cell autosizing, these should be set |
59
|
|
|
* after the last row has been added. |
60
|
|
|
* |
61
|
|
|
* @param array $columnWidths |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
1 |
|
public function setFixedColumnWidths(array $columnWidths) |
65
|
|
|
{ |
66
|
1 |
|
$this->sheet->setFixedColumnWidths($columnWidths); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set lower and/or upper limits for column widths. |
71
|
|
|
* |
72
|
|
|
* @param float|null $minWidth |
73
|
|
|
* @param float|null $maxWidth |
74
|
|
|
*/ |
75
|
1 |
|
public function setColumnWidthLimits($minWidth = null, $maxWidth = null) |
76
|
|
|
{ |
77
|
1 |
|
$this->sheet->setColumnWidthLimits($minWidth, $maxWidth); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Start recording row specs for column autosizing. |
82
|
|
|
* |
83
|
|
|
* @return Writer |
84
|
|
|
*/ |
85
|
3 |
|
public function enableCellAutosizing() |
86
|
|
|
{ |
87
|
3 |
|
$this->sheet->enableCellAutosizing(); |
88
|
3 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Stop recording row specs for column autosizing. |
93
|
|
|
* |
94
|
|
|
* @return Writer |
95
|
|
|
*/ |
96
|
1 |
|
public function disableCellAutosizing() |
97
|
|
|
{ |
98
|
1 |
|
$this->sheet->disableCellAutosizing(); |
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Initialize writer defaults. |
104
|
|
|
*/ |
105
|
7 |
|
private function initialize() |
106
|
|
|
{ |
107
|
7 |
|
$this->sheetFile = new SheetFile(); |
108
|
7 |
|
$this->sheetFile->fwrite(str_repeat(' ', 1024 * 1024) . '<sheetData>'); |
109
|
7 |
|
$this->styler = new Styler(); |
110
|
7 |
|
$this->sheet = new Sheet(new CellBuilder(), new SizeCalculator(new SizeCollection())); |
111
|
7 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $rows |
115
|
|
|
* @param Style $style |
116
|
|
|
* @throws \InvalidArgumentException |
117
|
|
|
*/ |
118
|
2 |
|
public function addRows(array $rows, Style $style = null) |
119
|
|
|
{ |
120
|
2 |
|
if (count($rows) === count($rows, COUNT_RECURSIVE)) { |
121
|
1 |
|
throw new \InvalidArgumentException('Array must contain arrays!'); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
foreach ($rows as $row) { |
125
|
1 |
|
$this->addRow($row, $style); |
126
|
1 |
|
} |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $row |
131
|
|
|
* @param Style $style |
132
|
|
|
*/ |
133
|
1 |
|
public function addRow(array $row, Style $style = null) |
134
|
|
|
{ |
135
|
1 |
|
if (!empty($row)) { |
136
|
1 |
|
$style = $style instanceof Style ? $style : $this->styler->getDefaultStyle(); |
137
|
1 |
|
$this->styler->addStyle($style); |
138
|
1 |
|
$this->sheetFile->fwrite($this->sheet->addRow($row, $style)); |
139
|
1 |
|
} |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Wrap things up and send xlsx to browser. |
144
|
|
|
* |
145
|
|
|
* @param string $fileName |
146
|
|
|
*/ |
147
|
|
|
public function writeToBrowser($fileName = 'report.xlsx') |
148
|
|
|
{ |
149
|
|
|
$this->output = fopen('php://output', 'w'); |
150
|
|
|
$finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile); |
151
|
|
|
$this->sendHeaders($fileName); |
152
|
|
|
$finalizer->finalize($this->output); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Wrap things up and write xlsx. |
157
|
|
|
* |
158
|
|
|
* @param string $fileName |
159
|
|
|
*/ |
160
|
1 |
|
public function writeToFile($fileName = 'report.xlsx') |
161
|
|
|
{ |
162
|
1 |
|
$this->output = fopen($fileName, 'w'); |
163
|
1 |
|
$finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile); |
164
|
1 |
|
$finalizer->finalize($this->output); |
165
|
1 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Output headers & content and unlink the xlsx file eventually. |
169
|
|
|
* |
170
|
|
|
* @param string $fileName |
171
|
|
|
*/ |
172
|
|
|
private function sendHeaders($fileName) |
173
|
|
|
{ |
174
|
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); |
175
|
|
|
header('Content-Disposition: attachment; filename="' . $fileName . '"'); |
176
|
|
|
header('Cache-Control: max-age=0'); |
177
|
|
|
header('Pragma: public'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|