/src/Maatwebsite/Excel/Classes/LaravelExcelWorksheet.php

https://gitlab.com/intelij/Laravel-Excel · PHP · 1089 lines · 534 code · 144 blank · 411 comment · 44 complexity · 33f143b53b7356215ececcd49a4e772f MD5 · raw file

  1. <?php namespace Maatwebsite\Excel\Classes;
  2. use Closure;
  3. use Config;
  4. use PHPExcel_Worksheet;
  5. use Illuminate\Support\Collection;
  6. use Maatwebsite\Excel\Writers\CellWriter;
  7. use Maatwebsite\Excel\Exceptions\LaravelExcelException;
  8. /**
  9. *
  10. * Laravel wrapper for PHPExcel_Worksheet
  11. *
  12. * @category Laravel Excel
  13. * @version 1.0.0
  14. * @package maatwebsite/excel
  15. * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
  16. * @copyright Original Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  17. * @author Maatwebsite <info@maatwebsite.nl>
  18. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  19. */
  20. class LaravelExcelWorksheet extends PHPExcel_Worksheet
  21. {
  22. /**
  23. * Parent
  24. * @var PHPExcel
  25. */
  26. public $_parent;
  27. /**
  28. * Parser
  29. * @var ViewParser
  30. */
  31. protected $parser;
  32. /**
  33. * View
  34. * @var string
  35. */
  36. public $view;
  37. /**
  38. * Data
  39. * @var array
  40. */
  41. public $data = array();
  42. /**
  43. * Merge data
  44. * @var array
  45. */
  46. public $mergeData = array();
  47. /**
  48. * Allowed page setup
  49. * @var array
  50. */
  51. public $allowedPageSetup = array(
  52. 'orientation',
  53. 'paperSize',
  54. 'scale',
  55. 'fitToPage',
  56. 'fitToHeight',
  57. 'fitToWidth',
  58. 'columnsToRepeatAtLeft',
  59. 'rowsToRepeatAtTop',
  60. 'horizontalCentered',
  61. 'verticalCentered',
  62. 'printArea',
  63. 'firstPageNumber'
  64. );
  65. /**
  66. * Allowed page setup
  67. * @var array
  68. */
  69. public $allowedStyles = array(
  70. 'fontFamily',
  71. 'fontSize',
  72. 'fontBold'
  73. );
  74. /**
  75. * Check if the file was autosized
  76. * @var boolean
  77. */
  78. public $hasFixedSizeColumns = false;
  79. /**
  80. * Auto generate table heading
  81. * @var [type]
  82. */
  83. protected $autoGenerateHeading = true;
  84. /**
  85. * Create a new worksheet
  86. *
  87. * @param PHPExcel $pParent
  88. * @param string $pTitle
  89. */
  90. public function __construct(PHPExcel $pParent = null, $pTitle = 'Worksheet')
  91. {
  92. parent::__construct($pParent, $pTitle);
  93. $this->setParent($pParent);
  94. }
  95. /**
  96. * Set default page setup
  97. * @return void
  98. */
  99. public function setDefaultPageSetup()
  100. {
  101. // Get the page setup
  102. $pageSetup = $this->getPageSetup();
  103. foreach($this->allowedPageSetup as $setup)
  104. {
  105. // set the setter
  106. list($setter, $set) = $this->_setSetter($setup);
  107. // get the value
  108. $value = Config::get('excel::sheets.pageSetup.' . $setup, NULL);
  109. // Set the page setup value
  110. if(!is_null($value))
  111. call_user_func_array(array($pageSetup, $setter), array($value));
  112. }
  113. // Set default page margins
  114. $this->setPageMargin(Config::get('excel::export.sheets.page_margin', false));
  115. }
  116. /**
  117. * Set the page margin
  118. * @param array|boolean|integer|float $margin
  119. */
  120. public function setPageMargin($margin = false)
  121. {
  122. if(!is_array($margin))
  123. {
  124. $marginArray = array($margin, $margin, $margin, $margin);
  125. }
  126. else
  127. {
  128. $marginArray = $margin;
  129. }
  130. // Get margin
  131. $pageMargin = $this->getPageMargins();
  132. if(isset($marginArray[0]))
  133. $pageMargin->setTop($marginArray[0]);
  134. if(isset($marginArray[1]))
  135. $pageMargin->setRight($marginArray[1]);
  136. if(isset($marginArray[2]))
  137. $pageMargin->setBottom($marginArray[2]);
  138. if(isset($marginArray[3]))
  139. $pageMargin->setLeft($marginArray[3]);
  140. }
  141. /**
  142. * Manipulate a single row
  143. * @param integer|callback|array $rowNumber
  144. * @param array|callback $callback
  145. * @return LaravelExcelWorksheet
  146. */
  147. public function row($rowNumber, $callback = null)
  148. {
  149. // If a callback is given, handle it with the cell writer
  150. if($callback instanceof Closure)
  151. {
  152. $range = $this->rowToRange($rowNumber);
  153. return $this->cells($range, $callback);
  154. }
  155. // Else if the 2nd param was set, we will use it as a cell value
  156. if(is_array($callback))
  157. {
  158. // Interpret the callback as cell values
  159. $values = $callback;
  160. // Set start column
  161. $column = 'A';
  162. foreach($values as $rowValue)
  163. {
  164. // Set cell coordinate
  165. $cell = $column . $rowNumber;
  166. // Set the cell value
  167. $this->setCellValue($cell, $rowValue);
  168. $column++;
  169. }
  170. }
  171. return $this;
  172. }
  173. /**
  174. * Add multiple rows
  175. * @param array $rows
  176. * @return LaravelExcelWorksheet
  177. */
  178. public function rows($rows = array())
  179. {
  180. // Get the start row
  181. $startRow = $this->getStartRow();
  182. // Add rows
  183. foreach($rows as $row)
  184. {
  185. $this->row($startRow, $row);
  186. $startRow++;
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Prepend a row
  192. * @param integer $rowNumber
  193. * @param array|callback $callback
  194. * @return LaravelExcelWorksheet
  195. */
  196. public function prependRow($rowNumber = 1, $callback = null)
  197. {
  198. // If only one param was given, prepend it before the first row
  199. if(is_null($callback))
  200. {
  201. $callback = $rowNumber;
  202. $rowNumber = 1;
  203. }
  204. // Create new row
  205. $this->insertNewRowBefore($rowNumber);
  206. // Add data to row
  207. return $this->row($rowNumber, $callback);
  208. }
  209. /**
  210. * Append a row
  211. * @param integer|callback $rowNumber
  212. * @param array|callback $callback
  213. * @return LaravelExcelWorksheet
  214. */
  215. public function appendRow($rowNumber = 1, $callback = null)
  216. {
  217. // If only one param was given, add it as very last
  218. if(is_null($callback))
  219. {
  220. $callback = $rowNumber;
  221. $rowNumber = $this->getStartRow();
  222. }
  223. // Add the row
  224. return $this->row($rowNumber, $callback);
  225. }
  226. /**
  227. * Manipulate a single cell
  228. * @param array|string $cell
  229. * @param callback $callback
  230. * @return LaravelExcelWorksheet
  231. */
  232. public function cell($cell, $callback = false)
  233. {
  234. // If a callback is given, handle it with the cell writer
  235. if($callback instanceof Closure)
  236. return $this->cells($cell, $callback);
  237. // Else if the 2nd param was set, we will use it as a cell value
  238. if($callback)
  239. $this->sheet->setCellValue($cell, $callback);
  240. return $this;
  241. }
  242. /**
  243. * Manipulate a cell or a range of cells
  244. * @param array $cells
  245. * @param callback $callback
  246. * @return LaravelExcelWorksheet
  247. */
  248. public function cells($cells, $callback = false)
  249. {
  250. // Init the cell writer
  251. $cells = new CellWriter($cells, $this);
  252. // Do the callback
  253. if($callback instanceof Closure)
  254. call_user_func($callback, $cells);
  255. return $this;
  256. }
  257. /**
  258. * Load a View and convert to HTML
  259. * @param string $view
  260. * @param array $data
  261. * @param array $mergeData
  262. * @return LaravelExcelWorksheet
  263. */
  264. public function setView()
  265. {
  266. return call_user_func_array(array($this, 'loadView'), func_get_args());
  267. }
  268. /**
  269. * Load a View and convert to HTML
  270. * @param string $view
  271. * @param array $data
  272. * @param array $mergeData
  273. * @return LaravelExcelWorksheet
  274. */
  275. public function loadView($view, $data = array(), $mergeData = array())
  276. {
  277. // Init the parser
  278. if(!$this->parser)
  279. $this->setParser();
  280. $this->parser->setView($view);
  281. $this->parser->setData($data);
  282. $this->parser->setMergeData($mergeData);
  283. return $this;
  284. }
  285. /**
  286. * Unset the view
  287. * @return LaravelExcelWorksheet
  288. */
  289. public function unsetView()
  290. {
  291. $this->parser = null;
  292. return $this;
  293. }
  294. /**
  295. * Set the parser
  296. * @param boolean $parser
  297. * @return ViewParser
  298. */
  299. public function setParser($parser = false)
  300. {
  301. return $this->parser = $parser ? $parser : app('excel.parsers.view');
  302. }
  303. /**
  304. * Get the view
  305. * @return ViewParser
  306. */
  307. public function getView()
  308. {
  309. return $this->parser;
  310. }
  311. /**
  312. * Return parsed sheet
  313. * @return LaravelExcelWorksheet
  314. */
  315. public function parsed()
  316. {
  317. // If parser is set, use it
  318. if($this->parser)
  319. return $this->parser->parse($this);
  320. // Else return the entire sheet
  321. return $this;
  322. }
  323. /**
  324. * Set data for the current sheet
  325. * @param string $keys
  326. * @param string $value
  327. * @param boolean $headingGeneration
  328. * @return LaravelExcelWorksheet
  329. */
  330. public function with($key, $value = false, $headingGeneration = true)
  331. {
  332. // Set the heading generation setting
  333. $this->setAutoHeadingGeneration($headingGeneration);
  334. // Add the vars
  335. $this->_addVars($key, $value);
  336. return $this;
  337. }
  338. /**
  339. * From array
  340. * @param Collection|array $source
  341. * @param boolean $headingGeneration
  342. * @return LaravelExcelWorksheet
  343. */
  344. public function fromModel($source = NULL, $nullValue = null, $startCell = false, $strictNullComparison = false, $headingGeneration = true)
  345. {
  346. return $this->fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration);
  347. }
  348. /**
  349. * Fill worksheet from values in array
  350. *
  351. * @param array $source Source array
  352. * @param mixed $nullValue Value in source array that stands for blank cell
  353. * @param string $startCell Insert array starting from this cell address as the top left coordinate
  354. * @param boolean $strictNullComparison Apply strict comparison when testing for null values in the array
  355. * @throws PHPExcel_Exception
  356. * @return LaravelExcelWorksheet
  357. */
  358. public function fromArray($source = null, $nullValue = null, $startCell = false, $strictNullComparison = false, $headingGeneration = true)
  359. {
  360. // Set defaults
  361. $nullValue = !is_null($nullValue) ? $nullValue : $this->getDefaultNullValue();
  362. $startCell = $startCell ? $startCell : $this->getDefaultStartCell();
  363. $strictNullComparison = $strictNullComparison ? $strictNullComparison : $this->getDefaultStrictNullComparison();
  364. // Set the heading generation setting
  365. $this->setAutoHeadingGeneration($headingGeneration);
  366. // Add the vars
  367. $this->_addVars($source);
  368. return parent::fromArray($this->data, $nullValue, $startCell, $strictNullComparison);
  369. }
  370. /**
  371. * Add vars to the data array
  372. * @param string $key
  373. * @param string $value
  374. * @return void
  375. */
  376. protected function _addVars($key, $value = false)
  377. {
  378. // Add array of data
  379. if(is_array($key) || $key instanceof Collection)
  380. {
  381. // Set the data
  382. $this->data = $this->addData($key);
  383. // Create excel from array without a view
  384. if(!$this->parser)
  385. {
  386. $nullValue = $this->getDefaultNullValue();
  387. $startCell = $this->getDefaultStartCell();
  388. $strictNullComparison = $this->getDefaultStrictNullComparison();
  389. return parent::fromArray($this->data, $nullValue, $startCell, $strictNullComparison);
  390. }
  391. }
  392. // Add seperate values
  393. else
  394. {
  395. $this->data[$key] = $value;
  396. }
  397. // Set data to parser
  398. if($this->parser)
  399. $this->parser->setData($this->data);
  400. }
  401. /**
  402. * Add data
  403. * @param array $array
  404. * @return array
  405. */
  406. protected function addData($array)
  407. {
  408. // If a parser was set
  409. if($this->parser)
  410. {
  411. // Don't change anything
  412. $data = $array;
  413. }
  414. else
  415. {
  416. // Transform model/collection to array
  417. if($array instanceof Collection)
  418. $array = $array->toArray();
  419. // Get the firstRow
  420. $firstRow = reset($array);
  421. // Check if the array has array values
  422. if(count($firstRow) != count($firstRow, 1))
  423. {
  424. // Loop through the data to remove arrays
  425. $data = array();
  426. $r = 0;
  427. foreach($array as $row)
  428. {
  429. $data[$r] = array();
  430. foreach($row as $key => $cell)
  431. {
  432. if(!is_array($cell))
  433. {
  434. $data[$r][$key] = $cell;
  435. }
  436. }
  437. $r++;
  438. }
  439. }
  440. else
  441. {
  442. $data = $array;
  443. }
  444. // Check if we should auto add the first row based on the indices
  445. if($this->generateHeadingByIndices())
  446. {
  447. // Get the first row
  448. $firstRow = reset($data);
  449. if(is_array($firstRow))
  450. {
  451. // Get the array keys
  452. $tableHeading = array_keys($firstRow);
  453. // Add table headings as first row
  454. array_unshift($data, $tableHeading);
  455. }
  456. }
  457. }
  458. // Add results
  459. if(!empty($data))
  460. $this->data = !empty($this->data) ? array_merge($this->data, $data) : $data;
  461. // return data
  462. return $this->data;
  463. }
  464. /**
  465. * Set the auto heading generation setting
  466. * @param boolean $boolean
  467. * @return LaravelExcelWorksheet
  468. */
  469. public function setAutoHeadingGeneration($boolean)
  470. {
  471. $this->autoGenerateHeading = $boolean;
  472. return $this;
  473. }
  474. /**
  475. * Disable the heading generation
  476. * @param boolean $boolean
  477. * @return LaravelExcelWorksheet
  478. */
  479. public function disableHeadingGeneration($boolean = false)
  480. {
  481. $this->setAutoHeadingGeneration($boolean);
  482. return $this;
  483. }
  484. /**
  485. * Check if we should auto generate the table heading
  486. * @return boolean
  487. */
  488. protected function generateHeadingByIndices()
  489. {
  490. if(!$this->autoGenerateHeading)
  491. return false;
  492. return Config::get('excel::export.generate_heading_by_indices', false);
  493. }
  494. /**
  495. * Set attributes
  496. * @param string $key
  497. * @param array|string $params
  498. * @return void|PHPExcel_Worksheet_PageSetup
  499. */
  500. public function _setAttributes($setter, $params)
  501. {
  502. // Set the setter and the key
  503. list($setter, $key) = $this->_setSetter($setter);
  504. // If is page setup
  505. if(in_array($key, $this->allowedPageSetup))
  506. {
  507. // Set params
  508. $params = is_array($params) ? $params : array($params);
  509. // Call the setter
  510. return call_user_func_array(array($this->getPageSetup(), $setter), $params);
  511. }
  512. // If is a style
  513. elseif(in_array($key, $this->allowedStyles) )
  514. {
  515. $this->setDefaultStyles($setter, $key, $params);
  516. }
  517. throw new LaravelExcelException('[ERROR] Laravel Worksheet method ['. $setter .'] does not exist.');
  518. }
  519. /**
  520. * Set default styles
  521. * @param string $setter
  522. * @param string $key
  523. * @param array|string $params
  524. * @return PHPExcel_Style
  525. */
  526. protected function setDefaultStyles($setter, $key, $params)
  527. {
  528. $caller = $this->getDefaultStyle();
  529. $params = is_array($params) ? $params : array($params);
  530. if(str_contains($key, 'font'))
  531. return $this->setFontStyle($caller, $setter, $key, $params);
  532. return call_user_func_array(array($caller, $setter), $params);
  533. }
  534. /**
  535. * Set default styles by array
  536. * @param array $styles
  537. * @return LaravelExcelWorksheet
  538. */
  539. public function setStyle($styles)
  540. {
  541. $this->getDefaultStyle()->applyFromArray($styles);
  542. return $this;
  543. }
  544. /**
  545. * Set the font
  546. * @param array $fonts
  547. * @return LaravelExcelWorksheet
  548. */
  549. public function setFont($fonts)
  550. {
  551. foreach($fonts as $key => $value)
  552. {
  553. $this->setFontStyle($this->getDefaultStyle(), $key, $key, $value);
  554. }
  555. return $this;
  556. }
  557. /**
  558. * Set default font styles
  559. * @param string $caller
  560. * @param string $setter
  561. * @param string $key
  562. * @param array|string $params
  563. * @return PHPExcel_Style
  564. */
  565. protected function setFontStyle($caller, $setter, $key, $params)
  566. {
  567. // Set caller to font
  568. $caller = $caller->getFont();
  569. $params = is_array($params) ? $params : array($params);
  570. // Clean the setter name
  571. $key = lcfirst(str_replace('font', '', $key));
  572. // Get setter method
  573. list($setter, $key) = $this->_setSetter($key);
  574. switch($key)
  575. {
  576. case 'family':
  577. $setter = 'setName';
  578. break;
  579. }
  580. return call_user_func_array(array($caller, $setter), $params);
  581. }
  582. /**
  583. * Set the setter
  584. * @param string $setter
  585. * @return array
  586. */
  587. protected function _setSetter($setter)
  588. {
  589. if(starts_with($setter, 'set'))
  590. {
  591. $key = lcfirst(str_replace('set', '', $setter));
  592. }
  593. else
  594. {
  595. $key = $setter;
  596. $setter = 'set' . ucfirst($key);
  597. }
  598. // Return the setter method and the key
  599. return array($setter, $key);
  600. }
  601. /**
  602. * Set the parent (excel object)
  603. * @param PHPExcel $parent
  604. */
  605. public function setParent($parent)
  606. {
  607. $this->_parent = $parent;
  608. }
  609. /**
  610. * Get the parent excel obj
  611. * @return PHPExcel
  612. */
  613. public function getParent()
  614. {
  615. return $this->_parent;
  616. }
  617. /**
  618. * Set the column width
  619. * @param string|array $column
  620. * @param boolean $value
  621. * @return LaravelExcelWorksheet
  622. */
  623. public function setWidth($column, $value = false)
  624. {
  625. // if is array of columns
  626. if(is_array($column))
  627. {
  628. // Set width for each column
  629. foreach($column as $subColumn => $subValue)
  630. {
  631. $this->setWidth($subColumn, $subValue);
  632. }
  633. }
  634. else
  635. {
  636. // Disable the autosize and set column width
  637. $this->getColumnDimension($column)
  638. ->setAutoSize(false)
  639. ->setWidth($value);
  640. // Set autosized to true
  641. $this->hasFixedSizeColumns = true;
  642. }
  643. return $this;
  644. }
  645. /**
  646. * Set the row height
  647. * @param integer|array $row
  648. * @param boolean $value
  649. * @return LaravelExcelWorksheet
  650. */
  651. public function setHeight($row, $value = false)
  652. {
  653. // if is array of columns
  654. if(is_array($row))
  655. {
  656. // Set width for each column
  657. foreach($row as $subRow => $subValue)
  658. {
  659. $this->setHeight($subRow, $subValue);
  660. }
  661. }
  662. else
  663. {
  664. // Set column width
  665. $this->getRowDimension($row)->setRowHeight($value);
  666. }
  667. return $this;
  668. }
  669. /**
  670. * Set cell size
  671. * @param array|string $cell
  672. * @param integer $width
  673. * @param integer $height
  674. * @return LaravelExcelWorksheet
  675. */
  676. public function setSize($cell, $width = false, $height = false)
  677. {
  678. // if is array of columns
  679. if(is_array($cell))
  680. {
  681. // Set width for each column
  682. foreach($cell as $subCell => $sizes)
  683. {
  684. $this->setSize($subCell, reset($sizes), end($sizes));
  685. }
  686. }
  687. else
  688. {
  689. // Split the cell to column and row
  690. list($column, $row) = preg_split('/(?<=[a-z])(?=[0-9]+)/i',$cell);
  691. if($column)
  692. $this->setWidth($column, $width);
  693. if($row)
  694. $this->setHeight($row, $height);
  695. }
  696. return $this;
  697. }
  698. /**
  699. * Autosize column for document
  700. * @param array|boolean $columns
  701. * @return void
  702. */
  703. public function setAutoSize($columns = false)
  704. {
  705. // Remember that the sheet was autosized
  706. $this->hasFixedSizeColumns = $columns || !empty($columns) ? false : true;
  707. // Set autosize to true
  708. $this->autoSize = $columns ? $columns : false;
  709. // If is not an array
  710. if(!is_array($columns) && $columns)
  711. {
  712. // Get the highest column
  713. $toCol = $this->getHighestColumn();
  714. // Lop through the columns and set the auto size
  715. $toCol++;
  716. for ($i = 'A'; $i !== $toCol; $i++) {
  717. $this->getColumnDimension($i)->setAutoSize(true);
  718. }
  719. }
  720. // Set autosize for the given columns
  721. elseif(is_array($columns))
  722. {
  723. foreach($columns as $column)
  724. {
  725. $this->getColumnDimension($column)->setAutoSize(true);
  726. }
  727. }
  728. // Calculate the column widths
  729. $this->calculateColumnWidths();
  730. return $this;
  731. }
  732. /**
  733. * Get Auto size
  734. * @return bool
  735. */
  736. public function getAutosize()
  737. {
  738. if(isset($this->autoSize))
  739. return $this->autoSize;
  740. return Config::get('excel::export.autosize', true);
  741. }
  742. /**
  743. * Check if the sheet was auto sized dynamically
  744. * @return boolean
  745. */
  746. public function hasFixedSizeColumns()
  747. {
  748. return $this->hasFixedSizeColumns ? true : false;
  749. }
  750. /**
  751. * Set the auto filter
  752. * @param boolean $value
  753. * @return LaravelExcelWorksheet
  754. */
  755. public function setAutoFilter($value = false)
  756. {
  757. $value = $value ? $value : $this->calculateWorksheetDimension();
  758. parent::setAutoFilter($value);
  759. return $this;
  760. }
  761. /**
  762. * Freeze or lock rows and columns
  763. * @param string $pane rows and columns
  764. * @return LaravelExcelWorksheet
  765. */
  766. public function setFreeze($pane = 'A2')
  767. {
  768. $this->freezePane($pane);
  769. return $this;
  770. }
  771. /**
  772. * Freeze the first row
  773. * @return LaravelExcelWorksheet
  774. */
  775. public function freezeFirstRow()
  776. {
  777. $this->setFreeze('A2');
  778. return $this;
  779. }
  780. /**
  781. * Freeze the first column
  782. * @return LaravelExcelWorksheet
  783. */
  784. public function freezeFirstColumn()
  785. {
  786. $this->setFreeze('B1');
  787. return $this;
  788. }
  789. /**
  790. * Freeze the first row and column
  791. * @return LaravelExcelWorksheet
  792. */
  793. public function freezeFirstRowAndColumn()
  794. {
  795. $this->setFreeze('B2');
  796. return $this;
  797. }
  798. /**
  799. * Set a range of cell borders
  800. * @param string $pane Start and end of the cell (A1:F10)
  801. * @param string $weight Border style
  802. * @return LaravelExcelWorksheet
  803. */
  804. public function setBorder($pane = 'A1', $weight = 'thin')
  805. {
  806. // Set all borders
  807. $this->getStyle($pane)
  808. ->getBorders()
  809. ->getAllBorders()
  810. ->setBorderStyle($weight);
  811. return $this;
  812. }
  813. /**
  814. * Set all cell borders
  815. * @param string $weight Border style (Reference setBorder style list)
  816. * @return LaravelExcelWorksheet
  817. */
  818. public function setAllBorders($weight = 'thin')
  819. {
  820. $styleArray = array(
  821. 'borders' => array(
  822. 'allborders' => array(
  823. 'style' => $weight
  824. )
  825. )
  826. );
  827. // Apply the style
  828. $this->getDefaultStyle()
  829. ->applyFromArray($styleArray);
  830. return $this;
  831. }
  832. /**
  833. * Set the cell format of the column
  834. * @param array $formats An array of cells you want to format columns
  835. * @return LaravelExcelWorksheet
  836. */
  837. public function setColumnFormat(Array $formats){
  838. // Loop through the columns
  839. foreach ($formats as $column => $format) {
  840. // Change the format for a specific cell or range
  841. $this->getStyle($column)
  842. ->getNumberFormat()
  843. ->setFormatCode($format);
  844. }
  845. return $this;
  846. }
  847. /**
  848. * Merge cells
  849. * @param string $pRange
  850. * @return LaravelExcelWorksheet
  851. */
  852. public function mergeCells($pRange = 'A1:A1', $alignment = false)
  853. {
  854. // Merge the cells
  855. parent::mergeCells($pRange);
  856. // Set center alignment on merge cells
  857. $this->cells($pRange, function($cell) use ($alignment) {
  858. $aligment = is_string($alignment) ? $alignment : Config::get('excel::export.merged_cell_alignment', 'left');
  859. $cell->setAlignment($aligment);
  860. });
  861. return $this;
  862. }
  863. /**
  864. * Set the columns you want to merge
  865. * @return LaravelExcelWorksheet
  866. * @param array $mergeColumn An array of columns you want to merge
  867. */
  868. public function setMergeColumn(Array $mergeColumn, $alignment = false)
  869. {
  870. foreach ($mergeColumn['columns'] as $column) {
  871. foreach ($mergeColumn['rows'] as $row) {
  872. $this->mergeCells($column.$row[0].":".$column.$row[1], $alignment);
  873. }
  874. }
  875. return $this;
  876. }
  877. /**
  878. * Return the start row
  879. * @return integer
  880. */
  881. protected function getStartRow()
  882. {
  883. if($this->getHighestRow() == 1)
  884. return 1;
  885. return $this->getHighestRow() + 1;
  886. }
  887. /**
  888. * Return range from row
  889. * @param integer $rowNumber
  890. * @return string $range
  891. */
  892. protected function rowToRange($rowNumber)
  893. {
  894. return 'A' . $rowNumber . ':' . $this->getHighestColumn() . $rowNumber;
  895. }
  896. /**
  897. * Return default null value
  898. * @return string|integer|null
  899. */
  900. protected function getDefaultNullValue()
  901. {
  902. return Config::get('excel::export.sheets.nullValue', null);
  903. }
  904. /**
  905. * Return default null value
  906. * @return string|integer|null
  907. */
  908. protected function getDefaultStartCell()
  909. {
  910. return Config::get('excel::export.sheets.startCell', 'A1');
  911. }
  912. /**
  913. * Return default strict null comparison
  914. * @return boolean
  915. */
  916. protected function getDefaultStrictNullComparison()
  917. {
  918. return Config::get('excel::export.sheets.strictNullComparison', false);
  919. }
  920. /**
  921. * Dynamically call methods
  922. * @param string $method
  923. * @param array $params
  924. * @throws LaravelExcelException
  925. * @return LaravelExcelWorksheet
  926. */
  927. public function __call($method, $params)
  928. {
  929. // If the dynamic call starts with "with", add the var to the data array
  930. if(starts_with($method, 'with'))
  931. {
  932. $key = lcfirst(str_replace('with', '', $method));
  933. $this->_addVars($key, reset($params));
  934. return $this;
  935. }
  936. // If it's a setter
  937. elseif(starts_with($method, 'set') )
  938. {
  939. // set the attribute
  940. $this->_setAttributes($method, $params);
  941. return $this;
  942. }
  943. throw new LaravelExcelException('[ERROR] Laravel Worksheet method ['. $method .'] does not exist.');
  944. }
  945. }