1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSheet; |
4
|
|
|
|
5
|
|
|
use OneSheet\Style\Style; |
6
|
|
|
use OneSheet\Style\Styler; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Writer |
10
|
|
|
* |
11
|
|
|
* @package OneSheet |
12
|
|
|
*/ |
13
|
|
|
class Writer |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var SheetFile |
17
|
|
|
*/ |
18
|
|
|
private $sheetFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Styler |
22
|
|
|
*/ |
23
|
|
|
private $styler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Sheet |
27
|
|
|
*/ |
28
|
|
|
private $sheet; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Writer constructor. |
32
|
|
|
*/ |
33
|
2 |
|
public function __construct() |
34
|
|
|
{ |
35
|
2 |
|
$this->initialize(); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* All cells _above_ this cell will be fozen/fixed. |
40
|
|
|
* |
41
|
|
|
* @param int $cellId |
42
|
|
|
*/ |
43
|
|
|
public function setfreezePaneCellId($cellId) |
44
|
|
|
{ |
45
|
|
|
$this->sheet->setFreezePaneId($cellId); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set fixed column widths per cell (no ranges) and array index |
50
|
|
|
* 0 beeing the first column. |
51
|
|
|
* If used alongside cell autosizing, these should be set |
52
|
|
|
* after the last row has been added. |
53
|
|
|
* |
54
|
|
|
* @param array $columnWidths |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
public function setFixedColumnWidths(array $columnWidths) |
58
|
|
|
{ |
59
|
|
|
$this->sheet->setFixedColumnWidths($columnWidths); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Start recording row specs for column autosizing. |
64
|
|
|
* |
65
|
|
|
* @return Writer |
66
|
|
|
*/ |
67
|
1 |
|
public function enableCellAutosizing() |
68
|
|
|
{ |
69
|
1 |
|
$this->sheet->enableCellAutosizing(); |
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Stop recording row specs for column autosizing. |
75
|
|
|
* |
76
|
|
|
* @return Writer |
77
|
|
|
*/ |
78
|
|
|
public function disableCellAutosizing() |
79
|
|
|
{ |
80
|
|
|
$this->sheet->disableCellAutosizing(); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Initialize writer defaults. |
86
|
|
|
*/ |
87
|
2 |
|
private function initialize() |
88
|
|
|
{ |
89
|
2 |
|
$this->sheetFile = new SheetFile(); |
90
|
2 |
|
$this->sheetFile->fwrite(str_repeat(' ', pow(2, 20)) . '<sheetData>'); |
91
|
2 |
|
$this->styler = new Styler(); |
92
|
2 |
|
$this->sheet = new Sheet(); |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $rows |
97
|
|
|
* @param Style $style |
98
|
|
|
* @throws \InvalidArgumentException |
99
|
|
|
*/ |
100
|
2 |
|
public function addRows(array $rows, Style $style = null) |
101
|
|
|
{ |
102
|
2 |
|
if (count($rows) === count($rows, COUNT_RECURSIVE)) { |
103
|
1 |
|
throw new \InvalidArgumentException('Array must contain arrays!'); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
foreach ($rows as $row) { |
107
|
1 |
|
$this->addRow($row, $style); |
108
|
1 |
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $row |
113
|
|
|
* @param Style $style |
114
|
|
|
*/ |
115
|
1 |
|
public function addRow(array $row, Style $style = null) |
116
|
|
|
{ |
117
|
1 |
|
$style = $style instanceof Style ? $style : $this->styler->getDefaultStyle(); |
118
|
1 |
|
$this->styler->addStyle($style); |
119
|
1 |
|
$this->sheetFile->fwrite($this->sheet->addRow($row, $style)); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Wrap things up and write/output xlsx. |
124
|
|
|
* |
125
|
|
|
* @param string $fileName |
126
|
|
|
*/ |
127
|
1 |
|
public function writeToFile($fileName = 'dummy.xlsx') |
128
|
|
|
{ |
129
|
1 |
|
$finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile); |
130
|
1 |
|
$finalizer->finalize($fileName); |
131
|
1 |
|
} |
132
|
|
|
} |
133
|
|
|
|