Style.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Style
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2013 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. // font
  67. $objWriter->startElement('font');
  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->getNumberFormat()->getBuiltInFormatCode() === false) {
  341. $objWriter->writeAttribute('numFmtId', (int)($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164) );
  342. } else {
  343. $objWriter->writeAttribute('numFmtId', (int)$pStyle->getNumberFormat()->getBuiltInFormatCode());
  344. }
  345. $objWriter->writeAttribute('fillId', (int)$this->getParentWriter()->getFillHashTable()->getIndexForHashCode($pStyle->getFill()->getHashCode()));
  346. $objWriter->writeAttribute('borderId', (int)$this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($pStyle->getBorders()->getHashCode()));
  347. // Apply styles?
  348. $objWriter->writeAttribute('applyFont', ($pPHPExcel->getDefaultStyle()->getFont()->getHashCode() != $pStyle->getFont()->getHashCode()) ? '1' : '0');
  349. $objWriter->writeAttribute('applyNumberFormat', ($pPHPExcel->getDefaultStyle()->getNumberFormat()->getHashCode() != $pStyle->getNumberFormat()->getHashCode()) ? '1' : '0');
  350. $objWriter->writeAttribute('applyFill', ($pPHPExcel->getDefaultStyle()->getFill()->getHashCode() != $pStyle->getFill()->getHashCode()) ? '1' : '0');
  351. $objWriter->writeAttribute('applyBorder', ($pPHPExcel->getDefaultStyle()->getBorders()->getHashCode() != $pStyle->getBorders()->getHashCode()) ? '1' : '0');
  352. $objWriter->writeAttribute('applyAlignment', ($pPHPExcel->getDefaultStyle()->getAlignment()->getHashCode() != $pStyle->getAlignment()->getHashCode()) ? '1' : '0');
  353. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  354. $objWriter->writeAttribute('applyProtection', 'true');
  355. }
  356. // alignment
  357. $objWriter->startElement('alignment');
  358. $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
  359. $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
  360. $textRotation = 0;
  361. if ($pStyle->getAlignment()->getTextRotation() >= 0) {
  362. $textRotation = $pStyle->getAlignment()->getTextRotation();
  363. } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
  364. $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
  365. }
  366. $objWriter->writeAttribute('textRotation', $textRotation);
  367. $objWriter->writeAttribute('wrapText', ($pStyle->getAlignment()->getWrapText() ? 'true' : 'false'));
  368. $objWriter->writeAttribute('shrinkToFit', ($pStyle->getAlignment()->getShrinkToFit() ? 'true' : 'false'));
  369. if ($pStyle->getAlignment()->getIndent() > 0) {
  370. $objWriter->writeAttribute('indent', $pStyle->getAlignment()->getIndent());
  371. }
  372. $objWriter->endElement();
  373. // protection
  374. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  375. $objWriter->startElement('protection');
  376. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  377. $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  378. }
  379. if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  380. $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  381. }
  382. $objWriter->endElement();
  383. }
  384. $objWriter->endElement();
  385. }
  386. /**
  387. * Write Cell Style Dxf
  388. *
  389. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  390. * @param PHPExcel_Style $pStyle Style
  391. * @throws PHPExcel_Writer_Exception
  392. */
  393. private function _writeCellStyleDxf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null)
  394. {
  395. // dxf
  396. $objWriter->startElement('dxf');
  397. // font
  398. $this->_writeFont($objWriter, $pStyle->getFont());
  399. // numFmt
  400. $this->_writeNumFmt($objWriter, $pStyle->getNumberFormat());
  401. // fill
  402. $this->_writeFill($objWriter, $pStyle->getFill());
  403. // alignment
  404. $objWriter->startElement('alignment');
  405. if ($pStyle->getAlignment()->getHorizontal() !== NULL) {
  406. $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
  407. }
  408. if ($pStyle->getAlignment()->getVertical() !== NULL) {
  409. $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
  410. }
  411. if ($pStyle->getAlignment()->getTextRotation() !== NULL) {
  412. $textRotation = 0;
  413. if ($pStyle->getAlignment()->getTextRotation() >= 0) {
  414. $textRotation = $pStyle->getAlignment()->getTextRotation();
  415. } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
  416. $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
  417. }
  418. $objWriter->writeAttribute('textRotation', $textRotation);
  419. }
  420. $objWriter->endElement();
  421. // border
  422. $this->_writeBorder($objWriter, $pStyle->getBorders());
  423. // protection
  424. if (($pStyle->getProtection()->getLocked() !== NULL) ||
  425. ($pStyle->getProtection()->getHidden() !== NULL)) {
  426. if ($pStyle->getProtection()->getLocked() !== PHPExcel_Style_Protection::PROTECTION_INHERIT ||
  427. $pStyle->getProtection()->getHidden() !== PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  428. $objWriter->startElement('protection');
  429. if (($pStyle->getProtection()->getLocked() !== NULL) &&
  430. ($pStyle->getProtection()->getLocked() !== PHPExcel_Style_Protection::PROTECTION_INHERIT)) {
  431. $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  432. }
  433. if (($pStyle->getProtection()->getHidden() !== NULL) &&
  434. ($pStyle->getProtection()->getHidden() !== PHPExcel_Style_Protection::PROTECTION_INHERIT)) {
  435. $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  436. }
  437. $objWriter->endElement();
  438. }
  439. }
  440. $objWriter->endElement();
  441. }
  442. /**
  443. * Write BorderPr
  444. *
  445. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  446. * @param string $pName Element name
  447. * @param PHPExcel_Style_Border $pBorder Border style
  448. * @throws PHPExcel_Writer_Exception
  449. */
  450. private function _writeBorderPr(PHPExcel_Shared_XMLWriter $objWriter = null, $pName = 'left', PHPExcel_Style_Border $pBorder = null)
  451. {
  452. // Write BorderPr
  453. if ($pBorder->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  454. $objWriter->startElement($pName);
  455. $objWriter->writeAttribute('style', $pBorder->getBorderStyle());
  456. // color
  457. $objWriter->startElement('color');
  458. $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB());
  459. $objWriter->endElement();
  460. $objWriter->endElement();
  461. }
  462. }
  463. /**
  464. * Write NumberFormat
  465. *
  466. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  467. * @param PHPExcel_Style_NumberFormat $pNumberFormat Number Format
  468. * @param int $pId Number Format identifier
  469. * @throws PHPExcel_Writer_Exception
  470. */
  471. private function _writeNumFmt(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_NumberFormat $pNumberFormat = null, $pId = 0)
  472. {
  473. // Translate formatcode
  474. $formatCode = $pNumberFormat->getFormatCode();
  475. // numFmt
  476. if ($formatCode !== NULL) {
  477. $objWriter->startElement('numFmt');
  478. $objWriter->writeAttribute('numFmtId', ($pId + 164));
  479. $objWriter->writeAttribute('formatCode', $formatCode);
  480. $objWriter->endElement();
  481. }
  482. }
  483. /**
  484. * Get an array of all styles
  485. *
  486. * @param PHPExcel $pPHPExcel
  487. * @return PHPExcel_Style[] All styles in PHPExcel
  488. * @throws PHPExcel_Writer_Exception
  489. */
  490. public function allStyles(PHPExcel $pPHPExcel = null)
  491. {
  492. $aStyles = $pPHPExcel->getCellXfCollection();
  493. return $aStyles;
  494. }
  495. /**
  496. * Get an array of all conditional styles
  497. *
  498. * @param PHPExcel $pPHPExcel
  499. * @return PHPExcel_Style_Conditional[] All conditional styles in PHPExcel
  500. * @throws PHPExcel_Writer_Exception
  501. */
  502. public function allConditionalStyles(PHPExcel $pPHPExcel = null)
  503. {
  504. // Get an array of all styles
  505. $aStyles = array();
  506. $sheetCount = $pPHPExcel->getSheetCount();
  507. for ($i = 0; $i < $sheetCount; ++$i) {
  508. foreach ($pPHPExcel->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
  509. foreach ($conditionalStyles as $conditionalStyle) {
  510. $aStyles[] = $conditionalStyle;
  511. }
  512. }
  513. }
  514. return $aStyles;
  515. }
  516. /**
  517. * Get an array of all fills
  518. *
  519. * @param PHPExcel $pPHPExcel
  520. * @return PHPExcel_Style_Fill[] All fills in PHPExcel
  521. * @throws PHPExcel_Writer_Exception
  522. */
  523. public function allFills(PHPExcel $pPHPExcel = null)
  524. {
  525. // Get an array of unique fills
  526. $aFills = array();
  527. // Two first fills are predefined
  528. $fill0 = new PHPExcel_Style_Fill();
  529. $fill0->setFillType(PHPExcel_Style_Fill::FILL_NONE);
  530. $aFills[] = $fill0;
  531. $fill1 = new PHPExcel_Style_Fill();
  532. $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125);
  533. $aFills[] = $fill1;
  534. // The remaining fills
  535. $aStyles = $this->allStyles($pPHPExcel);
  536. foreach ($aStyles as $style) {
  537. if (!array_key_exists($style->getFill()->getHashCode(), $aFills)) {
  538. $aFills[ $style->getFill()->getHashCode() ] = $style->getFill();
  539. }
  540. }
  541. return $aFills;
  542. }
  543. /**
  544. * Get an array of all font
  545. *
  546. * @param PHPExcel $pPHPExcel
  547. * @return PHPExcel_Style_Font[] All font in PHPExcel
  548. * @throws PHPExcel_Writer_Exception
  549. */
  550. public function allFonts(PHPExcel $pPHPExcel = null)
  551. {
  552. // Get an array of unique font
  553. $aFonts = array();
  554. $aStyles = $this->allStyles($pPHPExcel);
  555. foreach ($aStyles as $style) {
  556. if (!array_key_exists($style->getFont()->getHashCode(), $aFonts)) {
  557. $aFonts[ $style->getFont()->getHashCode() ] = $style->getFont();
  558. }
  559. }
  560. return $aFonts;
  561. }
  562. /**
  563. * Get an array of all borders
  564. *
  565. * @param PHPExcel $pPHPExcel
  566. * @return PHPExcel_Style_Borders[] All borders in PHPExcel
  567. * @throws PHPExcel_Writer_Exception
  568. */
  569. public function allBorders(PHPExcel $pPHPExcel = null)
  570. {
  571. // Get an array of unique borders
  572. $aBorders = array();
  573. $aStyles = $this->allStyles($pPHPExcel);
  574. foreach ($aStyles as $style) {
  575. if (!array_key_exists($style->getBorders()->getHashCode(), $aBorders)) {
  576. $aBorders[ $style->getBorders()->getHashCode() ] = $style->getBorders();
  577. }
  578. }
  579. return $aBorders;
  580. }
  581. /**
  582. * Get an array of all number formats
  583. *
  584. * @param PHPExcel $pPHPExcel
  585. * @return PHPExcel_Style_NumberFormat[] All number formats in PHPExcel
  586. * @throws PHPExcel_Writer_Exception
  587. */
  588. public function allNumberFormats(PHPExcel $pPHPExcel = null)
  589. {
  590. // Get an array of unique number formats
  591. $aNumFmts = array();
  592. $aStyles = $this->allStyles($pPHPExcel);
  593. foreach ($aStyles as $style) {
  594. if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !array_key_exists($style->getNumberFormat()->getHashCode(), $aNumFmts)) {
  595. $aNumFmts[ $style->getNumberFormat()->getHashCode() ] = $style->getNumberFormat();
  596. }
  597. }
  598. return $aNumFmts;
  599. }
  600. }