GC0202 /
ThinkLibrary
| 1 | <?php |
||
| 2 | |||
| 3 | // +---------------------------------------------------------------------- |
||
| 4 | // | ThinkLibrary 6.0 for ThinkPhP 6.0 |
||
| 5 | // +---------------------------------------------------------------------- |
||
| 6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||
| 7 | // +---------------------------------------------------------------------- |
||
| 8 | // | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
||
| 9 | // +---------------------------------------------------------------------- |
||
| 10 | // | 开源协议 ( https://mit-license.org ) |
||
| 11 | // +---------------------------------------------------------------------- |
||
| 12 | // | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
||
| 13 | // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
||
| 14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||
| 15 | // +---------------------------------------------------------------------- |
||
| 16 | |||
| 17 | namespace DtApp\ThinkLibrary\service; |
||
| 18 | |||
| 19 | use DtApp\ThinkLibrary\Service; |
||
| 20 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
||
| 21 | use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
||
| 22 | use think\Exception; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * 表格服务 |
||
| 26 | * Class ExcelService |
||
| 27 | * @package DtApp\ThinkLibrary\service |
||
| 28 | */ |
||
| 29 | class ExcelService extends Service |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * 头部 |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $head = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * 设置头部 |
||
| 39 | * [ |
||
| 40 | * [ |
||
| 41 | * [ |
||
| 42 | * 'index' => 1, |
||
| 43 | * 'value' => '标题' |
||
| 44 | * ], |
||
| 45 | * [ |
||
| 46 | * 'index' => 2, |
||
| 47 | * 'value' => '名称' |
||
| 48 | * ] |
||
| 49 | * ], |
||
| 50 | * [ |
||
| 51 | * [ |
||
| 52 | * 'index' => 1, |
||
| 53 | * 'value' => '标题2' |
||
| 54 | * ], |
||
| 55 | * [ |
||
| 56 | * 'index' => 2, |
||
| 57 | * 'value' => '名称2' |
||
| 58 | * ] |
||
| 59 | * ] |
||
| 60 | * ]; |
||
| 61 | * @param array $head |
||
| 62 | * @return ExcelService |
||
| 63 | */ |
||
| 64 | public function setHead(array $head = []): ExcelService |
||
| 65 | { |
||
| 66 | $this->head = $head; |
||
| 67 | return $this; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * 头部长度 |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $head_length = 0; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 设置头部长度 |
||
| 78 | * @param int $length |
||
| 79 | * @return ExcelService |
||
| 80 | */ |
||
| 81 | public function setHeadLength(int $length = 0): ExcelService |
||
| 82 | { |
||
| 83 | $this->head_length = $length; |
||
|
0 ignored issues
–
show
|
|||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * 内容 |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private $content = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * 设置内容 |
||
| 95 | * [ |
||
| 96 | * [ |
||
| 97 | * [ |
||
| 98 | * 'index' => 1, |
||
| 99 | * 'value' => '标题' |
||
| 100 | * ], |
||
| 101 | * [ |
||
| 102 | * 'index' => 2, |
||
| 103 | * 'value' => '名称' |
||
| 104 | * ] |
||
| 105 | * ], |
||
| 106 | * [ |
||
| 107 | * [ |
||
| 108 | * 'index' => 1, |
||
| 109 | * 'value' => '标题2' |
||
| 110 | * ], |
||
| 111 | * [ |
||
| 112 | * 'index' => 2, |
||
| 113 | * 'value' => '名称2' |
||
| 114 | * ] |
||
| 115 | * ] |
||
| 116 | * ]; |
||
| 117 | * @param array $content |
||
| 118 | * @return ExcelService |
||
| 119 | */ |
||
| 120 | public function setContent(array $content = []): ExcelService |
||
| 121 | { |
||
| 122 | $this->content = $content; |
||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * 文件名 |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | private $file_name = ''; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * 设置文件名(不需要后缀名) |
||
| 134 | * @param string $file_name |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function setFileName(string $file_name = ''): self |
||
| 138 | { |
||
| 139 | $this->file_name = $file_name; |
||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * 生成表格文件 |
||
| 145 | * @return string |
||
| 146 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | public function generate(): string |
||
| 150 | { |
||
| 151 | // 生成表格 |
||
| 152 | $spreadsheet = new Spreadsheet(); |
||
| 153 | $sheet = $spreadsheet->getActiveSheet(); |
||
| 154 | if (empty($this->head)) { |
||
| 155 | throw new Exception('头部内容未设置!'); |
||
| 156 | } |
||
| 157 | if (empty($this->head_length)) { |
||
| 158 | throw new Exception('头部长度未设置!'); |
||
| 159 | } |
||
| 160 | if (empty($this->file_name)) { |
||
| 161 | throw new Exception('文件保存路径未设置!'); |
||
| 162 | } |
||
| 163 | //设置工作表标题名称 |
||
| 164 | //设置单元格内容 |
||
| 165 | foreach ($this->head as $key => $value) { |
||
| 166 | foreach ($value as $k => $v) { |
||
| 167 | $sheet->setCellValueByColumnAndRow($v['index'], $key + 1, $v['value']); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | foreach ($this->content as $key => $value) { |
||
| 171 | foreach ($value as $k => $v) { |
||
| 172 | $sheet->setCellValueByColumnAndRow($v['index'], $key + $this->head_length, $v['value']); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | $writer = new Xlsx($spreadsheet); |
||
| 176 | $writer->save("{$this->file_name}.xlsx"); |
||
| 177 | |||
| 178 | return "{$this->file_name}.xlsx"; |
||
| 179 | } |
||
| 180 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..