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
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $encoding; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Writer constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $encoding |
39
|
|
|
*/ |
40
|
1 |
|
public function __construct($encoding = 'utf-8') |
41
|
|
|
{ |
42
|
1 |
|
$this->encoding = $encoding; |
43
|
1 |
|
$this->initialize(); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* All cells _above_ this cell will be fozen/fixed. |
48
|
|
|
* |
49
|
|
|
* @param int $cellId |
50
|
|
|
*/ |
51
|
|
|
public function setfreezePaneCellId($cellId) |
52
|
|
|
{ |
53
|
|
|
$this->sheet->setFreezePaneId($cellId); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Writer |
58
|
|
|
*/ |
59
|
1 |
|
public function enableCellAutosizing() |
60
|
|
|
{ |
61
|
1 |
|
$this->sheet->enableCellAutosizing(); |
62
|
1 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Initialize writer defaults. |
67
|
|
|
*/ |
68
|
1 |
|
private function initialize() |
69
|
|
|
{ |
70
|
1 |
|
$this->sheetFile = new SheetFile(); |
71
|
1 |
|
$this->sheetFile->fwrite(str_repeat(' ', pow(2, 20)) . '<sheetData>'); |
72
|
1 |
|
$this->styler = new Styler(); |
73
|
1 |
|
$this->sheet = new Sheet(); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $rows |
78
|
|
|
* @param Style $style |
79
|
|
|
*/ |
80
|
1 |
|
public function addRows(array $rows, Style $style = null) |
81
|
|
|
{ |
82
|
1 |
|
if (count($rows) !== count($rows, COUNT_RECURSIVE)) { |
83
|
1 |
|
foreach ($rows as $row) { |
84
|
1 |
|
$this->addRow($row, $style); |
85
|
1 |
|
} |
86
|
1 |
|
} |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $row |
91
|
|
|
* @param Style $style |
92
|
|
|
*/ |
93
|
1 |
|
public function addRow(array $row, Style $style = null) |
94
|
|
|
{ |
95
|
1 |
|
$style = $style instanceof Style ? $style : $this->styler->getDefaultStyle(); |
96
|
1 |
|
$this->styler->addStyle($style); |
97
|
1 |
|
$this->sheetFile->fwrite($this->sheet->addRow($row, $style)); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Wrap things up and write/output xlsx. |
102
|
|
|
* |
103
|
|
|
* @param string $fileName |
104
|
|
|
*/ |
105
|
1 |
|
public function writeToFile($fileName = 'dummy.xlsx') |
106
|
|
|
{ |
107
|
1 |
|
$finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile, $this->encoding); |
108
|
1 |
|
$finalizer->finalize($fileName); |
109
|
1 |
|
} |
110
|
|
|
} |
111
|
|
|
|