PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/excellib.class.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 861 lines | 452 code | 77 blank | 332 comment | 89 complexity | 4bb9ade633ac844ddee28e6467a22992 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Excel writer abstraction layer.
  18. *
  19. * @copyright (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Define and operate over one Moodle Workbook.
  26. *
  27. * This class acts as a wrapper around another library
  28. * maintaining Moodle functions isolated from underlying code.
  29. *
  30. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. * @package moodlecore
  33. */
  34. class MoodleExcelWorkbook {
  35. /** @var PHPExcel */
  36. protected $objPHPExcel;
  37. /** @var string */
  38. protected $filename;
  39. /** @var string format type */
  40. protected $type;
  41. /**
  42. * Constructs one Moodle Workbook.
  43. *
  44. * @param string $filename The name of the file
  45. * @param string $type file format type 'Excel5' or 'Excel2007'
  46. */
  47. public function __construct($filename, $type = 'Excel2007') {
  48. global $CFG;
  49. require_once("$CFG->libdir/phpexcel/PHPExcel.php");
  50. $this->objPHPExcel = new PHPExcel();
  51. $this->objPHPExcel->removeSheetByIndex(0);
  52. $this->filename = $filename;
  53. if (strtolower($type) === 'excel5') {
  54. $this->type = 'Excel5';
  55. } else {
  56. $this->type = 'Excel2007';
  57. }
  58. }
  59. /**
  60. * Create one Moodle Worksheet
  61. *
  62. * @param string $name Name of the sheet
  63. * @return MoodleExcelWorksheet
  64. */
  65. public function add_worksheet($name = '') {
  66. return new MoodleExcelWorksheet($name, $this->objPHPExcel);
  67. }
  68. /**
  69. * Create one cell Format.
  70. *
  71. * @param array $properties array of properties [name]=value;
  72. * valid names are set_XXXX existing
  73. * functions without the set_ part
  74. * i.e: [bold]=1 for set_bold(1)...Optional!
  75. * @return MoodleExcelFormat
  76. */
  77. public function add_format($properties = array()) {
  78. return new MoodleExcelFormat($properties);
  79. }
  80. /**
  81. * Close the Moodle Workbook
  82. */
  83. public function close() {
  84. global $CFG;
  85. foreach ($this->objPHPExcel->getAllSheets() as $sheet){
  86. $sheet->setSelectedCells('A1');
  87. }
  88. $this->objPHPExcel->setActiveSheetIndex(0);
  89. $filename = preg_replace('/\.xlsx?$/i', '', $this->filename);
  90. if ($this->type === 'Excel5') {
  91. $mimetype = 'application/vnd.ms-excel';
  92. $filename = $filename.'.xls';
  93. } else {
  94. $mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  95. $filename = $filename.'.xlsx';
  96. }
  97. if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
  98. header('Cache-Control: max-age=10');
  99. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  100. header('Pragma: ');
  101. } else { //normal http - prevent caching at all cost
  102. header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
  103. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  104. header('Pragma: no-cache');
  105. }
  106. if (check_browser_version('MSIE')) {
  107. $filename = rawurlencode($filename);
  108. } else {
  109. $filename = s($filename);
  110. }
  111. header('Content-Type: '.$mimetype);
  112. header('Content-Disposition: attachment;filename="'.$filename.'"');
  113. $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, $this->type);
  114. $objWriter->save('php://output');
  115. }
  116. /**
  117. * Not required to use.
  118. * @param string $filename Name of the downloaded file
  119. */
  120. public function send($filename) {
  121. $this->filename = $filename;
  122. }
  123. }
  124. /**
  125. * Define and operate over one Worksheet.
  126. *
  127. * This class acts as a wrapper around another library
  128. * maintaining Moodle functions isolated from underlying code.
  129. *
  130. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  131. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  132. * @package core
  133. */
  134. class MoodleExcelWorksheet {
  135. /** @var PHPExcel_Worksheet */
  136. protected $worksheet;
  137. /**
  138. * Constructs one Moodle Worksheet.
  139. *
  140. * @param string $name The name of the file
  141. * @param PHPExcel $workbook The internal Workbook object we are creating.
  142. */
  143. public function __construct($name, PHPExcel $workbook) {
  144. // Replace any characters in the name that Excel cannot cope with.
  145. $name = strtr($name, '[]*/\?:', ' ');
  146. // Shorten the title if necessary.
  147. $name = textlib::substr($name, 0, 31);
  148. if ($name === '') {
  149. // Name is required!
  150. $name = 'Sheet'.($workbook->getSheetCount()+1);
  151. }
  152. $this->worksheet = new PHPExcel_Worksheet($workbook, $name);
  153. $this->worksheet->setPrintGridlines(false);
  154. $workbook->addSheet($this->worksheet);
  155. }
  156. /**
  157. * Write one string somewhere in the worksheet.
  158. *
  159. * @param integer $row Zero indexed row
  160. * @param integer $col Zero indexed column
  161. * @param string $str The string to write
  162. * @param mixed $format The XF format for the cell
  163. */
  164. public function write_string($row, $col, $str, $format = null) {
  165. $this->worksheet->getStyleByColumnAndRow($col, $row+1)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  166. $this->worksheet->setCellValueExplicitByColumnAndRow($col, $row+1, $str, PHPExcel_Cell_DataType::TYPE_STRING);
  167. $this->apply_format($row, $col, $format);
  168. }
  169. /**
  170. * Write one number somewhere in the worksheet.
  171. *
  172. * @param integer $row Zero indexed row
  173. * @param integer $col Zero indexed column
  174. * @param float $num The number to write
  175. * @param mixed $format The XF format for the cell
  176. */
  177. public function write_number($row, $col, $num, $format = null) {
  178. $this->worksheet->getStyleByColumnAndRow($col, $row+1)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
  179. $this->worksheet->setCellValueExplicitByColumnAndRow($col, $row+1, $num, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  180. $this->apply_format($row, $col, $format);
  181. }
  182. /**
  183. * Write one url somewhere in the worksheet.
  184. *
  185. * @param integer $row Zero indexed row
  186. * @param integer $col Zero indexed column
  187. * @param string $url The url to write
  188. * @param mixed $format The XF format for the cell
  189. */
  190. public function write_url($row, $col, $url, $format = null) {
  191. $this->worksheet->setCellValueByColumnAndRow($col, $row+1, $url);
  192. $this->worksheet->getCellByColumnAndRow($col, $row+1)->getHyperlink()->setUrl($url);
  193. $this->apply_format($row, $col, $format);
  194. }
  195. /**
  196. * Write one date somewhere in the worksheet.
  197. * @param integer $row Zero indexed row
  198. * @param integer $col Zero indexed column
  199. * @param string $date The date to write in UNIX timestamp format
  200. * @param mixed $format The XF format for the cell
  201. */
  202. public function write_date($row, $col, $date, $format = null) {
  203. $getdate = usergetdate($date);
  204. $exceldate = PHPExcel_Shared_Date::FormattedPHPToExcel(
  205. $getdate['year'],
  206. $getdate['mon'],
  207. $getdate['mday'],
  208. $getdate['hours'],
  209. $getdate['minutes'],
  210. $getdate['seconds']
  211. );
  212. $this->worksheet->setCellValueByColumnAndRow($col, $row+1, $exceldate);
  213. $this->worksheet->getStyleByColumnAndRow($col, $row+1)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22);
  214. $this->apply_format($row, $col, $format);
  215. }
  216. /**
  217. * Write one formula somewhere in the worksheet.
  218. *
  219. * @param integer $row Zero indexed row
  220. * @param integer $col Zero indexed column
  221. * @param string $formula The formula to write
  222. * @param mixed $format The XF format for the cell
  223. */
  224. public function write_formula($row, $col, $formula, $format = null) {
  225. $this->worksheet->setCellValueExplicitByColumnAndRow($col, $row+1, $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  226. $this->apply_format($row, $col, $format);
  227. }
  228. /**
  229. * Write one blank somewhere in the worksheet.
  230. *
  231. * @param integer $row Zero indexed row
  232. * @param integer $col Zero indexed column
  233. * @param mixed $format The XF format for the cell
  234. */
  235. public function write_blank($row, $col, $format = null) {
  236. $this->worksheet->setCellValueByColumnAndRow($col, $row+1, '');
  237. $this->apply_format($row, $col, $format);
  238. }
  239. /**
  240. * Write anything somewhere in the worksheet,
  241. * type will be automatically detected.
  242. *
  243. * @param integer $row Zero indexed row
  244. * @param integer $col Zero indexed column
  245. * @param mixed $token What we are writing
  246. * @param mixed $format The XF format for the cell
  247. */
  248. public function write($row, $col, $token, $format = null) {
  249. // Analyse what are we trying to send.
  250. if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
  251. // Match number
  252. return $this->write_number($row, $col, $token, $format);
  253. } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
  254. // Match http or ftp URL
  255. return $this->write_url($row, $col, $token, '', $format);
  256. } elseif (preg_match("/^mailto:/", $token)) {
  257. // Match mailto:
  258. return $this->write_url($row, $col, $token, '', $format);
  259. } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
  260. // Match internal or external sheet link
  261. return $this->write_url($row, $col, $token, '', $format);
  262. } elseif (preg_match("/^=/", $token)) {
  263. // Match formula
  264. return $this->write_formula($row, $col, $token, $format);
  265. } elseif (preg_match("/^@/", $token)) {
  266. // Match formula
  267. return $this->write_formula($row, $col, $token, $format);
  268. } elseif ($token == '') {
  269. // Match blank
  270. return $this->write_blank($row, $col, $format);
  271. } else {
  272. // Default: match string
  273. return $this->write_string($row, $col, $token, $format);
  274. }
  275. }
  276. /**
  277. * Sets the height (and other settings) of one row.
  278. *
  279. * @param integer $row The row to set
  280. * @param integer $height Height we are giving to the row (null to set just format without setting the height)
  281. * @param mixed $format The optional format we are giving to the row
  282. * @param bool $hidden The optional hidden attribute
  283. * @param integer $level The optional outline level (0-7)
  284. */
  285. public function set_row($row, $height, $format = null, $hidden = false, $level = 0) {
  286. if ($level < 0) {
  287. $level = 0;
  288. } else if ($level > 7) {
  289. $level = 7;
  290. }
  291. if (isset($height)) {
  292. $this->worksheet->getRowDimension($row+1)->setRowHeight($height);
  293. }
  294. $this->worksheet->getRowDimension($row+1)->setVisible(!$hidden);
  295. $this->worksheet->getRowDimension($row+1)->setOutlineLevel($level);
  296. $this->apply_row_format($row, $format);
  297. }
  298. /**
  299. * Sets the width (and other settings) of one column.
  300. *
  301. * @param integer $firstcol first column on the range
  302. * @param integer $lastcol last column on the range
  303. * @param integer $width width to set (null to set just format without setting the width)
  304. * @param mixed $format The optional format to apply to the columns
  305. * @param bool $hidden The optional hidden attribute
  306. * @param integer $level The optional outline level (0-7)
  307. */
  308. public function set_column($firstcol, $lastcol, $width, $format = null, $hidden = false, $level = 0) {
  309. if ($level < 0) {
  310. $level = 0;
  311. } else if ($level > 7) {
  312. $level = 7;
  313. }
  314. $i = $firstcol;
  315. while($i <= $lastcol) {
  316. if (isset($width)) {
  317. $this->worksheet->getColumnDimensionByColumn($i)->setWidth($width);
  318. }
  319. $this->worksheet->getColumnDimensionByColumn($i)->setVisible(!$hidden);
  320. $this->worksheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  321. $this->apply_column_format($i, $format);
  322. $i++;
  323. }
  324. }
  325. /**
  326. * Set the option to hide grid lines on the printed page.
  327. */
  328. public function hide_gridlines() {
  329. // Not implemented - always off.
  330. }
  331. /**
  332. * Set the option to hide gridlines on the worksheet (as seen on the screen).
  333. */
  334. public function hide_screen_gridlines() {
  335. $this->worksheet->setShowGridlines(false);
  336. }
  337. /**
  338. * Insert an image in a worksheet.
  339. *
  340. * @param integer $row The row we are going to insert the bitmap into
  341. * @param integer $col The column we are going to insert the bitmap into
  342. * @param string $bitmap The bitmap filename
  343. * @param integer $x The horizontal position (offset) of the image inside the cell.
  344. * @param integer $y The vertical position (offset) of the image inside the cell.
  345. * @param integer $scale_x The horizontal scale
  346. * @param integer $scale_y The vertical scale
  347. */
  348. public function insert_bitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1) {
  349. $objDrawing = new PHPExcel_Worksheet_Drawing();
  350. $objDrawing->setPath($bitmap);
  351. $objDrawing->setCoordinates(PHPExcel_Cell::stringFromColumnIndex($col) . ($row+1));
  352. $objDrawing->setOffsetX($x);
  353. $objDrawing->setOffsetY($y);
  354. $objDrawing->setWorksheet($this->worksheet);
  355. if ($scale_x != 1) {
  356. $objDrawing->setResizeProportional(false);
  357. $objDrawing->getWidth($objDrawing->getWidth()*$scale_x);
  358. }
  359. if ($scale_y != 1) {
  360. $objDrawing->setResizeProportional(false);
  361. $objDrawing->setHeight($objDrawing->getHeight()*$scale_y);
  362. }
  363. }
  364. /**
  365. * Merges the area given by its arguments.
  366. *
  367. * @param integer $first_row First row of the area to merge
  368. * @param integer $first_col First column of the area to merge
  369. * @param integer $last_row Last row of the area to merge
  370. * @param integer $last_col Last column of the area to merge
  371. */
  372. public function merge_cells($first_row, $first_col, $last_row, $last_col) {
  373. $this->worksheet->mergeCellsByColumnAndRow($first_col, $first_row+1, $last_col, $last_row+1);
  374. }
  375. protected function apply_format($row, $col, $format = null) {
  376. if (!$format) {
  377. $format = new MoodleExcelFormat();
  378. } else if (is_array($format)) {
  379. $format = new MoodleExcelFormat($format);
  380. }
  381. $this->worksheet->getStyleByColumnAndRow($col, $row+1)->applyFromArray($format->get_format_array());
  382. }
  383. protected function apply_column_format($col, $format = null) {
  384. if (!$format) {
  385. $format = new MoodleExcelFormat();
  386. } else if (is_array($format)) {
  387. $format = new MoodleExcelFormat($format);
  388. }
  389. $this->worksheet->getStyle(PHPExcel_Cell::stringFromColumnIndex($col))->applyFromArray($format->get_format_array());
  390. }
  391. protected function apply_row_format($row, $format = null) {
  392. if (!$format) {
  393. $format = new MoodleExcelFormat();
  394. } else if (is_array($format)) {
  395. $format = new MoodleExcelFormat($format);
  396. }
  397. $this->worksheet->getStyle($row+1)->applyFromArray($format->get_format_array());
  398. }
  399. }
  400. /**
  401. * Define and operate over one Format.
  402. *
  403. * A big part of this class acts as a wrapper over the PEAR
  404. * Spreadsheet_Excel_Writer_Workbook and OLE libraries
  405. * maintaining Moodle functions isolated from underlying code.
  406. *
  407. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  408. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  409. * @package moodlecore
  410. */
  411. class MoodleExcelFormat {
  412. /** @var array */
  413. protected $format = array('font'=>array('size'=>10, 'name'=>'Arial'));
  414. /**
  415. * Constructs one Moodle Format.
  416. *
  417. * @param array $properties
  418. */
  419. public function __construct($properties = array()) {
  420. // If we have something in the array of properties, compute them
  421. foreach($properties as $property => $value) {
  422. if(method_exists($this,"set_$property")) {
  423. $aux = 'set_'.$property;
  424. $this->$aux($value);
  425. }
  426. }
  427. }
  428. /**
  429. * Returns standardised Excel format array.
  430. * @private
  431. *
  432. * @return array
  433. */
  434. public function get_format_array() {
  435. return $this->format;
  436. }
  437. /**
  438. * Set the size of the text in the format (in pixels).
  439. * By default all texts in generated sheets are 10pt.
  440. *
  441. * @param integer $size Size of the text (in points)
  442. */
  443. public function set_size($size) {
  444. $this->format['font']['size'] = $size;
  445. }
  446. /**
  447. * Set weight of the format.
  448. *
  449. * @param integer $weight Weight for the text, 0 maps to 400 (normal text),
  450. * 1 maps to 700 (bold text). Valid range is: 100-1000.
  451. * It's Optional, default is 1 (bold).
  452. */
  453. public function set_bold($weight = 1) {
  454. if ($weight == 1) {
  455. $weight = 700;
  456. }
  457. $this->format['font']['bold'] = ($weight > 400);
  458. }
  459. /**
  460. * Set underline of the format.
  461. *
  462. * @param integer $underline The value for underline. Possible values are:
  463. * 1 => underline, 2 => double underline
  464. */
  465. public function set_underline($underline) {
  466. if ($underline == 1) {
  467. $this->format['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLE;
  468. } else if ($underline == 2) {
  469. $this->format['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLE;
  470. } else {
  471. $this->format['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_NONE;
  472. }
  473. }
  474. /**
  475. * Set italic of the format.
  476. */
  477. public function set_italic() {
  478. $this->format['font']['italic'] = true;
  479. }
  480. /**
  481. * Set strikeout of the format.
  482. */
  483. public function set_strikeout() {
  484. $this->format['font']['strike'] = true;
  485. }
  486. /**
  487. * Set outlining of the format.
  488. */
  489. public function set_outline() {
  490. // Not implemented.
  491. }
  492. /**
  493. * Set shadow of the format.
  494. */
  495. public function set_shadow() {
  496. // Not implemented.
  497. }
  498. /**
  499. * Set the script of the text.
  500. *
  501. * @param integer $script The value for script type. Possible values are:
  502. * 1 => superscript, 2 => subscript
  503. */
  504. public function set_script($script) {
  505. if ($script == 1) {
  506. $this->format['font']['superScript'] = true;
  507. } else if ($script == 2) {
  508. $this->format['font']['subScript'] = true;
  509. } else {
  510. $this->format['font']['superScript'] = false;
  511. $this->format['font']['subScript'] = false;
  512. }
  513. }
  514. /**
  515. * Set color of the format. Used to specify the color of the text to be formatted.
  516. *
  517. * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
  518. */
  519. public function set_color($color) {
  520. $this->format['font']['color']['rgb'] = $this->parse_color($color);
  521. }
  522. /**
  523. * Standardise colour name.
  524. *
  525. * @param mixed $color name of the color (i.e.: 'blue', 'red', etc..), or an integer (range is [8...63]).
  526. * @return string the RGB color value
  527. */
  528. protected function parse_color($color) {
  529. if (strpos($color, '#') === 0) {
  530. // No conversion should be needed.
  531. return substr($color, 1);
  532. }
  533. if ($color > 7 and $color < 53) {
  534. $numbers = array(
  535. 8 => 'black',
  536. 12 => 'blue',
  537. 16 => 'brown',
  538. 15 => 'cyan',
  539. 23 => 'gray',
  540. 17 => 'green',
  541. 11 => 'lime',
  542. 14 => 'magenta',
  543. 18 => 'navy',
  544. 53 => 'orange',
  545. 33 => 'pink',
  546. 20 => 'purple',
  547. 10 => 'red',
  548. 22 => 'silver',
  549. 9 => 'white',
  550. 13 => 'yellow',
  551. );
  552. if (isset($numbers[$color])) {
  553. $color = $numbers[$color];
  554. } else {
  555. $color = 'black';
  556. }
  557. }
  558. $colors = array(
  559. 'aqua' => '00FFFF',
  560. 'black' => '000000',
  561. 'blue' => '0000FF',
  562. 'brown' => 'A52A2A',
  563. 'cyan' => '00FFFF',
  564. 'fuchsia' => 'FF00FF',
  565. 'gray' => '808080',
  566. 'grey' => '808080',
  567. 'green' => '00FF00',
  568. 'lime' => '00FF00',
  569. 'magenta' => 'FF00FF',
  570. 'maroon' => '800000',
  571. 'navy' => '000080',
  572. 'orange' => 'FFA500',
  573. 'olive' => '808000',
  574. 'pink' => 'FAAFBE',
  575. 'purple' => '800080',
  576. 'red' => 'FF0000',
  577. 'silver' => 'C0C0C0',
  578. 'teal' => '008080',
  579. 'white' => 'FFFFFF',
  580. 'yellow' => 'FFFF00',
  581. );
  582. if (isset($colors[$color])) {
  583. return($colors[$color]);
  584. }
  585. return($colors['black']);
  586. }
  587. /**
  588. * Not used.
  589. *
  590. * @param mixed $color
  591. */
  592. public function set_fg_color($color) {
  593. // Not implemented.
  594. }
  595. /**
  596. * Set background color of the cell.
  597. *
  598. * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
  599. */
  600. public function set_bg_color($color) {
  601. if (!isset($this->format['fill']['type'])) {
  602. $this->format['fill']['type'] = PHPExcel_Style_Fill::FILL_SOLID;
  603. }
  604. $this->format['fill']['color']['rgb'] = $this->parse_color($color);
  605. }
  606. /**
  607. * Set the cell fill pattern.
  608. *
  609. * @deprecated use set_bg_color() instead.
  610. * @param integer
  611. */
  612. public function set_pattern($pattern=1) {
  613. if ($pattern > 0) {
  614. if (!isset($this->format['fill']['color']['rgb'])) {
  615. $this->set_bg_color('black');
  616. }
  617. } else {
  618. unset($this->format['fill']['color']['rgb']);
  619. unset($this->format['fill']['type']);
  620. }
  621. }
  622. /**
  623. * Set text wrap of the format.
  624. */
  625. public function set_text_wrap() {
  626. $this->format['alignment']['wrap'] = true;
  627. }
  628. /**
  629. * Set the cell alignment of the format.
  630. *
  631. * @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
  632. */
  633. public function set_align($location) {
  634. if (in_array($location, array('left', 'centre', 'center', 'right', 'fill', 'merge', 'justify', 'equal_space'))) {
  635. $this->set_h_align($location);
  636. } else if (in_array($location, array('top', 'vcentre', 'vcenter', 'bottom', 'vjustify', 'vequal_space'))) {
  637. $this->set_v_align($location);
  638. }
  639. }
  640. /**
  641. * Set the cell horizontal alignment of the format.
  642. *
  643. * @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
  644. */
  645. public function set_h_align($location) {
  646. switch ($location) {
  647. case 'left':
  648. $this->format['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_LEFT;
  649. break;
  650. case 'center':
  651. case 'centre':
  652. $this->format['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_CENTER;
  653. break;
  654. case 'right':
  655. $this->format['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT;
  656. break;
  657. case 'justify':
  658. $this->format['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY;
  659. break;
  660. default:
  661. $this->format['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  662. }
  663. }
  664. /**
  665. * Set the cell vertical alignment of the format.
  666. *
  667. * @param string $location alignment for the cell ('top', 'bottom', 'center', 'justify')
  668. */
  669. public function set_v_align($location) {
  670. switch ($location) {
  671. case 'top':
  672. $this->format['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_TOP;
  673. break;
  674. case 'vcentre':
  675. case 'vcenter':
  676. case 'centre':
  677. case 'center':
  678. $this->format['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_CENTER;
  679. break;
  680. case 'vjustify':
  681. case 'justify':
  682. $this->format['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_JUSTIFY;
  683. break;
  684. default:
  685. $this->format['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  686. }
  687. }
  688. /**
  689. * Set the top border of the format.
  690. *
  691. * @param integer $style style for the cell. 1 => thin, 2 => thick
  692. */
  693. public function set_top($style) {
  694. if ($style == 1) {
  695. $this->format['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  696. } else if ($style == 2) {
  697. $this->format['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THICK;
  698. } else {
  699. $this->format['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_NONE;
  700. }
  701. }
  702. /**
  703. * Set the bottom border of the format.
  704. *
  705. * @param integer $style style for the cell. 1 => thin, 2 => thick
  706. */
  707. public function set_bottom($style) {
  708. if ($style == 1) {
  709. $this->format['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  710. } else if ($style == 2) {
  711. $this->format['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THICK;
  712. } else {
  713. $this->format['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_NONE;
  714. }
  715. }
  716. /**
  717. * Set the left border of the format.
  718. *
  719. * @param integer $style style for the cell. 1 => thin, 2 => thick
  720. */
  721. public function set_left($style) {
  722. if ($style == 1) {
  723. $this->format['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  724. } else if ($style == 2) {
  725. $this->format['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THICK;
  726. } else {
  727. $this->format['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_NONE;
  728. }
  729. }
  730. /**
  731. * Set the right border of the format.
  732. *
  733. * @param integer $style style for the cell. 1 => thin, 2 => thick
  734. */
  735. public function set_right($style) {
  736. if ($style == 1) {
  737. $this->format['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  738. } else if ($style == 2) {
  739. $this->format['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THICK;
  740. } else {
  741. $this->format['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_NONE;
  742. }
  743. }
  744. /**
  745. * Set cells borders to the same style.
  746. *
  747. * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
  748. */
  749. public function set_border($style) {
  750. $this->set_top($style);
  751. $this->set_bottom($style);
  752. $this->set_left($style);
  753. $this->set_right($style);
  754. }
  755. /**
  756. * Set the numerical format of the format.
  757. * It can be date, time, currency, etc...
  758. *
  759. * @param mixed $num_format The numeric format
  760. */
  761. public function set_num_format($num_format) {
  762. $numbers = array();
  763. $numbers[1] = '0';
  764. $numbers[2] = '0.00';
  765. $numbers[3] = '#,##0';
  766. $numbers[4] = '#,##0.00';
  767. $numbers[11] = '0.00E+00';
  768. $numbers[12] = '# ?/?';
  769. $numbers[13] = '# ??/??';
  770. $numbers[14] = 'mm-dd-yy';
  771. $numbers[15] = 'd-mmm-yy';
  772. $numbers[16] = 'd-mmm';
  773. $numbers[17] = 'mmm-yy';
  774. $numbers[22] = 'm/d/yy h:mm';
  775. $numbers[49] = '@';
  776. if ($num_format !== 0 and in_array($num_format, $numbers)) {
  777. $this->format['numberformat']['code'] = $num_format;
  778. }
  779. if (!isset($numbers[$num_format])) {
  780. return;
  781. }
  782. $this->format['numberformat']['code'] = $numbers[$num_format];
  783. }
  784. }