7e6bfb79a895e411f340c29a1838a0ae71c18ce4.svn-base 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 ##VERSION##, ##DATE##
  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. if ($pStyle->getAlignment()->getReadorder() > 0) {
  376. $objWriter->writeAttribute('readingOrder', $pStyle->getAlignment()->getReadorder());
  377. }
  378. $objWriter->endElement();
  379. // protection
  380. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  381. $objWriter->startElement('protection');
  382. if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  383. $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  384. }
  385. if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  386. $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  387. }
  388. $objWriter->endElement();
  389. }
  390. $objWriter->endElement();
  391. }
  392. /**
  393. * Write Cell Style Dxf
  394. *
  395. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  396. * @param PHPExcel_Style $pStyle Style
  397. * @throws PHPExcel_Writer_Exception
  398. */
  399. private function _writeCellStyleDxf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null)
  400. {
  401. // dxf
  402. $objWriter->startElement('dxf');
  403. // font
  404. $this->_writeFont($objWriter, $pStyle->getFont());
  405. // numFmt
  406. $this->_writeNumFmt($objWriter, $pStyle->getNumberFormat());
  407. // fill
  408. $this->_writeFill($objWriter, $pStyle->getFill());
  409. // alignment
  410. $objWriter->startElement('alignment');
  411. if ($pStyle->getAlignment()->getHorizontal() !== NULL) {
  412. $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
  413. }
  414. if ($pStyle->getAlignment()->getVertical() !== NULL) {
  415. $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
  416. }
  417. if ($pStyle->getAlignment()->getTextRotation() !== NULL) {
  418. $textRotation = 0;
  419. if ($pStyle->getAlignment()->getTextRotation() >= 0) {
  420. $textRotation = $pStyle->getAlignment()->getTextRotation();
  421. } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
  422. $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
  423. }
  424. $objWriter->writeAttribute('textRotation', $textRotation);
  425. }
  426. $objWriter->endElement();
  427. // border
  428. $this->_writeBorder($objWriter, $pStyle->getBorders());
  429. // protection
  430. if (($pStyle->getProtection()->getLocked() !== NULL) ||
  431. ($pStyle->getProtection()->getHidden() !== NULL)) {
  432. if ($pStyle->getProtection()->getLocked() !== PHPExcel_Style_Protection::PROTECTION_INHERIT ||
  433. $pStyle->getProtection()->getHidden() !== PHPExcel_Style_Protection::PROTECTION_INHERIT) {
  434. $objWriter->startElement('protection');
  435. if (($pStyle->getProtection()->getLocked() !== NULL) &&
  436. ($pStyle->getProtection()->getLocked() !== PHPExcel_Style_Protection::PROTECTION_INHERIT)) {
  437. $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  438. }
  439. if (($pStyle->getProtection()->getHidden() !== NULL) &&
  440. ($pStyle->getProtection()->getHidden() !== PHPExcel_Style_Protection::PROTECTION_INHERIT)) {
  441. $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
  442. }
  443. $objWriter->endElement();
  444. }
  445. }
  446. $objWriter->endElement();
  447. }
  448. /**
  449. * Write BorderPr
  450. *
  451. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  452. * @param string $pName Element name
  453. * @param PHPExcel_Style_Border $pBorder Border style
  454. * @throws PHPExcel_Writer_Exception
  455. */
  456. private function _writeBorderPr(PHPExcel_Shared_XMLWriter $objWriter = null, $pName = 'left', PHPExcel_Style_Border $pBorder = null)
  457. {
  458. // Write BorderPr
  459. if ($pBorder->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  460. $objWriter->startElement($pName);
  461. $objWriter->writeAttribute('style', $pBorder->getBorderStyle());
  462. // color
  463. $objWriter->startElement('color');
  464. $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB());
  465. $objWriter->endElement();
  466. $objWriter->endElement();
  467. }
  468. }
  469. /**
  470. * Write NumberFormat
  471. *
  472. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  473. * @param PHPExcel_Style_NumberFormat $pNumberFormat Number Format
  474. * @param int $pId Number Format identifier
  475. * @throws PHPExcel_Writer_Exception
  476. */
  477. private function _writeNumFmt(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_NumberFormat $pNumberFormat = null, $pId = 0)
  478. {
  479. // Translate formatcode
  480. $formatCode = $pNumberFormat->getFormatCode();
  481. // numFmt
  482. if ($formatCode !== NULL) {
  483. $objWriter->startElement('numFmt');
  484. $objWriter->writeAttribute('numFmtId', ($pId + 164));
  485. $objWriter->writeAttribute('formatCode', $formatCode);
  486. $objWriter->endElement();
  487. }
  488. }
  489. /**
  490. * Get an array of all styles
  491. *
  492. * @param PHPExcel $pPHPExcel
  493. * @return PHPExcel_Style[] All styles in PHPExcel
  494. * @throws PHPExcel_Writer_Exception
  495. */
  496. public function allStyles(PHPExcel $pPHPExcel = null)
  497. {
  498. $aStyles = $pPHPExcel->getCellXfCollection();
  499. return $aStyles;
  500. }
  501. /**
  502. * Get an array of all conditional styles
  503. *
  504. * @param PHPExcel $pPHPExcel
  505. * @return PHPExcel_Style_Conditional[] All conditional styles in PHPExcel
  506. * @throws PHPExcel_Writer_Exception
  507. */
  508. public function allConditionalStyles(PHPExcel $pPHPExcel = null)
  509. {
  510. // Get an array of all styles
  511. $aStyles = array();
  512. $sheetCount = $pPHPExcel->getSheetCount();
  513. for ($i = 0; $i < $sheetCount; ++$i) {
  514. foreach ($pPHPExcel->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
  515. foreach ($conditionalStyles as $conditionalStyle) {
  516. $aStyles[] = $conditionalStyle;
  517. }
  518. }
  519. }
  520. return $aStyles;
  521. }
  522. /**
  523. * Get an array of all fills
  524. *
  525. * @param PHPExcel $pPHPExcel
  526. * @return PHPExcel_Style_Fill[] All fills in PHPExcel
  527. * @throws PHPExcel_Writer_Exception
  528. */
  529. public function allFills(PHPExcel $pPHPExcel = null)
  530. {
  531. // Get an array of unique fills
  532. $aFills = array();
  533. // Two first fills are predefined
  534. $fill0 = new PHPExcel_Style_Fill();
  535. $fill0->setFillType(PHPExcel_Style_Fill::FILL_NONE);
  536. $aFills[] = $fill0;
  537. $fill1 = new PHPExcel_Style_Fill();
  538. $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125);
  539. $aFills[] = $fill1;
  540. // The remaining fills
  541. $aStyles = $this->allStyles($pPHPExcel);
  542. foreach ($aStyles as $style) {
  543. if (!array_key_exists($style->getFill()->getHashCode(), $aFills)) {
  544. $aFills[ $style->getFill()->getHashCode() ] = $style->getFill();
  545. }
  546. }
  547. return $aFills;
  548. }
  549. /**
  550. * Get an array of all fonts
  551. *
  552. * @param PHPExcel $pPHPExcel
  553. * @return PHPExcel_Style_Font[] All fonts in PHPExcel
  554. * @throws PHPExcel_Writer_Exception
  555. */
  556. public function allFonts(PHPExcel $pPHPExcel = null)
  557. {
  558. // Get an array of unique fonts
  559. $aFonts = array();
  560. $aStyles = $this->allStyles($pPHPExcel);
  561. foreach ($aStyles as $style) {
  562. if (!array_key_exists($style->getFont()->getHashCode(), $aFonts)) {
  563. $aFonts[ $style->getFont()->getHashCode() ] = $style->getFont();
  564. }
  565. }
  566. return $aFonts;
  567. }
  568. /**
  569. * Get an array of all borders
  570. *
  571. * @param PHPExcel $pPHPExcel
  572. * @return PHPExcel_Style_Borders[] All borders in PHPExcel
  573. * @throws PHPExcel_Writer_Exception
  574. */
  575. public function allBorders(PHPExcel $pPHPExcel = null)
  576. {
  577. // Get an array of unique borders
  578. $aBorders = array();
  579. $aStyles = $this->allStyles($pPHPExcel);
  580. foreach ($aStyles as $style) {
  581. if (!array_key_exists($style->getBorders()->getHashCode(), $aBorders)) {
  582. $aBorders[ $style->getBorders()->getHashCode() ] = $style->getBorders();
  583. }
  584. }
  585. return $aBorders;
  586. }
  587. /**
  588. * Get an array of all number formats
  589. *
  590. * @param PHPExcel $pPHPExcel
  591. * @return PHPExcel_Style_NumberFormat[] All number formats in PHPExcel
  592. * @throws PHPExcel_Writer_Exception
  593. */
  594. public function allNumberFormats(PHPExcel $pPHPExcel = null)
  595. {
  596. // Get an array of unique number formats
  597. $aNumFmts = array();
  598. $aStyles = $this->allStyles($pPHPExcel);
  599. foreach ($aStyles as $style) {
  600. if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !array_key_exists($style->getNumberFormat()->getHashCode(), $aNumFmts)) {
  601. $aNumFmts[ $style->getNumberFormat()->getHashCode() ] = $style->getNumberFormat();
  602. }
  603. }
  604. return $aNumFmts;
  605. }
  606. }