123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace App\Exports;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\WithEvents;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Events\AfterSheet;
- use PhpOffice\PhpSpreadsheet\Style\Border;
- /**
- * 导出Excel类
- * @author 牧羊人
- * @since 2021/4/10
- * Class Export
- * @package App\Exports
- */
- class Export implements FromCollection, WithHeadings, WithEvents
- {
- protected $data;
- protected $headings;
- protected $columnWidth = [];//设置列宽 key:列 value:宽
- protected $rowHeight = []; //设置行高 key:行 value:高
- protected $mergeCells = []; //合并单元格 value:A1:K8
- protected $font = []; //设置字体 key:A1:K8 value:Arial
- protected $fontSize = []; //设置字体大小 key:A1:K8 value:11
- protected $bold = []; //设置粗体 key:A1:K8 value:true
- protected $background = []; //设置背景颜色 key:A1:K8 value:#F0F0F0F
- protected $vertical = []; //设置定位 key:A1:K8 value:center
- protected $sheetName; //sheet title
- protected $borders = []; //设置边框颜色 key:A1:K8 value:#000000
- //设置页面属性时如果无效 更改excel格式尝试即可
- //构造函数传值
- public function __construct($data, $headings, $sheetName)
- {
- $this->data = $data;
- $this->headings = $headings;
- $this->sheetName = $sheetName;
- $this->createData();
- }
- public function headings(): array
- {
- return $this->headings;
- }
- //数组转集合
- public function collection()
- {
- return new Collection($this->data);
- }
- //业务代码
- public function createData()
- {
- $this->data = collect($this->data)->toArray();
- }
- public function registerEvents(): array
- {
- return [
- AfterSheet::class => function (AfterSheet $event) {
- //设置区域单元格垂直居中
- $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
- //设置区域单元格水平居中
- $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
- //设置列宽
- foreach ($this->columnWidth as $column => $width) {
- $event->sheet->getDelegate()
- ->getColumnDimension($column)
- ->setWidth($width);
- }
- //设置行高,$i为数据行数
- foreach ($this->rowHeight as $row => $height) {
- $event->sheet->getDelegate()
- ->getRowDimension($row)
- ->setRowHeight($height);
- }
- //设置区域单元格垂直居中
- foreach ($this->vertical as $region => $position) {
- $event->sheet->getDelegate()
- ->getStyle($region)
- ->getAlignment()
- ->setVertical($position);
- }
- //设置区域单元格字体
- foreach ($this->font as $region => $value) {
- $event->sheet->getDelegate()
- ->getStyle($region)
- ->getFont()->setName($value);
- }
- //设置区域单元格字体大小
- foreach ($this->fontSize as $region => $value) {
- $event->sheet->getDelegate()
- ->getStyle($region)
- ->getFont()
- ->setSize($value);
- }
- //设置区域单元格字体粗体
- foreach ($this->bold as $region => $bool) {
- $event->sheet->getDelegate()
- ->getStyle($region)
- ->getFont()
- ->setBold($bool);
- }
- //设置区域单元格背景颜色
- foreach ($this->background as $region => $item) {
- $event->sheet->getDelegate()->getStyle($region)->applyFromArray([
- 'fill' => [
- 'fillType' => 'linear', //线性填充,类似渐变
- 'startColor' => [
- 'rgb' => $item //初始颜色
- ],
- //结束颜色,如果需要单一背景色,请和初始颜色保持一致
- 'endColor' => [
- 'argb' => $item
- ]
- ]
- ]);
- }
- //设置边框颜色
- foreach ($this->borders as $region => $item) {
- $event->sheet->getDelegate()->getStyle($region)->applyFromArray([
- 'borders' => [
- 'allBorders' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => $item],
- ],
- ],
- ]);
- }
- //合并单元格
- $event->sheet->getDelegate()->setMergeCells($this->mergeCells);
- if (!empty($this->sheetName)) {
- $event->sheet->getDelegate()->setTitle($this->sheetName);
- }
- }
- ];
- }
- /**
- * @return array
- * [
- * 'B' => 40,
- * 'C' => 60
- * ]
- */
- public function setColumnWidth(array $columnwidth)
- {
- $this->columnWidth = array_change_key_case($columnwidth, CASE_UPPER);
- }
- /**
- * @return array
- * [
- * 1 => 40,
- * 2 => 60
- * ]
- */
- public function setRowHeight(array $rowHeight)
- {
- $this->rowHeight = $rowHeight;
- }
- /**
- * @return array
- * [
- * A1:K7 => '宋体'
- * ]
- */
- public function setFont(array $font)
- {
- $this->font = array_change_key_case($font, CASE_UPPER);
- }
- /**
- * @return array
- * @2020/3/22 10:33
- * [
- * A1:K7 => true
- * ]
- */
- public function setBold(array $bold)
- {
- $this->bold = array_change_key_case($bold, CASE_UPPER);
- }
- /**
- * @return array
- * @2020/3/22 10:33
- * [
- * A1:K7 => F0FF0F
- * ]
- */
- public function setBackground(array $background)
- {
- $this->background = array_change_key_case($background, CASE_UPPER);
- }
- /**
- * @return array
- * [
- * A1:K7
- * ]
- */
- public function setMergeCells(array $mergeCells)
- {
- $this->mergeCells = array_change_key_case($mergeCells, CASE_UPPER);
- }
- /**
- * @return array
- * [
- * A1:K7 => 14
- * ]
- */
- public function setFontSize(array $fontSize)
- {
- $this->fontSize = array_change_key_case($fontSize, CASE_UPPER);
- }
- /**
- * @return array
- * [
- * A1:K7 => #000000
- * ]
- */
- public function setBorders(array $borders)
- {
- $this->borders = array_change_key_case($borders, CASE_UPPER);
- }
- }
|