Style.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Style
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2011 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 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 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. } else {
  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 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 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. // fgColor
  204. $objWriter->startElement('fgColor');
  205. $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
  206. $objWriter->endElement();
  207. // bgColor
  208. $objWriter->startElement('bgColor');
  209. $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
  210. $objWriter->endElement();
  211. $objWriter->endElement();
  212. $objWriter->endElement();
  213. }
  214. /**
  215. * Write Font
  216. *
  217. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  218. * @param PHPExcel_Style_Font $pFont Font style
  219. * @throws Exception
  220. */
  221. private function _writeFont(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Font $pFont = null)
  222. {
  223. // font
  224. $objWriter->startElement('font');
  225. // Name
  226. $objWriter->startElement('name');
  227. $objWriter->writeAttribute('val', $pFont->getName());
  228. $objWriter->endElement();
  229. // Size
  230. $objWriter->startElement('sz');
  231. $objWriter->writeAttribute('val', $pFont->getSize());
  232. $objWriter->endElement();
  233. // Bold. We explicitly write this element also when false (like MS Office Excel 2007 does
  234. // for conditional formatting). Otherwise it will apparently not be picked up in conditional
  235. // formatting style dialog
  236. $objWriter->startElement('b');
  237. $objWriter->writeAttribute('val', $pFont->getBold() ? '1' : '0');
  238. $objWriter->endElement();
  239. // Italic
  240. $objWriter->startElement('i');
  241. $objWriter->writeAttribute('val', $pFont->getItalic() ? '1' : '0');
  242. $objWriter->endElement();
  243. // Superscript / subscript
  244. if ($pFont->getSuperScript() || $pFont->getSubScript()) {
  245. $objWriter->startElement('vertAlign');
  246. if ($pFont->getSuperScript()) {
  247. $objWriter->writeAttribute('val', 'superscript');
  248. } else if ($pFont->getSubScript()) {
  249. $objWriter->writeAttribute('val', 'subscript');
  250. }
  251. $objWriter->endElement();
  252. }
  253. // Underline
  254. $objWriter->startElement('u');
  255. $objWriter->writeAttribute('val', $pFont->getUnderline());
  256. $objWriter->endElement();
  257. // Strikethrough
  258. $objWriter->startElement('strike');
  259. $objWriter->writeAttribute('val', $pFont->getStrikethrough() ? '1' : '0');
  260. $objWriter->endElement();
  261. // Foreground color
  262. $objWriter->startElement('color');
  263. $objWriter->writeAttribute('rgb', $pFont->getColor()->getARGB());
  264. $objWriter->endElement();
  265. $objWriter->endElement();
  266. }
  267. /**
  268. * Write Border
  269. *
  270. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  271. * @param PHPExcel_Style_Borders $pBorders Borders style
  272. * @throws Exception
  273. */
  274. private function _writeBorder(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Borders $pBorders = null)
  275. {
  276. // Write border
  277. $objWriter->startElement('border');
  278. // Diagonal?
  279. switch ($pBorders->getDiagonalDirection()) {
  280. case PHPExcel_Style_Borders::DIAGONAL_UP:
  281. $objWriter->writeAttribute('diagonalUp', 'true');
  282. $objWriter->writeAttribute('diagonalDown', 'false');
  283. break;
  284. case PHPExcel_Style_Borders::DIAGONAL_DOWN:
  285. $objWriter->writeAttribute('diagonalUp', 'false');
  286. $objWriter->writeAttribute('diagonalDown', 'true');
  287. break;
  288. case PHPExcel_Style_Borders::DIAGONAL_BOTH:
  289. $objWriter->writeAttribute('diagonalUp', 'true');
  290. $objWriter->writeAttribute('diagonalDown', 'true');
  291. break;
  292. }
  293. // BorderPr
  294. $this->_writeBorderPr($objWriter, 'left', $pBorders->getLeft());
  295. $this->_writeBorderPr($objWriter, 'right', $pBorders->getRight());
  296. $this->_writeBorderPr($objWriter, 'top', $pBorders->getTop());
  297. $this->_writeBorderPr($objWriter, 'bottom', $pBorders->getBottom());
  298. $this->_writeBorderPr($objWriter, 'diagonal', $pBorders->getDiagonal());
  299. $objWriter->endElement();
  300. }
  301. /**
  302. * Write Cell Style Xf
  303. *
  304. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  305. * @param PHPExcel_Style $pStyle Style
  306. * @param PHPExcel $pPHPExcel Workbook
  307. * @throws Exception
  308. */
  309. private function _writeCellStyleXf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null, PHPExcel $pPHPExcel = null)
  310. {
  311. // xf
  312. $objWriter->startElement('xf');
  313. $objWriter->writeAttribute('xfId', 0);
  314. $objWriter->writeAttribute('fontId', (int)$this->getParentWriter()->getFontHashTable()->getIndexForHashCode($pStyle->getFont()->getHashCode()));
  315. if ($pStyle->getNumberFormat()->getBuiltInFormatCode() === false) {
  316. $objWriter->writeAttribute('numFmtId', (int)($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164) );
  317. } else {
  318. $objWriter->writeAttribute('numFmtId', (int)$pStyle->getNumberFormat()->getBuiltInFormatCode());
  319. }
  320. $objWriter->writeAttribute('fillId', (int)$this->getParentWriter()->getFillHashTable()->getIndexForHashCode($pStyle->getFill()->getHashCode()));
  321. $objWriter->writeAttribute('borderId', (int)$this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($pStyle->getBorders()->getHashCode()));
  322. // Apply styles?
  323. $objWriter->writeAttribute('applyFont', ($pPHPExcel->getDefaultStyle()->getFont()->getHashCode() != $pStyle->getFont()->getHashCode()) ? '1' : '0');
  324. $objWriter->writeAttribute('applyNumberFormat', ($pPHPExcel->getDefaultStyle()->getNumberFormat()->getHashCode() != $pStyle->getNumberFormat()->getHashCode()) ? '1' : '0');
  325. $objWriter->writeAttribute('applyFill', ($pPHPExcel->getDefaultStyle()->getFill()->getHashCode() != $pStyle->getFill()->getHashCode()) ? '1' : '0');
  326. $objWriter->writeAttribute('applyBorder', ($pPHPExcel->getDefaultStyle()->getBorders()->getHashCode() != $pStyle->getBorders()->getHashCode()) ? '1' : '0');
  327. $objWriter->writeAttribute('applyAlignment', ($pPHPExcel->getDefaultStyle()->getAlignment()->getHashCode() != $pStyle->getAlignment()->getHashCode()) ? '1' : '0');
  328. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  329. $objWriter->writeAttribute('applyProtection', 'true');
  330. }
  331. // alignment
  332. $objWriter->startElement('alignment');
  333. $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
  334. $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
  335. $textRotation = 0;
  336. if ($pStyle->getAlignment()->getTextRotation() >= 0) {
  337. $textRotation = $pStyle->getAlignment()->getTextRotation();
  338. } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
  339. $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
  340. }
  341. $objWriter->writeAttribute('textRotation', $textRotation);
  342. $objWriter->writeAttribute('wrapText', ($pStyle->getAlignment()->getWrapText() ? 'true' : 'false'));
  343. $objWriter->writeAttribute('shrinkToFit', ($pStyle->getAlignment()->getShrinkToFit() ? 'true' : 'false'));
  344. if ($pStyle->getAlignment()->getIndent() > 0) {
  345. $objWriter->writeAttribute('indent', $pStyle->getAlignment()->getIndent());
  346. }
  347. $objWriter->endElement();
  348. // protection
  349. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  350. $objWriter->startElement('protection');
  351. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  352. $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  353. }
  354. if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  355. $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  356. }
  357. $objWriter->endElement();
  358. }
  359. $objWriter->endElement();
  360. }
  361. /**
  362. * Write Cell Style Dxf
  363. *
  364. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  365. * @param PHPExcel_Style $pStyle Style
  366. * @throws Exception
  367. */
  368. private function _writeCellStyleDxf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null)
  369. {
  370. // dxf
  371. $objWriter->startElement('dxf');
  372. // font
  373. $this->_writeFont($objWriter, $pStyle->getFont());
  374. // numFmt
  375. $this->_writeNumFmt($objWriter, $pStyle->getNumberFormat());
  376. // fill
  377. $this->_writeFill($objWriter, $pStyle->getFill());
  378. // alignment
  379. $objWriter->startElement('alignment');
  380. $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
  381. $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
  382. $textRotation = 0;
  383. if ($pStyle->getAlignment()->getTextRotation() >= 0) {
  384. $textRotation = $pStyle->getAlignment()->getTextRotation();
  385. } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
  386. $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
  387. }
  388. $objWriter->writeAttribute('textRotation', $textRotation);
  389. $objWriter->endElement();
  390. // border
  391. $this->_writeBorder($objWriter, $pStyle->getBorders());
  392. // protection
  393. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  394. $objWriter->startElement('protection');
  395. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  396. $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  397. }
  398. if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  399. $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  400. }
  401. $objWriter->endElement();
  402. }
  403. $objWriter->endElement();
  404. }
  405. /**
  406. * Write BorderPr
  407. *
  408. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  409. * @param string $pName Element name
  410. * @param PHPExcel_Style_Border $pBorder Border style
  411. * @throws Exception
  412. */
  413. private function _writeBorderPr(PHPExcel_Shared_XMLWriter $objWriter = null, $pName = 'left', PHPExcel_Style_Border $pBorder = null)
  414. {
  415. // Write BorderPr
  416. if ($pBorder->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  417. $objWriter->startElement($pName);
  418. $objWriter->writeAttribute('style', $pBorder->getBorderStyle());
  419. // color
  420. $objWriter->startElement('color');
  421. $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB());
  422. $objWriter->endElement();
  423. $objWriter->endElement();
  424. }
  425. }
  426. /**
  427. * Write NumberFormat
  428. *
  429. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  430. * @param PHPExcel_Style_NumberFormat $pNumberFormat Number Format
  431. * @param int $pId Number Format identifier
  432. * @throws Exception
  433. */
  434. private function _writeNumFmt(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_NumberFormat $pNumberFormat = null, $pId = 0)
  435. {
  436. // Translate formatcode
  437. $formatCode = $pNumberFormat->getFormatCode();
  438. // numFmt
  439. $objWriter->startElement('numFmt');
  440. $objWriter->writeAttribute('numFmtId', ($pId + 164));
  441. $objWriter->writeAttribute('formatCode', $formatCode);
  442. $objWriter->endElement();
  443. }
  444. /**
  445. * Get an array of all styles
  446. *
  447. * @param PHPExcel $pPHPExcel
  448. * @return PHPExcel_Style[] All styles in PHPExcel
  449. * @throws Exception
  450. */
  451. public function allStyles(PHPExcel $pPHPExcel = null)
  452. {
  453. $aStyles = $pPHPExcel->getCellXfCollection();
  454. return $aStyles;
  455. }
  456. /**
  457. * Get an array of all conditional styles
  458. *
  459. * @param PHPExcel $pPHPExcel
  460. * @return PHPExcel_Style_Conditional[] All conditional styles in PHPExcel
  461. * @throws Exception
  462. */
  463. public function allConditionalStyles(PHPExcel $pPHPExcel = null)
  464. {
  465. // Get an array of all styles
  466. $aStyles = array();
  467. $sheetCount = $pPHPExcel->getSheetCount();
  468. for ($i = 0; $i < $sheetCount; ++$i) {
  469. foreach ($pPHPExcel->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
  470. foreach ($conditionalStyles as $conditionalStyle) {
  471. $aStyles[] = $conditionalStyle;
  472. }
  473. }
  474. }
  475. return $aStyles;
  476. }
  477. /**
  478. * Get an array of all fills
  479. *
  480. * @param PHPExcel $pPHPExcel
  481. * @return PHPExcel_Style_Fill[] All fills in PHPExcel
  482. * @throws Exception
  483. */
  484. public function allFills(PHPExcel $pPHPExcel = null)
  485. {
  486. // Get an array of unique fills
  487. $aFills = array();
  488. // Two first fills are predefined
  489. $fill0 = new PHPExcel_Style_Fill();
  490. $fill0->setFillType(PHPExcel_Style_Fill::FILL_NONE);
  491. $aFills[] = $fill0;
  492. $fill1 = new PHPExcel_Style_Fill();
  493. $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125);
  494. $aFills[] = $fill1;
  495. // The remaining fills
  496. $aStyles = $this->allStyles($pPHPExcel);
  497. foreach ($aStyles as $style) {
  498. if (!array_key_exists($style->getFill()->getHashCode(), $aFills)) {
  499. $aFills[ $style->getFill()->getHashCode() ] = $style->getFill();
  500. }
  501. }
  502. return $aFills;
  503. }
  504. /**
  505. * Get an array of all fonts
  506. *
  507. * @param PHPExcel $pPHPExcel
  508. * @return PHPExcel_Style_Font[] All fonts in PHPExcel
  509. * @throws Exception
  510. */
  511. public function allFonts(PHPExcel $pPHPExcel = null)
  512. {
  513. // Get an array of unique fonts
  514. $aFonts = array();
  515. $aStyles = $this->allStyles($pPHPExcel);
  516. foreach ($aStyles as $style) {
  517. if (!array_key_exists($style->getFont()->getHashCode(), $aFonts)) {
  518. $aFonts[ $style->getFont()->getHashCode() ] = $style->getFont();
  519. }
  520. }
  521. return $aFonts;
  522. }
  523. /**
  524. * Get an array of all borders
  525. *
  526. * @param PHPExcel $pPHPExcel
  527. * @return PHPExcel_Style_Borders[] All borders in PHPExcel
  528. * @throws Exception
  529. */
  530. public function allBorders(PHPExcel $pPHPExcel = null)
  531. {
  532. // Get an array of unique borders
  533. $aBorders = array();
  534. $aStyles = $this->allStyles($pPHPExcel);
  535. foreach ($aStyles as $style) {
  536. if (!array_key_exists($style->getBorders()->getHashCode(), $aBorders)) {
  537. $aBorders[ $style->getBorders()->getHashCode() ] = $style->getBorders();
  538. }
  539. }
  540. return $aBorders;
  541. }
  542. /**
  543. * Get an array of all number formats
  544. *
  545. * @param PHPExcel $pPHPExcel
  546. * @return PHPExcel_Style_NumberFormat[] All number formats in PHPExcel
  547. * @throws Exception
  548. */
  549. public function allNumberFormats(PHPExcel $pPHPExcel = null)
  550. {
  551. // Get an array of unique number formats
  552. $aNumFmts = array();
  553. $aStyles = $this->allStyles($pPHPExcel);
  554. foreach ($aStyles as $style) {
  555. if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !array_key_exists($style->getNumberFormat()->getHashCode(), $aNumFmts)) {
  556. $aNumFmts[ $style->getNumberFormat()->getHashCode() ] = $style->getNumberFormat();
  557. }
  558. }
  559. return $aNumFmts;
  560. }
  561. }