Style.php 27 KB

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