1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
|
10 |
use PhpOffice\PhpSpreadsheet\Style\Border;
|
|
|
11 |
use PhpOffice\PhpSpreadsheet\Style\Borders;
|
|
|
12 |
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
|
13 |
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
|
14 |
use PhpOffice\PhpSpreadsheet\Style\Font;
|
|
|
15 |
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
|
16 |
use PhpOffice\PhpSpreadsheet\Style\Protection;
|
|
|
17 |
|
|
|
18 |
class Style extends WriterPart
|
|
|
19 |
{
|
|
|
20 |
/**
|
|
|
21 |
* Write styles to XML format.
|
|
|
22 |
*
|
|
|
23 |
* @return string XML Output
|
|
|
24 |
*/
|
|
|
25 |
public function writeStyles(Spreadsheet $spreadsheet)
|
|
|
26 |
{
|
|
|
27 |
// Create XML writer
|
|
|
28 |
$objWriter = null;
|
|
|
29 |
if ($this->getParentWriter()->getUseDiskCaching()) {
|
|
|
30 |
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
|
|
31 |
} else {
|
|
|
32 |
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
// XML header
|
|
|
36 |
$objWriter->startDocument('1.0', 'UTF-8', 'yes');
|
|
|
37 |
|
|
|
38 |
// styleSheet
|
|
|
39 |
$objWriter->startElement('styleSheet');
|
|
|
40 |
$objWriter->writeAttribute('xml:space', 'preserve');
|
|
|
41 |
$objWriter->writeAttribute('xmlns', Namespaces::MAIN);
|
|
|
42 |
|
|
|
43 |
// numFmts
|
|
|
44 |
$objWriter->startElement('numFmts');
|
|
|
45 |
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getNumFmtHashTable()->count());
|
|
|
46 |
|
|
|
47 |
// numFmt
|
|
|
48 |
for ($i = 0; $i < $this->getParentWriter()->getNumFmtHashTable()->count(); ++$i) {
|
|
|
49 |
$this->writeNumFmt($objWriter, $this->getParentWriter()->getNumFmtHashTable()->getByIndex($i), $i);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$objWriter->endElement();
|
|
|
53 |
|
|
|
54 |
// fonts
|
|
|
55 |
$objWriter->startElement('fonts');
|
|
|
56 |
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getFontHashTable()->count());
|
|
|
57 |
|
|
|
58 |
// font
|
|
|
59 |
for ($i = 0; $i < $this->getParentWriter()->getFontHashTable()->count(); ++$i) {
|
|
|
60 |
$thisfont = $this->getParentWriter()->getFontHashTable()->getByIndex($i);
|
|
|
61 |
if ($thisfont !== null) {
|
|
|
62 |
$this->writeFont($objWriter, $thisfont);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$objWriter->endElement();
|
|
|
67 |
|
|
|
68 |
// fills
|
|
|
69 |
$objWriter->startElement('fills');
|
|
|
70 |
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getFillHashTable()->count());
|
|
|
71 |
|
|
|
72 |
// fill
|
|
|
73 |
for ($i = 0; $i < $this->getParentWriter()->getFillHashTable()->count(); ++$i) {
|
|
|
74 |
$thisfill = $this->getParentWriter()->getFillHashTable()->getByIndex($i);
|
|
|
75 |
if ($thisfill !== null) {
|
|
|
76 |
$this->writeFill($objWriter, $thisfill);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$objWriter->endElement();
|
|
|
81 |
|
|
|
82 |
// borders
|
|
|
83 |
$objWriter->startElement('borders');
|
|
|
84 |
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getBordersHashTable()->count());
|
|
|
85 |
|
|
|
86 |
// border
|
|
|
87 |
for ($i = 0; $i < $this->getParentWriter()->getBordersHashTable()->count(); ++$i) {
|
|
|
88 |
$thisborder = $this->getParentWriter()->getBordersHashTable()->getByIndex($i);
|
|
|
89 |
if ($thisborder !== null) {
|
|
|
90 |
$this->writeBorder($objWriter, $thisborder);
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$objWriter->endElement();
|
|
|
95 |
|
|
|
96 |
// cellStyleXfs
|
|
|
97 |
$objWriter->startElement('cellStyleXfs');
|
|
|
98 |
$objWriter->writeAttribute('count', '1');
|
|
|
99 |
|
|
|
100 |
// xf
|
|
|
101 |
$objWriter->startElement('xf');
|
|
|
102 |
$objWriter->writeAttribute('numFmtId', '0');
|
|
|
103 |
$objWriter->writeAttribute('fontId', '0');
|
|
|
104 |
$objWriter->writeAttribute('fillId', '0');
|
|
|
105 |
$objWriter->writeAttribute('borderId', '0');
|
|
|
106 |
$objWriter->endElement();
|
|
|
107 |
|
|
|
108 |
$objWriter->endElement();
|
|
|
109 |
|
|
|
110 |
// cellXfs
|
|
|
111 |
$objWriter->startElement('cellXfs');
|
|
|
112 |
$objWriter->writeAttribute('count', (string) count($spreadsheet->getCellXfCollection()));
|
|
|
113 |
|
|
|
114 |
// xf
|
|
|
115 |
$alignment = new Alignment();
|
|
|
116 |
$defaultAlignHash = $alignment->getHashCode();
|
|
|
117 |
if ($defaultAlignHash !== $spreadsheet->getDefaultStyle()->getAlignment()->getHashCode()) {
|
|
|
118 |
$defaultAlignHash = '';
|
|
|
119 |
}
|
|
|
120 |
foreach ($spreadsheet->getCellXfCollection() as $cellXf) {
|
|
|
121 |
$this->writeCellStyleXf($objWriter, $cellXf, $spreadsheet, $defaultAlignHash);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$objWriter->endElement();
|
|
|
125 |
|
|
|
126 |
// cellStyles
|
|
|
127 |
$objWriter->startElement('cellStyles');
|
|
|
128 |
$objWriter->writeAttribute('count', '1');
|
|
|
129 |
|
|
|
130 |
// cellStyle
|
|
|
131 |
$objWriter->startElement('cellStyle');
|
|
|
132 |
$objWriter->writeAttribute('name', 'Normal');
|
|
|
133 |
$objWriter->writeAttribute('xfId', '0');
|
|
|
134 |
$objWriter->writeAttribute('builtinId', '0');
|
|
|
135 |
$objWriter->endElement();
|
|
|
136 |
|
|
|
137 |
$objWriter->endElement();
|
|
|
138 |
|
|
|
139 |
// dxfs
|
|
|
140 |
$objWriter->startElement('dxfs');
|
|
|
141 |
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getStylesConditionalHashTable()->count());
|
|
|
142 |
|
|
|
143 |
// dxf
|
|
|
144 |
for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) {
|
|
|
145 |
$thisstyle = $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i);
|
|
|
146 |
if ($thisstyle !== null) {
|
|
|
147 |
$this->writeCellStyleDxf($objWriter, $thisstyle->getStyle());
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
$objWriter->endElement();
|
|
|
152 |
|
|
|
153 |
// tableStyles
|
|
|
154 |
$objWriter->startElement('tableStyles');
|
|
|
155 |
$objWriter->writeAttribute('defaultTableStyle', 'TableStyleMedium9');
|
|
|
156 |
$objWriter->writeAttribute('defaultPivotStyle', 'PivotTableStyle1');
|
|
|
157 |
$objWriter->endElement();
|
|
|
158 |
|
|
|
159 |
$objWriter->endElement();
|
|
|
160 |
|
|
|
161 |
// Return
|
|
|
162 |
return $objWriter->getData();
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Write Fill.
|
|
|
167 |
*/
|
|
|
168 |
private function writeFill(XMLWriter $objWriter, Fill $fill): void
|
|
|
169 |
{
|
|
|
170 |
// Check if this is a pattern type or gradient type
|
|
|
171 |
if (
|
|
|
172 |
$fill->getFillType() === Fill::FILL_GRADIENT_LINEAR ||
|
|
|
173 |
$fill->getFillType() === Fill::FILL_GRADIENT_PATH
|
|
|
174 |
) {
|
|
|
175 |
// Gradient fill
|
|
|
176 |
$this->writeGradientFill($objWriter, $fill);
|
|
|
177 |
} elseif ($fill->getFillType() !== null) {
|
|
|
178 |
// Pattern fill
|
|
|
179 |
$this->writePatternFill($objWriter, $fill);
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Write Gradient Fill.
|
|
|
185 |
*/
|
|
|
186 |
private function writeGradientFill(XMLWriter $objWriter, Fill $fill): void
|
|
|
187 |
{
|
|
|
188 |
// fill
|
|
|
189 |
$objWriter->startElement('fill');
|
|
|
190 |
|
|
|
191 |
// gradientFill
|
|
|
192 |
$objWriter->startElement('gradientFill');
|
|
|
193 |
$objWriter->writeAttribute('type', (string) $fill->getFillType());
|
|
|
194 |
$objWriter->writeAttribute('degree', (string) $fill->getRotation());
|
|
|
195 |
|
|
|
196 |
// stop
|
|
|
197 |
$objWriter->startElement('stop');
|
|
|
198 |
$objWriter->writeAttribute('position', '0');
|
|
|
199 |
|
|
|
200 |
// color
|
|
|
201 |
if ($fill->getStartColor()->getARGB() !== null) {
|
|
|
202 |
$objWriter->startElement('color');
|
|
|
203 |
$objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB());
|
|
|
204 |
$objWriter->endElement();
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
$objWriter->endElement();
|
|
|
208 |
|
|
|
209 |
// stop
|
|
|
210 |
$objWriter->startElement('stop');
|
|
|
211 |
$objWriter->writeAttribute('position', '1');
|
|
|
212 |
|
|
|
213 |
// color
|
|
|
214 |
if ($fill->getEndColor()->getARGB() !== null) {
|
|
|
215 |
$objWriter->startElement('color');
|
|
|
216 |
$objWriter->writeAttribute('rgb', $fill->getEndColor()->getARGB());
|
|
|
217 |
$objWriter->endElement();
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
$objWriter->endElement();
|
|
|
221 |
|
|
|
222 |
$objWriter->endElement();
|
|
|
223 |
|
|
|
224 |
$objWriter->endElement();
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
private static function writePatternColors(Fill $fill): bool
|
|
|
228 |
{
|
|
|
229 |
if ($fill->getFillType() === Fill::FILL_NONE) {
|
|
|
230 |
return false;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
return $fill->getFillType() === Fill::FILL_SOLID || $fill->getColorsChanged();
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Write Pattern Fill.
|
|
|
238 |
*/
|
|
|
239 |
private function writePatternFill(XMLWriter $objWriter, Fill $fill): void
|
|
|
240 |
{
|
|
|
241 |
// fill
|
|
|
242 |
$objWriter->startElement('fill');
|
|
|
243 |
|
|
|
244 |
// patternFill
|
|
|
245 |
$objWriter->startElement('patternFill');
|
|
|
246 |
$objWriter->writeAttribute('patternType', (string) $fill->getFillType());
|
|
|
247 |
|
|
|
248 |
if (self::writePatternColors($fill)) {
|
|
|
249 |
// fgColor
|
|
|
250 |
if ($fill->getStartColor()->getARGB()) {
|
|
|
251 |
if (!$fill->getEndColor()->getARGB() && $fill->getFillType() === Fill::FILL_SOLID) {
|
|
|
252 |
$objWriter->startElement('bgColor');
|
|
|
253 |
$objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB());
|
|
|
254 |
} else {
|
|
|
255 |
$objWriter->startElement('fgColor');
|
|
|
256 |
$objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB());
|
|
|
257 |
}
|
|
|
258 |
$objWriter->endElement();
|
|
|
259 |
}
|
|
|
260 |
// bgColor
|
|
|
261 |
if ($fill->getEndColor()->getARGB()) {
|
|
|
262 |
$objWriter->startElement('bgColor');
|
|
|
263 |
$objWriter->writeAttribute('rgb', $fill->getEndColor()->getARGB());
|
|
|
264 |
$objWriter->endElement();
|
|
|
265 |
}
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
$objWriter->endElement();
|
|
|
269 |
|
|
|
270 |
$objWriter->endElement();
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
private function startFont(XMLWriter $objWriter, bool &$fontStarted): void
|
|
|
274 |
{
|
|
|
275 |
if (!$fontStarted) {
|
|
|
276 |
$fontStarted = true;
|
|
|
277 |
$objWriter->startElement('font');
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Write Font.
|
|
|
283 |
*/
|
|
|
284 |
private function writeFont(XMLWriter $objWriter, Font $font): void
|
|
|
285 |
{
|
|
|
286 |
$fontStarted = false;
|
|
|
287 |
// font
|
|
|
288 |
// Weird! The order of these elements actually makes a difference when opening Xlsx
|
|
|
289 |
// files in Excel2003 with the compatibility pack. It's not documented behaviour,
|
|
|
290 |
// and makes for a real WTF!
|
|
|
291 |
|
|
|
292 |
// Bold. We explicitly write this element also when false (like MS Office Excel 2007 does
|
|
|
293 |
// for conditional formatting). Otherwise it will apparently not be picked up in conditional
|
|
|
294 |
// formatting style dialog
|
|
|
295 |
if ($font->getBold() !== null) {
|
|
|
296 |
$this->startFont($objWriter, $fontStarted);
|
|
|
297 |
$objWriter->startElement('b');
|
|
|
298 |
$objWriter->writeAttribute('val', $font->getBold() ? '1' : '0');
|
|
|
299 |
$objWriter->endElement();
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
// Italic
|
|
|
303 |
if ($font->getItalic() !== null) {
|
|
|
304 |
$this->startFont($objWriter, $fontStarted);
|
|
|
305 |
$objWriter->startElement('i');
|
|
|
306 |
$objWriter->writeAttribute('val', $font->getItalic() ? '1' : '0');
|
|
|
307 |
$objWriter->endElement();
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
// Strikethrough
|
|
|
311 |
if ($font->getStrikethrough() !== null) {
|
|
|
312 |
$this->startFont($objWriter, $fontStarted);
|
|
|
313 |
$objWriter->startElement('strike');
|
|
|
314 |
$objWriter->writeAttribute('val', $font->getStrikethrough() ? '1' : '0');
|
|
|
315 |
$objWriter->endElement();
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
// Underline
|
|
|
319 |
if ($font->getUnderline() !== null) {
|
|
|
320 |
$this->startFont($objWriter, $fontStarted);
|
|
|
321 |
$objWriter->startElement('u');
|
|
|
322 |
$objWriter->writeAttribute('val', $font->getUnderline());
|
|
|
323 |
$objWriter->endElement();
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
// Superscript / subscript
|
|
|
327 |
if ($font->getSuperscript() === true || $font->getSubscript() === true) {
|
|
|
328 |
$this->startFont($objWriter, $fontStarted);
|
|
|
329 |
$objWriter->startElement('vertAlign');
|
|
|
330 |
if ($font->getSuperscript() === true) {
|
|
|
331 |
$objWriter->writeAttribute('val', 'superscript');
|
|
|
332 |
} elseif ($font->getSubscript() === true) {
|
|
|
333 |
$objWriter->writeAttribute('val', 'subscript');
|
|
|
334 |
}
|
|
|
335 |
$objWriter->endElement();
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
// Size
|
|
|
339 |
if ($font->getSize() !== null) {
|
|
|
340 |
$this->startFont($objWriter, $fontStarted);
|
|
|
341 |
$objWriter->startElement('sz');
|
|
|
342 |
$objWriter->writeAttribute('val', StringHelper::formatNumber($font->getSize()));
|
|
|
343 |
$objWriter->endElement();
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
// Foreground color
|
|
|
347 |
if ($font->getColor()->getARGB() !== null) {
|
|
|
348 |
$this->startFont($objWriter, $fontStarted);
|
|
|
349 |
$objWriter->startElement('color');
|
|
|
350 |
$objWriter->writeAttribute('rgb', $font->getColor()->getARGB());
|
|
|
351 |
$objWriter->endElement();
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
// Name
|
|
|
355 |
if ($font->getName() !== null) {
|
|
|
356 |
$this->startFont($objWriter, $fontStarted);
|
|
|
357 |
$objWriter->startElement('name');
|
|
|
358 |
$objWriter->writeAttribute('val', $font->getName());
|
|
|
359 |
$objWriter->endElement();
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
if (!empty($font->getScheme())) {
|
|
|
363 |
$this->startFont($objWriter, $fontStarted);
|
|
|
364 |
$objWriter->startElement('scheme');
|
|
|
365 |
$objWriter->writeAttribute('val', $font->getScheme());
|
|
|
366 |
$objWriter->endElement();
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
if ($fontStarted) {
|
|
|
370 |
$objWriter->endElement();
|
|
|
371 |
}
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
/**
|
|
|
375 |
* Write Border.
|
|
|
376 |
*/
|
|
|
377 |
private function writeBorder(XMLWriter $objWriter, Borders $borders): void
|
|
|
378 |
{
|
|
|
379 |
// Write border
|
|
|
380 |
$objWriter->startElement('border');
|
|
|
381 |
// Diagonal?
|
|
|
382 |
switch ($borders->getDiagonalDirection()) {
|
|
|
383 |
case Borders::DIAGONAL_UP:
|
|
|
384 |
$objWriter->writeAttribute('diagonalUp', 'true');
|
|
|
385 |
$objWriter->writeAttribute('diagonalDown', 'false');
|
|
|
386 |
|
|
|
387 |
break;
|
|
|
388 |
case Borders::DIAGONAL_DOWN:
|
|
|
389 |
$objWriter->writeAttribute('diagonalUp', 'false');
|
|
|
390 |
$objWriter->writeAttribute('diagonalDown', 'true');
|
|
|
391 |
|
|
|
392 |
break;
|
|
|
393 |
case Borders::DIAGONAL_BOTH:
|
|
|
394 |
$objWriter->writeAttribute('diagonalUp', 'true');
|
|
|
395 |
$objWriter->writeAttribute('diagonalDown', 'true');
|
|
|
396 |
|
|
|
397 |
break;
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
// BorderPr
|
|
|
401 |
$this->writeBorderPr($objWriter, 'left', $borders->getLeft());
|
|
|
402 |
$this->writeBorderPr($objWriter, 'right', $borders->getRight());
|
|
|
403 |
$this->writeBorderPr($objWriter, 'top', $borders->getTop());
|
|
|
404 |
$this->writeBorderPr($objWriter, 'bottom', $borders->getBottom());
|
|
|
405 |
$this->writeBorderPr($objWriter, 'diagonal', $borders->getDiagonal());
|
|
|
406 |
$objWriter->endElement();
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
/** @var mixed */
|
|
|
410 |
private static $scrutinizerFalse = false;
|
|
|
411 |
|
|
|
412 |
/**
|
|
|
413 |
* Write Cell Style Xf.
|
|
|
414 |
*/
|
|
|
415 |
private function writeCellStyleXf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $style, Spreadsheet $spreadsheet, string $defaultAlignHash): void
|
|
|
416 |
{
|
|
|
417 |
// xf
|
|
|
418 |
$objWriter->startElement('xf');
|
|
|
419 |
$objWriter->writeAttribute('xfId', '0');
|
|
|
420 |
$objWriter->writeAttribute('fontId', (string) (int) $this->getParentWriter()->getFontHashTable()->getIndexForHashCode($style->getFont()->getHashCode()));
|
|
|
421 |
if ($style->getQuotePrefix()) {
|
|
|
422 |
$objWriter->writeAttribute('quotePrefix', '1');
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
if ($style->getNumberFormat()->getBuiltInFormatCode() === self::$scrutinizerFalse) {
|
|
|
426 |
$objWriter->writeAttribute('numFmtId', (string) (int) ($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($style->getNumberFormat()->getHashCode()) + 164));
|
|
|
427 |
} else {
|
|
|
428 |
$objWriter->writeAttribute('numFmtId', (string) (int) $style->getNumberFormat()->getBuiltInFormatCode());
|
|
|
429 |
}
|
|
|
430 |
|
|
|
431 |
$objWriter->writeAttribute('fillId', (string) (int) $this->getParentWriter()->getFillHashTable()->getIndexForHashCode($style->getFill()->getHashCode()));
|
|
|
432 |
$objWriter->writeAttribute('borderId', (string) (int) $this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($style->getBorders()->getHashCode()));
|
|
|
433 |
|
|
|
434 |
// Apply styles?
|
|
|
435 |
$objWriter->writeAttribute('applyFont', ($spreadsheet->getDefaultStyle()->getFont()->getHashCode() != $style->getFont()->getHashCode()) ? '1' : '0');
|
|
|
436 |
$objWriter->writeAttribute('applyNumberFormat', ($spreadsheet->getDefaultStyle()->getNumberFormat()->getHashCode() != $style->getNumberFormat()->getHashCode()) ? '1' : '0');
|
|
|
437 |
$objWriter->writeAttribute('applyFill', ($spreadsheet->getDefaultStyle()->getFill()->getHashCode() != $style->getFill()->getHashCode()) ? '1' : '0');
|
|
|
438 |
$objWriter->writeAttribute('applyBorder', ($spreadsheet->getDefaultStyle()->getBorders()->getHashCode() != $style->getBorders()->getHashCode()) ? '1' : '0');
|
|
|
439 |
if ($defaultAlignHash !== '' && $defaultAlignHash === $style->getAlignment()->getHashCode()) {
|
|
|
440 |
$applyAlignment = '0';
|
|
|
441 |
} else {
|
|
|
442 |
$applyAlignment = '1';
|
|
|
443 |
}
|
|
|
444 |
$objWriter->writeAttribute('applyAlignment', $applyAlignment);
|
|
|
445 |
if ($style->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $style->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) {
|
|
|
446 |
$objWriter->writeAttribute('applyProtection', 'true');
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
// alignment
|
|
|
450 |
if ($applyAlignment === '1') {
|
|
|
451 |
$objWriter->startElement('alignment');
|
|
|
452 |
$vertical = Alignment::VERTICAL_ALIGNMENT_FOR_XLSX[$style->getAlignment()->getVertical()] ?? '';
|
|
|
453 |
$horizontal = Alignment::HORIZONTAL_ALIGNMENT_FOR_XLSX[$style->getAlignment()->getHorizontal()] ?? '';
|
|
|
454 |
if ($horizontal !== '') {
|
|
|
455 |
$objWriter->writeAttribute('horizontal', $horizontal);
|
|
|
456 |
}
|
|
|
457 |
if ($vertical !== '') {
|
|
|
458 |
$objWriter->writeAttribute('vertical', $vertical);
|
|
|
459 |
}
|
|
|
460 |
|
|
|
461 |
if ($style->getAlignment()->getTextRotation() >= 0) {
|
|
|
462 |
$textRotation = $style->getAlignment()->getTextRotation();
|
|
|
463 |
} else {
|
|
|
464 |
$textRotation = 90 - $style->getAlignment()->getTextRotation();
|
|
|
465 |
}
|
|
|
466 |
$objWriter->writeAttribute('textRotation', (string) $textRotation);
|
|
|
467 |
|
|
|
468 |
$objWriter->writeAttribute('wrapText', ($style->getAlignment()->getWrapText() ? 'true' : 'false'));
|
|
|
469 |
$objWriter->writeAttribute('shrinkToFit', ($style->getAlignment()->getShrinkToFit() ? 'true' : 'false'));
|
|
|
470 |
|
|
|
471 |
if ($style->getAlignment()->getIndent() > 0) {
|
|
|
472 |
$objWriter->writeAttribute('indent', (string) $style->getAlignment()->getIndent());
|
|
|
473 |
}
|
|
|
474 |
if ($style->getAlignment()->getReadOrder() > 0) {
|
|
|
475 |
$objWriter->writeAttribute('readingOrder', (string) $style->getAlignment()->getReadOrder());
|
|
|
476 |
}
|
|
|
477 |
$objWriter->endElement();
|
|
|
478 |
}
|
|
|
479 |
|
|
|
480 |
// protection
|
|
|
481 |
if ($style->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $style->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) {
|
|
|
482 |
$objWriter->startElement('protection');
|
|
|
483 |
if ($style->getProtection()->getLocked() != Protection::PROTECTION_INHERIT) {
|
|
|
484 |
$objWriter->writeAttribute('locked', ($style->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
|
|
|
485 |
}
|
|
|
486 |
if ($style->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) {
|
|
|
487 |
$objWriter->writeAttribute('hidden', ($style->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
|
|
|
488 |
}
|
|
|
489 |
$objWriter->endElement();
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
$objWriter->endElement();
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
/**
|
|
|
496 |
* Write Cell Style Dxf.
|
|
|
497 |
*/
|
|
|
498 |
private function writeCellStyleDxf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $style): void
|
|
|
499 |
{
|
|
|
500 |
// dxf
|
|
|
501 |
$objWriter->startElement('dxf');
|
|
|
502 |
|
|
|
503 |
// font
|
|
|
504 |
$this->writeFont($objWriter, $style->getFont());
|
|
|
505 |
|
|
|
506 |
// numFmt
|
|
|
507 |
$this->writeNumFmt($objWriter, $style->getNumberFormat());
|
|
|
508 |
|
|
|
509 |
// fill
|
|
|
510 |
$this->writeFill($objWriter, $style->getFill());
|
|
|
511 |
|
|
|
512 |
// alignment
|
|
|
513 |
$horizontal = Alignment::HORIZONTAL_ALIGNMENT_FOR_XLSX[$style->getAlignment()->getHorizontal()] ?? '';
|
|
|
514 |
$vertical = Alignment::VERTICAL_ALIGNMENT_FOR_XLSX[$style->getAlignment()->getVertical()] ?? '';
|
|
|
515 |
$rotation = $style->getAlignment()->getTextRotation();
|
|
|
516 |
if ($horizontal || $vertical || $rotation !== null) {
|
|
|
517 |
$objWriter->startElement('alignment');
|
|
|
518 |
if ($horizontal) {
|
|
|
519 |
$objWriter->writeAttribute('horizontal', $horizontal);
|
|
|
520 |
}
|
|
|
521 |
if ($vertical) {
|
|
|
522 |
$objWriter->writeAttribute('vertical', $vertical);
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
if ($rotation !== null) {
|
|
|
526 |
if ($rotation >= 0) {
|
|
|
527 |
$textRotation = $rotation;
|
|
|
528 |
} else {
|
|
|
529 |
$textRotation = 90 - $rotation;
|
|
|
530 |
}
|
|
|
531 |
$objWriter->writeAttribute('textRotation', (string) $textRotation);
|
|
|
532 |
}
|
|
|
533 |
$objWriter->endElement();
|
|
|
534 |
}
|
|
|
535 |
|
|
|
536 |
// border
|
|
|
537 |
$this->writeBorder($objWriter, $style->getBorders());
|
|
|
538 |
|
|
|
539 |
// protection
|
|
|
540 |
if ((!empty($style->getProtection()->getLocked())) || (!empty($style->getProtection()->getHidden()))) {
|
|
|
541 |
if (
|
|
|
542 |
$style->getProtection()->getLocked() !== Protection::PROTECTION_INHERIT ||
|
|
|
543 |
$style->getProtection()->getHidden() !== Protection::PROTECTION_INHERIT
|
|
|
544 |
) {
|
|
|
545 |
$objWriter->startElement('protection');
|
|
|
546 |
if (
|
|
|
547 |
($style->getProtection()->getLocked() !== null) &&
|
|
|
548 |
($style->getProtection()->getLocked() !== Protection::PROTECTION_INHERIT)
|
|
|
549 |
) {
|
|
|
550 |
$objWriter->writeAttribute('locked', ($style->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
|
|
|
551 |
}
|
|
|
552 |
if (
|
|
|
553 |
($style->getProtection()->getHidden() !== null) &&
|
|
|
554 |
($style->getProtection()->getHidden() !== Protection::PROTECTION_INHERIT)
|
|
|
555 |
) {
|
|
|
556 |
$objWriter->writeAttribute('hidden', ($style->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
|
|
|
557 |
}
|
|
|
558 |
$objWriter->endElement();
|
|
|
559 |
}
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
$objWriter->endElement();
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
/**
|
|
|
566 |
* Write BorderPr.
|
|
|
567 |
*
|
|
|
568 |
* @param string $name Element name
|
|
|
569 |
*/
|
|
|
570 |
private function writeBorderPr(XMLWriter $objWriter, $name, Border $border): void
|
|
|
571 |
{
|
|
|
572 |
// Write BorderPr
|
|
|
573 |
if ($border->getBorderStyle() === Border::BORDER_OMIT) {
|
|
|
574 |
return;
|
|
|
575 |
}
|
|
|
576 |
$objWriter->startElement($name);
|
|
|
577 |
if ($border->getBorderStyle() !== Border::BORDER_NONE) {
|
|
|
578 |
$objWriter->writeAttribute('style', $border->getBorderStyle());
|
|
|
579 |
|
|
|
580 |
// color
|
|
|
581 |
if ($border->getColor()->getARGB() !== null) {
|
|
|
582 |
$objWriter->startElement('color');
|
|
|
583 |
$objWriter->writeAttribute('rgb', $border->getColor()->getARGB());
|
|
|
584 |
$objWriter->endElement();
|
|
|
585 |
}
|
|
|
586 |
}
|
|
|
587 |
$objWriter->endElement();
|
|
|
588 |
}
|
|
|
589 |
|
|
|
590 |
/**
|
|
|
591 |
* Write NumberFormat.
|
|
|
592 |
*
|
|
|
593 |
* @param int $id Number Format identifier
|
|
|
594 |
*/
|
|
|
595 |
private function writeNumFmt(XMLWriter $objWriter, ?NumberFormat $numberFormat, $id = 0): void
|
|
|
596 |
{
|
|
|
597 |
// Translate formatcode
|
|
|
598 |
$formatCode = ($numberFormat === null) ? null : $numberFormat->getFormatCode();
|
|
|
599 |
|
|
|
600 |
// numFmt
|
|
|
601 |
if ($formatCode !== null) {
|
|
|
602 |
$objWriter->startElement('numFmt');
|
|
|
603 |
$objWriter->writeAttribute('numFmtId', (string) ($id + 164));
|
|
|
604 |
$objWriter->writeAttribute('formatCode', $formatCode);
|
|
|
605 |
$objWriter->endElement();
|
|
|
606 |
}
|
|
|
607 |
}
|
|
|
608 |
|
|
|
609 |
/**
|
|
|
610 |
* Get an array of all styles.
|
|
|
611 |
*
|
|
|
612 |
* @return \PhpOffice\PhpSpreadsheet\Style\Style[] All styles in PhpSpreadsheet
|
|
|
613 |
*/
|
|
|
614 |
public function allStyles(Spreadsheet $spreadsheet)
|
|
|
615 |
{
|
|
|
616 |
return $spreadsheet->getCellXfCollection();
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
/**
|
|
|
620 |
* Get an array of all conditional styles.
|
|
|
621 |
*
|
|
|
622 |
* @return Conditional[] All conditional styles in PhpSpreadsheet
|
|
|
623 |
*/
|
|
|
624 |
public function allConditionalStyles(Spreadsheet $spreadsheet)
|
|
|
625 |
{
|
|
|
626 |
// Get an array of all styles
|
|
|
627 |
$aStyles = [];
|
|
|
628 |
|
|
|
629 |
$sheetCount = $spreadsheet->getSheetCount();
|
|
|
630 |
for ($i = 0; $i < $sheetCount; ++$i) {
|
|
|
631 |
foreach ($spreadsheet->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
|
|
|
632 |
foreach ($conditionalStyles as $conditionalStyle) {
|
|
|
633 |
$aStyles[] = $conditionalStyle;
|
|
|
634 |
}
|
|
|
635 |
}
|
|
|
636 |
}
|
|
|
637 |
|
|
|
638 |
return $aStyles;
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
/**
|
|
|
642 |
* Get an array of all fills.
|
|
|
643 |
*
|
|
|
644 |
* @return Fill[] All fills in PhpSpreadsheet
|
|
|
645 |
*/
|
|
|
646 |
public function allFills(Spreadsheet $spreadsheet)
|
|
|
647 |
{
|
|
|
648 |
// Get an array of unique fills
|
|
|
649 |
$aFills = [];
|
|
|
650 |
|
|
|
651 |
// Two first fills are predefined
|
|
|
652 |
$fill0 = new Fill();
|
|
|
653 |
$fill0->setFillType(Fill::FILL_NONE);
|
|
|
654 |
$aFills[] = $fill0;
|
|
|
655 |
|
|
|
656 |
$fill1 = new Fill();
|
|
|
657 |
$fill1->setFillType(Fill::FILL_PATTERN_GRAY125);
|
|
|
658 |
$aFills[] = $fill1;
|
|
|
659 |
// The remaining fills
|
|
|
660 |
$aStyles = $this->allStyles($spreadsheet);
|
|
|
661 |
/** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
|
|
|
662 |
foreach ($aStyles as $style) {
|
|
|
663 |
if (!isset($aFills[$style->getFill()->getHashCode()])) {
|
|
|
664 |
$aFills[$style->getFill()->getHashCode()] = $style->getFill();
|
|
|
665 |
}
|
|
|
666 |
}
|
|
|
667 |
|
|
|
668 |
return $aFills;
|
|
|
669 |
}
|
|
|
670 |
|
|
|
671 |
/**
|
|
|
672 |
* Get an array of all fonts.
|
|
|
673 |
*
|
|
|
674 |
* @return Font[] All fonts in PhpSpreadsheet
|
|
|
675 |
*/
|
|
|
676 |
public function allFonts(Spreadsheet $spreadsheet)
|
|
|
677 |
{
|
|
|
678 |
// Get an array of unique fonts
|
|
|
679 |
$aFonts = [];
|
|
|
680 |
$aStyles = $this->allStyles($spreadsheet);
|
|
|
681 |
|
|
|
682 |
/** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
|
|
|
683 |
foreach ($aStyles as $style) {
|
|
|
684 |
if (!isset($aFonts[$style->getFont()->getHashCode()])) {
|
|
|
685 |
$aFonts[$style->getFont()->getHashCode()] = $style->getFont();
|
|
|
686 |
}
|
|
|
687 |
}
|
|
|
688 |
|
|
|
689 |
return $aFonts;
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
/**
|
|
|
693 |
* Get an array of all borders.
|
|
|
694 |
*
|
|
|
695 |
* @return Borders[] All borders in PhpSpreadsheet
|
|
|
696 |
*/
|
|
|
697 |
public function allBorders(Spreadsheet $spreadsheet)
|
|
|
698 |
{
|
|
|
699 |
// Get an array of unique borders
|
|
|
700 |
$aBorders = [];
|
|
|
701 |
$aStyles = $this->allStyles($spreadsheet);
|
|
|
702 |
|
|
|
703 |
/** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
|
|
|
704 |
foreach ($aStyles as $style) {
|
|
|
705 |
if (!isset($aBorders[$style->getBorders()->getHashCode()])) {
|
|
|
706 |
$aBorders[$style->getBorders()->getHashCode()] = $style->getBorders();
|
|
|
707 |
}
|
|
|
708 |
}
|
|
|
709 |
|
|
|
710 |
return $aBorders;
|
|
|
711 |
}
|
|
|
712 |
|
|
|
713 |
/**
|
|
|
714 |
* Get an array of all number formats.
|
|
|
715 |
*
|
|
|
716 |
* @return NumberFormat[] All number formats in PhpSpreadsheet
|
|
|
717 |
*/
|
|
|
718 |
public function allNumberFormats(Spreadsheet $spreadsheet)
|
|
|
719 |
{
|
|
|
720 |
// Get an array of unique number formats
|
|
|
721 |
$aNumFmts = [];
|
|
|
722 |
$aStyles = $this->allStyles($spreadsheet);
|
|
|
723 |
|
|
|
724 |
/** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
|
|
|
725 |
foreach ($aStyles as $style) {
|
|
|
726 |
if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !isset($aNumFmts[$style->getNumberFormat()->getHashCode()])) {
|
|
|
727 |
$aNumFmts[$style->getNumberFormat()->getHashCode()] = $style->getNumberFormat();
|
|
|
728 |
}
|
|
|
729 |
}
|
|
|
730 |
|
|
|
731 |
return $aNumFmts;
|
|
|
732 |
}
|
|
|
733 |
}
|