Style.php 24 KB

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