Style.php 27 KB

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