|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ellumilel\Xl; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @todo work with all styles |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://msdn.microsoft.com/en-us/library/bb264572(v=office.12).aspx |
|
8
|
|
|
* |
|
9
|
|
|
* Class Styles |
|
10
|
|
|
* @package Ellumilel\Xl |
|
11
|
|
|
* @author Denis Tikhonov <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Styles |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $urlOpenXmlFormat = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; |
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private $cellFormats; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public function buildStylesXML() |
|
26
|
|
|
{ |
|
27
|
|
|
$xml = ''; |
|
28
|
|
|
$xml .= $this->xml; |
|
29
|
|
|
$xml .= '<styleSheet xmlns="'.$this->urlOpenXmlFormat.'">'; |
|
30
|
|
|
$xml .= '<numFmts count="'.count($this->cellFormats).'">'; |
|
31
|
|
|
foreach ($this->cellFormats as $i => $v) { |
|
32
|
|
|
$xml .= '<numFmt numFmtId="'.(164 + $i).'" formatCode="'. |
|
33
|
|
|
str_replace("'", "'", htmlspecialchars($v)). |
|
34
|
|
|
'" />'; |
|
35
|
|
|
} |
|
36
|
|
|
$xml .= '</numFmts>'; |
|
37
|
|
|
$xml .= $this->getFonts(); |
|
38
|
|
|
$xml .= $this->getFills(); |
|
39
|
|
|
$xml .= $this->getStyleXfs(); |
|
40
|
|
|
$xml .= $this->getXfs(); |
|
41
|
|
|
$xml .= $this->getCellStyles(); |
|
42
|
|
|
$xml .= '</styleSheet>'; |
|
43
|
|
|
|
|
44
|
|
|
return $xml; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
private function getFonts() |
|
51
|
|
|
{ |
|
52
|
|
|
$fonts = '<fonts count="4">'; |
|
53
|
|
|
$fonts .= '<font><name val="Arial"/><charset val="1"/><family val="2"/><sz val="10"/></font>'; |
|
54
|
|
|
$fonts .= '<font><name val="Arial"/><family val="0"/><sz val="10"/></font>'; |
|
55
|
|
|
$fonts .= '<font><name val="Arial"/><family val="0"/><sz val="10"/></font>'; |
|
56
|
|
|
$fonts .= '<font><name val="Arial"/><family val="0"/><sz val="10"/></font>'; |
|
57
|
|
|
$fonts .= '</fonts>'; |
|
58
|
|
|
|
|
59
|
|
|
return $fonts; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getFills() |
|
66
|
|
|
{ |
|
67
|
|
|
$fills = '<fills count="2"><fill><patternFill patternType="none"/></fill>'; |
|
68
|
|
|
$fills .= '<fill><patternFill patternType="gray125"/></fill></fills>'; |
|
69
|
|
|
$fills .= '<borders count="1">'; |
|
70
|
|
|
$fills .= '<border diagonalDown="false" diagonalUp="false"><left/><right/><top/><bottom/><diagonal/></border>'; |
|
71
|
|
|
$fills .= '</borders>'; |
|
72
|
|
|
|
|
73
|
|
|
return $fills; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
private function getStyleXfs() |
|
80
|
|
|
{ |
|
81
|
|
|
$xfs = '<cellStyleXfs count="20">'; |
|
82
|
|
|
$xfs .= '<xf applyAlignment="true" applyBorder="true" applyFont="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="164">'; |
|
83
|
|
|
$xfs .= '<alignment horizontal="general" indent="0" shrinkToFit="false" textRotation="0" vertical="bottom" wrapText="false"/>'; |
|
84
|
|
|
$xfs .= '<protection hidden="false" locked="true"/>'; |
|
85
|
|
|
$xfs .= '</xf>'; |
|
86
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"/>'; |
|
87
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"/>'; |
|
88
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"/>'; |
|
89
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"/>'; |
|
90
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
91
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
92
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
93
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
94
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
95
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
96
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
97
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
98
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
99
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'; |
|
100
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="43"/>'; |
|
101
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="41"/>'; |
|
102
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="44"/>'; |
|
103
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="42"/>'; |
|
104
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="9"/>'; |
|
105
|
|
|
$xfs .= '</cellStyleXfs>'; |
|
106
|
|
|
|
|
107
|
|
|
return $xfs; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
private function getXfs() |
|
114
|
|
|
{ |
|
115
|
|
|
$xfs = '<cellXfs count="'.count($this->cellFormats).'">'; |
|
116
|
|
|
foreach ($this->cellFormats as $i => $v) { |
|
117
|
|
|
$xfs .= '<xf applyAlignment="false" applyBorder="false" applyFont="false" |
|
118
|
|
|
applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="'.(164 + $i).'" xfId="0"/>'; |
|
119
|
|
|
} |
|
120
|
|
|
$xfs .= '</cellXfs>'; |
|
121
|
|
|
|
|
122
|
|
|
return $xfs; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
private function getCellStyles() |
|
129
|
|
|
{ |
|
130
|
|
|
$style = '<cellStyles count="6">'; |
|
131
|
|
|
$style .= '<cellStyle builtinId="0" customBuiltin="false" name="Normal" xfId="0"/>'; |
|
132
|
|
|
$style .= '<cellStyle builtinId="3" customBuiltin="false" name="Comma" xfId="15"/>'; |
|
133
|
|
|
$style .= '<cellStyle builtinId="6" customBuiltin="false" name="Comma [0]" xfId="16"/>'; |
|
134
|
|
|
$style .= '<cellStyle builtinId="4" customBuiltin="false" name="Currency" xfId="17"/>'; |
|
135
|
|
|
$style .= '<cellStyle builtinId="7" customBuiltin="false" name="Currency [0]" xfId="18"/>'; |
|
136
|
|
|
$style .= '<cellStyle builtinId="5" customBuiltin="false" name="Percent" xfId="19"/>'; |
|
137
|
|
|
$style .= '</cellStyles>'; |
|
138
|
|
|
|
|
139
|
|
|
return $style; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $cellFormats |
|
144
|
|
|
* |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setCellFormats(array $cellFormats) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->cellFormats = $cellFormats; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|