1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28:
29: 30: 31: 32: 33: 34: 35:
36: class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPart
37: {
38: 39: 40: 41: 42: 43: 44:
45: public function writeStyles(PHPExcel $pPHPExcel = null)
46: {
47:
48: $objWriter = null;
49: if ($this->getParentWriter()->getUseDiskCaching()) {
50: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
51: } else {
52: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
53: }
54:
55:
56: $objWriter->startDocument('1.0','UTF-8','yes');
57:
58:
59: $objWriter->startElement('styleSheet');
60: $objWriter->writeAttribute('xml:space', 'preserve');
61: $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
62:
63:
64: $objWriter->startElement('numFmts');
65: $objWriter->writeAttribute('count', $this->getParentWriter()->getNumFmtHashTable()->count());
66:
67:
68: for ($i = 0; $i < $this->getParentWriter()->getNumFmtHashTable()->count(); ++$i) {
69: $this->_writeNumFmt($objWriter, $this->getParentWriter()->getNumFmtHashTable()->getByIndex($i), $i);
70: }
71:
72: $objWriter->endElement();
73:
74:
75: $objWriter->startElement('fonts');
76: $objWriter->writeAttribute('count', $this->getParentWriter()->getFontHashTable()->count());
77:
78:
79: for ($i = 0; $i < $this->getParentWriter()->getFontHashTable()->count(); ++$i) {
80: $this->_writeFont($objWriter, $this->getParentWriter()->getFontHashTable()->getByIndex($i));
81: }
82:
83: $objWriter->endElement();
84:
85:
86: $objWriter->startElement('fills');
87: $objWriter->writeAttribute('count', $this->getParentWriter()->getFillHashTable()->count());
88:
89:
90: for ($i = 0; $i < $this->getParentWriter()->getFillHashTable()->count(); ++$i) {
91: $this->_writeFill($objWriter, $this->getParentWriter()->getFillHashTable()->getByIndex($i));
92: }
93:
94: $objWriter->endElement();
95:
96:
97: $objWriter->startElement('borders');
98: $objWriter->writeAttribute('count', $this->getParentWriter()->getBordersHashTable()->count());
99:
100:
101: for ($i = 0; $i < $this->getParentWriter()->getBordersHashTable()->count(); ++$i) {
102: $this->_writeBorder($objWriter, $this->getParentWriter()->getBordersHashTable()->getByIndex($i));
103: }
104:
105: $objWriter->endElement();
106:
107:
108: $objWriter->startElement('cellStyleXfs');
109: $objWriter->writeAttribute('count', 1);
110:
111:
112: $objWriter->startElement('xf');
113: $objWriter->writeAttribute('numFmtId', 0);
114: $objWriter->writeAttribute('fontId', 0);
115: $objWriter->writeAttribute('fillId', 0);
116: $objWriter->writeAttribute('borderId', 0);
117: $objWriter->endElement();
118:
119: $objWriter->endElement();
120:
121:
122: $objWriter->startElement('cellXfs');
123: $objWriter->writeAttribute('count', count($pPHPExcel->getCellXfCollection()));
124:
125:
126: foreach ($pPHPExcel->getCellXfCollection() as $cellXf) {
127: $this->_writeCellStyleXf($objWriter, $cellXf, $pPHPExcel);
128: }
129:
130: $objWriter->endElement();
131:
132:
133: $objWriter->startElement('cellStyles');
134: $objWriter->writeAttribute('count', 1);
135:
136:
137: $objWriter->startElement('cellStyle');
138: $objWriter->writeAttribute('name', 'Normal');
139: $objWriter->writeAttribute('xfId', 0);
140: $objWriter->writeAttribute('builtinId', 0);
141: $objWriter->endElement();
142:
143: $objWriter->endElement();
144:
145:
146: $objWriter->startElement('dxfs');
147: $objWriter->writeAttribute('count', $this->getParentWriter()->getStylesConditionalHashTable()->count());
148:
149:
150: for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) {
151: $this->_writeCellStyleDxf($objWriter, $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i)->getStyle());
152: }
153:
154: $objWriter->endElement();
155:
156:
157: $objWriter->startElement('tableStyles');
158: $objWriter->writeAttribute('defaultTableStyle', 'TableStyleMedium9');
159: $objWriter->writeAttribute('defaultPivotStyle', 'PivotTableStyle1');
160: $objWriter->endElement();
161:
162: $objWriter->endElement();
163:
164:
165: return $objWriter->getData();
166: }
167:
168: 169: 170: 171: 172: 173: 174:
175: private function _writeFill(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Fill $pFill = null)
176: {
177:
178: if ($pFill->getFillType() === PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR ||
179: $pFill->getFillType() === PHPExcel_Style_Fill::FILL_GRADIENT_PATH) {
180:
181: $this->_writeGradientFill($objWriter, $pFill);
182: } elseif($pFill->getFillType() !== NULL) {
183:
184: $this->_writePatternFill($objWriter, $pFill);
185: }
186: }
187:
188: 189: 190: 191: 192: 193: 194:
195: private function _writeGradientFill(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Fill $pFill = null)
196: {
197:
198: $objWriter->startElement('fill');
199:
200:
201: $objWriter->startElement('gradientFill');
202: $objWriter->writeAttribute('type', $pFill->getFillType());
203: $objWriter->writeAttribute('degree', $pFill->getRotation());
204:
205:
206: $objWriter->startElement('stop');
207: $objWriter->writeAttribute('position', '0');
208:
209:
210: $objWriter->startElement('color');
211: $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
212: $objWriter->endElement();
213:
214: $objWriter->endElement();
215:
216:
217: $objWriter->startElement('stop');
218: $objWriter->writeAttribute('position', '1');
219:
220:
221: $objWriter->startElement('color');
222: $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
223: $objWriter->endElement();
224:
225: $objWriter->endElement();
226:
227: $objWriter->endElement();
228:
229: $objWriter->endElement();
230: }
231:
232: 233: 234: 235: 236: 237: 238:
239: private function _writePatternFill(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Fill $pFill = null)
240: {
241:
242: $objWriter->startElement('fill');
243:
244:
245: $objWriter->startElement('patternFill');
246: $objWriter->writeAttribute('patternType', $pFill->getFillType());
247:
248: if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_NONE) {
249:
250: if ($pFill->getStartColor()->getARGB()) {
251: $objWriter->startElement('fgColor');
252: $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
253: $objWriter->endElement();
254: }
255: }
256: if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_NONE) {
257:
258: if ($pFill->getEndColor()->getARGB()) {
259: $objWriter->startElement('bgColor');
260: $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
261: $objWriter->endElement();
262: }
263: }
264:
265: $objWriter->endElement();
266:
267: $objWriter->endElement();
268: }
269:
270: 271: 272: 273: 274: 275: 276:
277: private function _writeFont(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Font $pFont = null)
278: {
279:
280: $objWriter->startElement('font');
281:
282:
283:
284:
285:
286:
287:
288: if ($pFont->getBold() !== NULL) {
289: $objWriter->startElement('b');
290: $objWriter->writeAttribute('val', $pFont->getBold() ? '1' : '0');
291: $objWriter->endElement();
292: }
293:
294:
295: if ($pFont->getItalic() !== NULL) {
296: $objWriter->startElement('i');
297: $objWriter->writeAttribute('val', $pFont->getItalic() ? '1' : '0');
298: $objWriter->endElement();
299: }
300:
301:
302: if ($pFont->getStrikethrough() !== NULL) {
303: $objWriter->startElement('strike');
304: $objWriter->writeAttribute('val', $pFont->getStrikethrough() ? '1' : '0');
305: $objWriter->endElement();
306: }
307:
308:
309: if ($pFont->getUnderline() !== NULL) {
310: $objWriter->startElement('u');
311: $objWriter->writeAttribute('val', $pFont->getUnderline());
312: $objWriter->endElement();
313: }
314:
315:
316: if ($pFont->getSuperScript() === TRUE || $pFont->getSubScript() === TRUE) {
317: $objWriter->startElement('vertAlign');
318: if ($pFont->getSuperScript() === TRUE) {
319: $objWriter->writeAttribute('val', 'superscript');
320: } else if ($pFont->getSubScript() === TRUE) {
321: $objWriter->writeAttribute('val', 'subscript');
322: }
323: $objWriter->endElement();
324: }
325:
326:
327: if ($pFont->getSize() !== NULL) {
328: $objWriter->startElement('sz');
329: $objWriter->writeAttribute('val', $pFont->getSize());
330: $objWriter->endElement();
331: }
332:
333:
334: if ($pFont->getColor()->getARGB() !== NULL) {
335: $objWriter->startElement('color');
336: $objWriter->writeAttribute('rgb', $pFont->getColor()->getARGB());
337: $objWriter->endElement();
338: }
339:
340:
341: if ($pFont->getName() !== NULL) {
342: $objWriter->startElement('name');
343: $objWriter->writeAttribute('val', $pFont->getName());
344: $objWriter->endElement();
345: }
346:
347: $objWriter->endElement();
348: }
349:
350: 351: 352: 353: 354: 355: 356:
357: private function _writeBorder(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Borders $pBorders = null)
358: {
359:
360: $objWriter->startElement('border');
361:
362: switch ($pBorders->getDiagonalDirection()) {
363: case PHPExcel_Style_Borders::DIAGONAL_UP:
364: $objWriter->writeAttribute('diagonalUp', 'true');
365: $objWriter->writeAttribute('diagonalDown', 'false');
366: break;
367: case PHPExcel_Style_Borders::DIAGONAL_DOWN:
368: $objWriter->writeAttribute('diagonalUp', 'false');
369: $objWriter->writeAttribute('diagonalDown', 'true');
370: break;
371: case PHPExcel_Style_Borders::DIAGONAL_BOTH:
372: $objWriter->writeAttribute('diagonalUp', 'true');
373: $objWriter->writeAttribute('diagonalDown', 'true');
374: break;
375: }
376:
377:
378: $this->_writeBorderPr($objWriter, 'left', $pBorders->getLeft());
379: $this->_writeBorderPr($objWriter, 'right', $pBorders->getRight());
380: $this->_writeBorderPr($objWriter, 'top', $pBorders->getTop());
381: $this->_writeBorderPr($objWriter, 'bottom', $pBorders->getBottom());
382: $this->_writeBorderPr($objWriter, 'diagonal', $pBorders->getDiagonal());
383: $objWriter->endElement();
384: }
385:
386: 387: 388: 389: 390: 391: 392: 393:
394: private function _writeCellStyleXf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null, PHPExcel $pPHPExcel = null)
395: {
396:
397: $objWriter->startElement('xf');
398: $objWriter->writeAttribute('xfId', 0);
399: $objWriter->writeAttribute('fontId', (int)$this->getParentWriter()->getFontHashTable()->getIndexForHashCode($pStyle->getFont()->getHashCode()));
400: if ($pStyle->getQuotePrefix()) {
401: $objWriter->writeAttribute('quotePrefix', 1);
402: }
403:
404: if ($pStyle->getNumberFormat()->getBuiltInFormatCode() === false) {
405: $objWriter->writeAttribute('numFmtId', (int)($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164) );
406: } else {
407: $objWriter->writeAttribute('numFmtId', (int)$pStyle->getNumberFormat()->getBuiltInFormatCode());
408: }
409:
410: $objWriter->writeAttribute('fillId', (int)$this->getParentWriter()->getFillHashTable()->getIndexForHashCode($pStyle->getFill()->getHashCode()));
411: $objWriter->writeAttribute('borderId', (int)$this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($pStyle->getBorders()->getHashCode()));
412:
413:
414: $objWriter->writeAttribute('applyFont', ($pPHPExcel->getDefaultStyle()->getFont()->getHashCode() != $pStyle->getFont()->getHashCode()) ? '1' : '0');
415: $objWriter->writeAttribute('applyNumberFormat', ($pPHPExcel->getDefaultStyle()->getNumberFormat()->getHashCode() != $pStyle->getNumberFormat()->getHashCode()) ? '1' : '0');
416: $objWriter->writeAttribute('applyFill', ($pPHPExcel->getDefaultStyle()->getFill()->getHashCode() != $pStyle->getFill()->getHashCode()) ? '1' : '0');
417: $objWriter->writeAttribute('applyBorder', ($pPHPExcel->getDefaultStyle()->getBorders()->getHashCode() != $pStyle->getBorders()->getHashCode()) ? '1' : '0');
418: $objWriter->writeAttribute('applyAlignment', ($pPHPExcel->getDefaultStyle()->getAlignment()->getHashCode() != $pStyle->getAlignment()->getHashCode()) ? '1' : '0');
419: if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
420: $objWriter->writeAttribute('applyProtection', 'true');
421: }
422:
423:
424: $objWriter->startElement('alignment');
425: $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
426: $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
427:
428: $textRotation = 0;
429: if ($pStyle->getAlignment()->getTextRotation() >= 0) {
430: $textRotation = $pStyle->getAlignment()->getTextRotation();
431: } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
432: $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
433: }
434: $objWriter->writeAttribute('textRotation', $textRotation);
435:
436: $objWriter->writeAttribute('wrapText', ($pStyle->getAlignment()->getWrapText() ? 'true' : 'false'));
437: $objWriter->writeAttribute('shrinkToFit', ($pStyle->getAlignment()->getShrinkToFit() ? 'true' : 'false'));
438:
439: if ($pStyle->getAlignment()->getIndent() > 0) {
440: $objWriter->writeAttribute('indent', $pStyle->getAlignment()->getIndent());
441: }
442: $objWriter->endElement();
443:
444:
445: if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
446: $objWriter->startElement('protection');
447: if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
448: $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
449: }
450: if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
451: $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
452: }
453: $objWriter->endElement();
454: }
455:
456: $objWriter->endElement();
457: }
458:
459: 460: 461: 462: 463: 464: 465:
466: private function _writeCellStyleDxf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null)
467: {
468:
469: $objWriter->startElement('dxf');
470:
471:
472: $this->_writeFont($objWriter, $pStyle->getFont());
473:
474:
475: $this->_writeNumFmt($objWriter, $pStyle->getNumberFormat());
476:
477:
478: $this->_writeFill($objWriter, $pStyle->getFill());
479:
480:
481: $objWriter->startElement('alignment');
482: if ($pStyle->getAlignment()->getHorizontal() !== NULL) {
483: $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
484: }
485: if ($pStyle->getAlignment()->getVertical() !== NULL) {
486: $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
487: }
488:
489: if ($pStyle->getAlignment()->getTextRotation() !== NULL) {
490: $textRotation = 0;
491: if ($pStyle->getAlignment()->getTextRotation() >= 0) {
492: $textRotation = $pStyle->getAlignment()->getTextRotation();
493: } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
494: $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
495: }
496: $objWriter->writeAttribute('textRotation', $textRotation);
497: }
498: $objWriter->endElement();
499:
500:
501: $this->_writeBorder($objWriter, $pStyle->getBorders());
502:
503:
504: if (($pStyle->getProtection()->getLocked() !== NULL) ||
505: ($pStyle->getProtection()->getHidden() !== NULL)) {
506: if ($pStyle->getProtection()->getLocked() !== PHPExcel_Style_Protection::PROTECTION_INHERIT ||
507: $pStyle->getProtection()->getHidden() !== PHPExcel_Style_Protection::PROTECTION_INHERIT) {
508: $objWriter->startElement('protection');
509: if (($pStyle->getProtection()->getLocked() !== NULL) &&
510: ($pStyle->getProtection()->getLocked() !== PHPExcel_Style_Protection::PROTECTION_INHERIT)) {
511: $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
512: }
513: if (($pStyle->getProtection()->getHidden() !== NULL) &&
514: ($pStyle->getProtection()->getHidden() !== PHPExcel_Style_Protection::PROTECTION_INHERIT)) {
515: $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
516: }
517: $objWriter->endElement();
518: }
519: }
520:
521: $objWriter->endElement();
522: }
523:
524: 525: 526: 527: 528: 529: 530: 531:
532: private function _writeBorderPr(PHPExcel_Shared_XMLWriter $objWriter = null, $pName = 'left', PHPExcel_Style_Border $pBorder = null)
533: {
534:
535: if ($pBorder->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
536: $objWriter->startElement($pName);
537: $objWriter->writeAttribute('style', $pBorder->getBorderStyle());
538:
539:
540: $objWriter->startElement('color');
541: $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB());
542: $objWriter->endElement();
543:
544: $objWriter->endElement();
545: }
546: }
547:
548: 549: 550: 551: 552: 553: 554: 555:
556: private function _writeNumFmt(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_NumberFormat $pNumberFormat = null, $pId = 0)
557: {
558:
559: $formatCode = $pNumberFormat->getFormatCode();
560:
561:
562: if ($formatCode !== NULL) {
563: $objWriter->startElement('numFmt');
564: $objWriter->writeAttribute('numFmtId', ($pId + 164));
565: $objWriter->writeAttribute('formatCode', $formatCode);
566: $objWriter->endElement();
567: }
568: }
569:
570: 571: 572: 573: 574: 575: 576:
577: public function allStyles(PHPExcel $pPHPExcel = null)
578: {
579: $aStyles = $pPHPExcel->getCellXfCollection();
580:
581: return $aStyles;
582: }
583:
584: 585: 586: 587: 588: 589: 590:
591: public function allConditionalStyles(PHPExcel $pPHPExcel = null)
592: {
593:
594: $aStyles = array();
595:
596: $sheetCount = $pPHPExcel->getSheetCount();
597: for ($i = 0; $i < $sheetCount; ++$i) {
598: foreach ($pPHPExcel->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
599: foreach ($conditionalStyles as $conditionalStyle) {
600: $aStyles[] = $conditionalStyle;
601: }
602: }
603: }
604:
605: return $aStyles;
606: }
607:
608: 609: 610: 611: 612: 613: 614:
615: public function allFills(PHPExcel $pPHPExcel = null)
616: {
617:
618: $aFills = array();
619:
620:
621: $fill0 = new PHPExcel_Style_Fill();
622: $fill0->setFillType(PHPExcel_Style_Fill::FILL_NONE);
623: $aFills[] = $fill0;
624:
625: $fill1 = new PHPExcel_Style_Fill();
626: $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125);
627: $aFills[] = $fill1;
628:
629: $aStyles = $this->allStyles($pPHPExcel);
630: foreach ($aStyles as $style) {
631: if (!array_key_exists($style->getFill()->getHashCode(), $aFills)) {
632: $aFills[ $style->getFill()->getHashCode() ] = $style->getFill();
633: }
634: }
635:
636: return $aFills;
637: }
638:
639: 640: 641: 642: 643: 644: 645:
646: public function allFonts(PHPExcel $pPHPExcel = null)
647: {
648:
649: $aFonts = array();
650: $aStyles = $this->allStyles($pPHPExcel);
651:
652: foreach ($aStyles as $style) {
653: if (!array_key_exists($style->getFont()->getHashCode(), $aFonts)) {
654: $aFonts[ $style->getFont()->getHashCode() ] = $style->getFont();
655: }
656: }
657:
658: return $aFonts;
659: }
660:
661: 662: 663: 664: 665: 666: 667:
668: public function allBorders(PHPExcel $pPHPExcel = null)
669: {
670:
671: $aBorders = array();
672: $aStyles = $this->allStyles($pPHPExcel);
673:
674: foreach ($aStyles as $style) {
675: if (!array_key_exists($style->getBorders()->getHashCode(), $aBorders)) {
676: $aBorders[ $style->getBorders()->getHashCode() ] = $style->getBorders();
677: }
678: }
679:
680: return $aBorders;
681: }
682:
683: 684: 685: 686: 687: 688: 689:
690: public function allNumberFormats(PHPExcel $pPHPExcel = null)
691: {
692:
693: $aNumFmts = array();
694: $aStyles = $this->allStyles($pPHPExcel);
695:
696: foreach ($aStyles as $style) {
697: if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !array_key_exists($style->getNumberFormat()->getHashCode(), $aNumFmts)) {
698: $aNumFmts[ $style->getNumberFormat()->getHashCode() ] = $style->getNumberFormat();
699: }
700: }
701:
702: return $aNumFmts;
703: }
704: }
705: