1 | <?php |
||
14 | class Writer |
||
15 | { |
||
16 | /** |
||
17 | * @var SheetFile |
||
18 | */ |
||
19 | private $sheetFile; |
||
20 | |||
21 | /** |
||
22 | * @var Styler |
||
23 | */ |
||
24 | private $styler; |
||
25 | |||
26 | /** |
||
27 | * @var Sheet |
||
28 | */ |
||
29 | private $sheet; |
||
30 | |||
31 | /** |
||
32 | * @var resource |
||
33 | */ |
||
34 | private $output; |
||
35 | |||
36 | /** |
||
37 | * If a $fontsDirectory is supplied it will be scanned for usable ttf/otf fonts |
||
38 | * to be used for cell auto-sizing. Keep in mind though - viewers of an excel |
||
39 | * file have to have that font on their machine. XLSX does not embed fonts! |
||
40 | * |
||
41 | * @param string|null $fontsDirectory |
||
42 | * @throws \Exception |
||
43 | */ |
||
44 | 11 | public function __construct($fontsDirectory = null) |
|
45 | { |
||
46 | 11 | $this->sheetFile = new SheetFile(); |
|
47 | 11 | $this->sheetFile->fwrite(str_repeat(' ', 1024 * 1024) . '<sheetData>'); |
|
48 | 11 | $this->styler = new Styler(); |
|
49 | 11 | $this->sheet = new Sheet(new CellBuilder(), new SizeCalculator($fontsDirectory)); |
|
50 | 11 | } |
|
51 | |||
52 | /** |
||
53 | * All cells _above_ this cell will be frozen/fixed. |
||
54 | * |
||
55 | * @param string $cellId |
||
56 | */ |
||
57 | 1 | public function setFreezePaneCellId($cellId) |
|
58 | { |
||
59 | 1 | $this->sheet->setFreezePaneCellId($cellId); |
|
60 | 1 | } |
|
61 | |||
62 | /** |
||
63 | * Set fixed column widths per cell (no ranges) and array index |
||
64 | * 0 being the first column. |
||
65 | * If used alongside cell autosizing, these should be set |
||
66 | * after the last row has been added. |
||
67 | * |
||
68 | * @param array $columnWidths |
||
69 | * @throws \InvalidArgumentException |
||
70 | */ |
||
71 | 1 | public function setFixedColumnWidths(array $columnWidths) |
|
72 | { |
||
73 | 1 | $this->sheet->setFixedColumnWidths($columnWidths); |
|
74 | 1 | } |
|
75 | |||
76 | /** |
||
77 | * Set lower and/or upper limits for column widths. |
||
78 | * |
||
79 | * @param float|null $minWidth |
||
80 | * @param float|null $maxWidth |
||
81 | */ |
||
82 | 1 | public function setColumnWidthLimits($minWidth = null, $maxWidth = null) |
|
83 | { |
||
84 | 1 | $this->sheet->setColumnWidthLimits($minWidth, $maxWidth); |
|
85 | 1 | } |
|
86 | |||
87 | /** |
||
88 | * Start recording row specs for column autosizing. |
||
89 | * |
||
90 | * @return Writer |
||
91 | */ |
||
92 | 3 | public function enableCellAutosizing() |
|
93 | { |
||
94 | 3 | $this->sheet->enableCellAutosizing(); |
|
95 | 3 | return $this; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Stop recording row specs for column autosizing. |
||
100 | * |
||
101 | * @return Writer |
||
102 | */ |
||
103 | 1 | public function disableCellAutosizing() |
|
104 | { |
||
105 | 1 | $this->sheet->disableCellAutosizing(); |
|
106 | 1 | return $this; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Add multiple rows at once. |
||
111 | * |
||
112 | * @param array|\Traversable $rows |
||
113 | * @param Style $style |
||
114 | * @throws \InvalidArgumentException |
||
115 | */ |
||
116 | 4 | public function addRows($rows, Style $style = null) |
|
117 | { |
||
118 | 4 | if (!(is_array($rows) || is_object($rows) && $rows instanceof \Traversable)) { |
|
119 | 1 | throw new \InvalidArgumentException('Expected array or traversable object as rows', 1517564833); |
|
120 | } |
||
121 | |||
122 | 3 | foreach ($rows as $row) { |
|
123 | 3 | $this->addRow($row, $style); |
|
124 | 3 | } |
|
125 | 3 | } |
|
126 | |||
127 | /** |
||
128 | * Add a single new row to the sheet and supply an optional style. |
||
129 | * |
||
130 | * @param array $row |
||
131 | * @param Style $style |
||
132 | */ |
||
133 | 4 | public function addRow(array $row, Style $style = null) |
|
141 | |||
142 | /** |
||
143 | * Wrap things up and write xlsx. |
||
144 | * |
||
145 | * @param string $fileName |
||
146 | */ |
||
147 | 3 | public function writeToFile($fileName = 'report.xlsx') |
|
153 | |||
154 | /** |
||
155 | * Wrap things up and send xlsx to browser. |
||
156 | * |
||
157 | * @param string $fileName |
||
158 | */ |
||
159 | 1 | public function writeToBrowser($fileName = 'report.xlsx') |
|
166 | |||
167 | /** |
||
168 | * Send headers for browser output. |
||
169 | * |
||
170 | * @param string $fileName |
||
171 | */ |
||
172 | 1 | private function sendHeaders($fileName) |
|
181 | |||
182 | /** |
||
183 | * Return array of available fonts & paths as key value pairs. |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | 1 | public function getFonts() |
|
191 | } |
||
192 |