Xf.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. // Original file header of PEAR::Spreadsheet_Excel_Writer_Format (used as the base for this class):
  8. // -----------------------------------------------------------------------------------------
  9. // /*
  10. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  11. // *
  12. // * The majority of this is _NOT_ my code. I simply ported it from the
  13. // * PERL Spreadsheet::WriteExcel module.
  14. // *
  15. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  16. // * <jmcnamara@cpan.org>
  17. // *
  18. // * I _DO_ maintain this code, and John McNamara has nothing to do with the
  19. // * porting of this code to PHP. Any questions directly related to this
  20. // * class library should be directed to me.
  21. // *
  22. // * License Information:
  23. // *
  24. // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  25. // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  26. // *
  27. // * This library is free software; you can redistribute it and/or
  28. // * modify it under the terms of the GNU Lesser General Public
  29. // * License as published by the Free Software Foundation; either
  30. // * version 2.1 of the License, or (at your option) any later version.
  31. // *
  32. // * This library is distributed in the hope that it will be useful,
  33. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. // * Lesser General Public License for more details.
  36. // *
  37. // * You should have received a copy of the GNU Lesser General Public
  38. // * License along with this library; if not, write to the Free Software
  39. // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. // */
  41. class PHPExcel_Writer_Excel5_Xf
  42. {
  43. /**
  44. * Style XF or a cell XF ?
  45. *
  46. * @var boolean
  47. */
  48. private $isStyleXf;
  49. /**
  50. * Index to the FONT record. Index 4 does not exist
  51. * @var integer
  52. */
  53. private $fontIndex;
  54. /**
  55. * An index (2 bytes) to a FORMAT record (number format).
  56. * @var integer
  57. */
  58. private $numberFormatIndex;
  59. /**
  60. * 1 bit, apparently not used.
  61. * @var integer
  62. */
  63. private $textJustLast;
  64. /**
  65. * The cell's foreground color.
  66. * @var integer
  67. */
  68. private $foregroundColor;
  69. /**
  70. * The cell's background color.
  71. * @var integer
  72. */
  73. private $backgroundColor;
  74. /**
  75. * Color of the bottom border of the cell.
  76. * @var integer
  77. */
  78. private $bottomBorderColor;
  79. /**
  80. * Color of the top border of the cell.
  81. * @var integer
  82. */
  83. private $topBorderColor;
  84. /**
  85. * Color of the left border of the cell.
  86. * @var integer
  87. */
  88. private $leftBorderColor;
  89. /**
  90. * Color of the right border of the cell.
  91. * @var integer
  92. */
  93. private $rightBorderColor;
  94. /**
  95. * Constructor
  96. *
  97. * @access public
  98. * @param PHPExcel_Style The XF format
  99. */
  100. public function __construct(PHPExcel_Style $style = null)
  101. {
  102. $this->isStyleXf = false;
  103. $this->fontIndex = 0;
  104. $this->numberFormatIndex = 0;
  105. $this->textJustLast = 0;
  106. $this->foregroundColor = 0x40;
  107. $this->backgroundColor = 0x41;
  108. $this->_diag = 0;
  109. $this->bottomBorderColor = 0x40;
  110. $this->topBorderColor = 0x40;
  111. $this->leftBorderColor = 0x40;
  112. $this->rightBorderColor = 0x40;
  113. $this->_diag_color = 0x40;
  114. $this->_style = $style;
  115. }
  116. /**
  117. * Generate an Excel BIFF XF record (style or cell).
  118. *
  119. * @return string The XF record
  120. */
  121. public function writeXf()
  122. {
  123. // Set the type of the XF record and some of the attributes.
  124. if ($this->isStyleXf) {
  125. $style = 0xFFF5;
  126. } else {
  127. $style = self::mapLocked($this->_style->getProtection()->getLocked());
  128. $style |= self::mapHidden($this->_style->getProtection()->getHidden()) << 1;
  129. }
  130. // Flags to indicate if attributes have been set.
  131. $atr_num = ($this->numberFormatIndex != 0)?1:0;
  132. $atr_fnt = ($this->fontIndex != 0)?1:0;
  133. $atr_alc = ((int) $this->_style->getAlignment()->getWrapText()) ? 1 : 0;
  134. $atr_bdr = (self::mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
  135. self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
  136. self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
  137. self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
  138. $atr_pat = (($this->foregroundColor != 0x40) ||
  139. ($this->backgroundColor != 0x41) ||
  140. self::mapFillType($this->_style->getFill()->getFillType()))?1:0;
  141. $atr_prot = self::mapLocked($this->_style->getProtection()->getLocked())
  142. | self::mapHidden($this->_style->getProtection()->getHidden());
  143. // Zero the default border colour if the border has not been set.
  144. if (self::mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
  145. $this->bottomBorderColor = 0;
  146. }
  147. if (self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
  148. $this->topBorderColor = 0;
  149. }
  150. if (self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
  151. $this->rightBorderColor = 0;
  152. }
  153. if (self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
  154. $this->leftBorderColor = 0;
  155. }
  156. if (self::mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
  157. $this->_diag_color = 0;
  158. }
  159. $record = 0x00E0; // Record identifier
  160. $length = 0x0014; // Number of bytes to follow
  161. $ifnt = $this->fontIndex; // Index to FONT record
  162. $ifmt = $this->numberFormatIndex; // Index to FORMAT record
  163. $align = $this->mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
  164. $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
  165. $align |= self::mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
  166. $align |= $this->textJustLast << 7;
  167. $used_attrib = $atr_num << 2;
  168. $used_attrib |= $atr_fnt << 3;
  169. $used_attrib |= $atr_alc << 4;
  170. $used_attrib |= $atr_bdr << 5;
  171. $used_attrib |= $atr_pat << 6;
  172. $used_attrib |= $atr_prot << 7;
  173. $icv = $this->foregroundColor; // fg and bg pattern colors
  174. $icv |= $this->backgroundColor << 7;
  175. $border1 = self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
  176. $border1 |= self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
  177. $border1 |= self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
  178. $border1 |= self::mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
  179. $border1 |= $this->leftBorderColor << 16;
  180. $border1 |= $this->rightBorderColor << 23;
  181. $diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
  182. $diag_tl_to_rb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
  183. || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
  184. $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
  185. || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
  186. $border1 |= $diag_tl_to_rb << 30;
  187. $border1 |= $diag_tr_to_lb << 31;
  188. $border2 = $this->topBorderColor; // Border color
  189. $border2 |= $this->bottomBorderColor << 7;
  190. $border2 |= $this->_diag_color << 14;
  191. $border2 |= self::mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
  192. $border2 |= self::mapFillType($this->_style->getFill()->getFillType()) << 26;
  193. $header = pack("vv", $record, $length);
  194. //BIFF8 options: identation, shrinkToFit and text direction
  195. $biff8_options = $this->_style->getAlignment()->getIndent();
  196. $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
  197. $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
  198. $data .= pack("CCC", self::mapTextRotation($this->_style->getAlignment()->getTextRotation()), $biff8_options, $used_attrib);
  199. $data .= pack("VVv", $border1, $border2, $icv);
  200. return($header . $data);
  201. }
  202. /**
  203. * Is this a style XF ?
  204. *
  205. * @param boolean $value
  206. */
  207. public function setIsStyleXf($value)
  208. {
  209. $this->isStyleXf = $value;
  210. }
  211. /**
  212. * Sets the cell's bottom border color
  213. *
  214. * @access public
  215. * @param int $colorIndex Color index
  216. */
  217. public function setBottomColor($colorIndex)
  218. {
  219. $this->bottomBorderColor = $colorIndex;
  220. }
  221. /**
  222. * Sets the cell's top border color
  223. *
  224. * @access public
  225. * @param int $colorIndex Color index
  226. */
  227. public function setTopColor($colorIndex)
  228. {
  229. $this->topBorderColor = $colorIndex;
  230. }
  231. /**
  232. * Sets the cell's left border color
  233. *
  234. * @access public
  235. * @param int $colorIndex Color index
  236. */
  237. public function setLeftColor($colorIndex)
  238. {
  239. $this->leftBorderColor = $colorIndex;
  240. }
  241. /**
  242. * Sets the cell's right border color
  243. *
  244. * @access public
  245. * @param int $colorIndex Color index
  246. */
  247. public function setRightColor($colorIndex)
  248. {
  249. $this->rightBorderColor = $colorIndex;
  250. }
  251. /**
  252. * Sets the cell's diagonal border color
  253. *
  254. * @access public
  255. * @param int $colorIndex Color index
  256. */
  257. public function setDiagColor($colorIndex)
  258. {
  259. $this->_diag_color = $colorIndex;
  260. }
  261. /**
  262. * Sets the cell's foreground color
  263. *
  264. * @access public
  265. * @param int $colorIndex Color index
  266. */
  267. public function setFgColor($colorIndex)
  268. {
  269. $this->foregroundColor = $colorIndex;
  270. }
  271. /**
  272. * Sets the cell's background color
  273. *
  274. * @access public
  275. * @param int $colorIndex Color index
  276. */
  277. public function setBgColor($colorIndex)
  278. {
  279. $this->backgroundColor = $colorIndex;
  280. }
  281. /**
  282. * Sets the index to the number format record
  283. * It can be date, time, currency, etc...
  284. *
  285. * @access public
  286. * @param integer $numberFormatIndex Index to format record
  287. */
  288. public function setNumberFormatIndex($numberFormatIndex)
  289. {
  290. $this->numberFormatIndex = $numberFormatIndex;
  291. }
  292. /**
  293. * Set the font index.
  294. *
  295. * @param int $value Font index, note that value 4 does not exist
  296. */
  297. public function setFontIndex($value)
  298. {
  299. $this->fontIndex = $value;
  300. }
  301. /**
  302. * Map of BIFF2-BIFF8 codes for border styles
  303. * @static array of int
  304. *
  305. */
  306. private static $mapBorderStyles = array(
  307. PHPExcel_Style_Border::BORDER_NONE => 0x00,
  308. PHPExcel_Style_Border::BORDER_THIN => 0x01,
  309. PHPExcel_Style_Border::BORDER_MEDIUM => 0x02,
  310. PHPExcel_Style_Border::BORDER_DASHED => 0x03,
  311. PHPExcel_Style_Border::BORDER_DOTTED => 0x04,
  312. PHPExcel_Style_Border::BORDER_THICK => 0x05,
  313. PHPExcel_Style_Border::BORDER_DOUBLE => 0x06,
  314. PHPExcel_Style_Border::BORDER_HAIR => 0x07,
  315. PHPExcel_Style_Border::BORDER_MEDIUMDASHED => 0x08,
  316. PHPExcel_Style_Border::BORDER_DASHDOT => 0x09,
  317. PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT => 0x0A,
  318. PHPExcel_Style_Border::BORDER_DASHDOTDOT => 0x0B,
  319. PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT => 0x0C,
  320. PHPExcel_Style_Border::BORDER_SLANTDASHDOT => 0x0D,
  321. );
  322. /**
  323. * Map border style
  324. *
  325. * @param string $borderStyle
  326. * @return int
  327. */
  328. private static function mapBorderStyle($borderStyle)
  329. {
  330. if (isset(self::$mapBorderStyles[$borderStyle])) {
  331. return self::$mapBorderStyles[$borderStyle];
  332. }
  333. return 0x00;
  334. }
  335. /**
  336. * Map of BIFF2-BIFF8 codes for fill types
  337. * @static array of int
  338. *
  339. */
  340. private static $mapFillTypes = array(
  341. PHPExcel_Style_Fill::FILL_NONE => 0x00,
  342. PHPExcel_Style_Fill::FILL_SOLID => 0x01,
  343. PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
  344. PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY => 0x03,
  345. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04,
  346. PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05,
  347. PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06,
  348. PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN => 0x07,
  349. PHPExcel_Style_Fill::FILL_PATTERN_DARKUP => 0x08,
  350. PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID => 0x09,
  351. PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A,
  352. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B,
  353. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C,
  354. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D,
  355. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E,
  356. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F,
  357. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10,
  358. PHPExcel_Style_Fill::FILL_PATTERN_GRAY125 => 0x11,
  359. PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625 => 0x12,
  360. PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8
  361. PHPExcel_Style_Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8
  362. );
  363. /**
  364. * Map fill type
  365. *
  366. * @param string $fillType
  367. * @return int
  368. */
  369. private static function mapFillType($fillType)
  370. {
  371. if (isset(self::$mapFillTypes[$fillType])) {
  372. return self::$mapFillTypes[$fillType];
  373. }
  374. return 0x00;
  375. }
  376. /**
  377. * Map of BIFF2-BIFF8 codes for horizontal alignment
  378. * @static array of int
  379. *
  380. */
  381. private static $mapHAlignments = array(
  382. PHPExcel_Style_Alignment::HORIZONTAL_GENERAL => 0,
  383. PHPExcel_Style_Alignment::HORIZONTAL_LEFT => 1,
  384. PHPExcel_Style_Alignment::HORIZONTAL_CENTER => 2,
  385. PHPExcel_Style_Alignment::HORIZONTAL_RIGHT => 3,
  386. PHPExcel_Style_Alignment::HORIZONTAL_FILL => 4,
  387. PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY => 5,
  388. PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6,
  389. );
  390. /**
  391. * Map to BIFF2-BIFF8 codes for horizontal alignment
  392. *
  393. * @param string $hAlign
  394. * @return int
  395. */
  396. private function mapHAlign($hAlign)
  397. {
  398. if (isset(self::$mapHAlignments[$hAlign])) {
  399. return self::$mapHAlignments[$hAlign];
  400. }
  401. return 0;
  402. }
  403. /**
  404. * Map of BIFF2-BIFF8 codes for vertical alignment
  405. * @static array of int
  406. *
  407. */
  408. private static $mapVAlignments = array(
  409. PHPExcel_Style_Alignment::VERTICAL_TOP => 0,
  410. PHPExcel_Style_Alignment::VERTICAL_CENTER => 1,
  411. PHPExcel_Style_Alignment::VERTICAL_BOTTOM => 2,
  412. PHPExcel_Style_Alignment::VERTICAL_JUSTIFY => 3,
  413. );
  414. /**
  415. * Map to BIFF2-BIFF8 codes for vertical alignment
  416. *
  417. * @param string $vAlign
  418. * @return int
  419. */
  420. private static function mapVAlign($vAlign)
  421. {
  422. if (isset(self::$mapVAlignments[$vAlign])) {
  423. return self::$mapVAlignments[$vAlign];
  424. }
  425. return 2;
  426. }
  427. /**
  428. * Map to BIFF8 codes for text rotation angle
  429. *
  430. * @param int $textRotation
  431. * @return int
  432. */
  433. private static function mapTextRotation($textRotation)
  434. {
  435. if ($textRotation >= 0) {
  436. return $textRotation;
  437. } elseif ($textRotation == -165) {
  438. return 255;
  439. } elseif ($textRotation < 0) {
  440. return 90 - $textRotation;
  441. }
  442. }
  443. /**
  444. * Map locked
  445. *
  446. * @param string
  447. * @return int
  448. */
  449. private static function mapLocked($locked)
  450. {
  451. switch ($locked) {
  452. case PHPExcel_Style_Protection::PROTECTION_INHERIT:
  453. return 1;
  454. case PHPExcel_Style_Protection::PROTECTION_PROTECTED:
  455. return 1;
  456. case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED:
  457. return 0;
  458. default:
  459. return 1;
  460. }
  461. }
  462. /**
  463. * Map hidden
  464. *
  465. * @param string
  466. * @return int
  467. */
  468. private static function mapHidden($hidden)
  469. {
  470. switch ($hidden) {
  471. case PHPExcel_Style_Protection::PROTECTION_INHERIT:
  472. return 0;
  473. case PHPExcel_Style_Protection::PROTECTION_PROTECTED:
  474. return 1;
  475. case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED:
  476. return 0;
  477. default:
  478. return 0;
  479. }
  480. }
  481. }