[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Writer/Excel2007/ -> Style.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1