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