123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- declare(strict_types=1);
- namespace catcher\library\excel;
- trait MacroExcel
- {
- /**
- * @var string
- */
- protected $start = 'A';
- /**
- * 开始行
- *
- * @var int
- */
- protected $row = 1;
- /**
- * @var array
- */
- protected $columns = [];
- /**
- * 设置开始的单元
- *
- * @time 2020年05月25日
- * @return string
- */
- protected function getStartSheet(): string
- {
- if (method_exists($this->excel, 'start')) {
- $this->start = $this->excel->start();
- }
- return $this->start;
- }
- /**
- * 设置单元格宽度
- *
- * @time 2020年05月25日
- * @return void
- */
- protected function setSheetWidth()
- {
- if (method_exists($this->excel, 'setWidth')) {
- $width = $this->excel->setWidth();
- foreach ($width as $sheet => $w) {
- $this->getWorksheet()->getColumnDimension($sheet)->setWidth($w);
- }
- }
- }
- /**
- * before
- *
- * @time 2020年05月25日
- * @return void
- */
- protected function before()
- {
- if (method_exists($this->excel, 'before')) {
- $this->excel->before();
- }
- }
- /**
- * 设置 column 信息 ['A', 'B', 'C' ...]
- *
- * @time 2020年05月25日
- * @return array
- */
- protected function getSheetColumns()
- {
- if (empty($this->columns)) {
- $start = $this->getStartSheet();
- $columns = [];
- // 通过 headers 推断需要的 columns
- foreach ($this->excel->headers() as $k => $header) {
- $columns[] = chr(ord($start) + $k);
- }
- return $columns;
- }
- return $this->columns;
- }
- /**
- * set keys
- *
- * @time 2020年05月25日
- * @return array
- */
- protected function getKeys()
- {
- if (method_exists($this->excel, 'keys')) {
- return $this->excel->keys();
- }
- return [];
- }
- /**
- * set start row
- *
- * @time 2020年05月25日
- * @return int
- */
- protected function getStartRow()
- {
- if (method_exists($this->excel, 'setRow')) {
- $this->row = $this->excel->setRow();
- }
- return $this->row;
- }
- /**
- * 设置 title
- *
- * @time 2020年05月25日
- * @return void
- */
- protected function setTitle()
- {
- if (method_exists($this->excel, 'setTitle')) {
- [$cells, $title, $style] = $this->excel->setTitle();
- $this->getWorksheet()
- ->mergeCells($cells) // 合并单元格
- ->setCellValue(explode(':', $cells)[0], $title)
- ->getStyle($cells) // 设置样式
- ->getAlignment()
- ->setHorizontal($style);
- }
- }
- /**
- * register worksheet for excel
- *
- * @time 2020年05月25日
- * @return void
- */
- protected function registerWorksheet()
- {
- if (method_exists($this->excel, 'getWorksheet')) {
- $this->excel->getWorksheet($this->getWorksheet());
- }
- }
- /**
- * 增加 start row
- *
- * @time 2020年05月25日
- * @return void
- */
- protected function incRow()
- {
- ++$this->row;
- }
- /**
- * 设置内存限制
- *
- * @time 2020年05月26日
- * @return void
- */
- public function setMemoryLimit()
- {
- if (property_exists($this->excel, 'memory')) {
- ini_set('memory_limit', $this->excel->memory);
- }
- }
- }
|