|
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
|
|
|
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary |
|
15
|
|
|
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary |
|
16
|
|
|
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git |
|
17
|
|
|
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
|
18
|
|
|
// +---------------------------------------------------------------------- |
|
19
|
|
|
|
|
20
|
|
|
namespace DtApp\ThinkLibrary\service; |
|
21
|
|
|
|
|
22
|
|
|
use DtApp\ThinkLibrary\Service; |
|
23
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
24
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
|
25
|
|
|
use think\Exception; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* 表格服务 |
|
29
|
|
|
* Class ExcelService |
|
30
|
|
|
* @package DtApp\ThinkLibrary\service |
|
31
|
|
|
*/ |
|
32
|
|
|
class ExcelService extends Service |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* 头部 |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $head = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 设置头部 |
|
42
|
|
|
* [ |
|
43
|
|
|
* [ |
|
44
|
|
|
* [ |
|
45
|
|
|
* 'index' => 1, |
|
46
|
|
|
* 'value' => '标题' |
|
47
|
|
|
* ], |
|
48
|
|
|
* [ |
|
49
|
|
|
* 'index' => 2, |
|
50
|
|
|
* 'value' => '名称' |
|
51
|
|
|
* ] |
|
52
|
|
|
* ], |
|
53
|
|
|
* [ |
|
54
|
|
|
* [ |
|
55
|
|
|
* 'index' => 1, |
|
56
|
|
|
* 'value' => '标题2' |
|
57
|
|
|
* ], |
|
58
|
|
|
* [ |
|
59
|
|
|
* 'index' => 2, |
|
60
|
|
|
* 'value' => '名称2' |
|
61
|
|
|
* ] |
|
62
|
|
|
* ] |
|
63
|
|
|
* ]; |
|
64
|
|
|
* @param array $head |
|
65
|
|
|
* @return ExcelService |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setHead(array $head = []): ExcelService |
|
68
|
|
|
{ |
|
69
|
|
|
$this->head = $head; |
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* 头部长度 |
|
75
|
|
|
* @var array |
|
76
|
|
|
*/ |
|
77
|
|
|
private $head_length = 0; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* 设置头部长度 |
|
81
|
|
|
* @param int $length |
|
82
|
|
|
* @return ExcelService |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setHeadLength(int $length = 0): ExcelService |
|
85
|
|
|
{ |
|
86
|
|
|
$this->head_length = $length; |
|
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* 内容 |
|
92
|
|
|
* @var array |
|
93
|
|
|
*/ |
|
94
|
|
|
private $content = []; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* 设置内容 |
|
98
|
|
|
* [ |
|
99
|
|
|
* [ |
|
100
|
|
|
* [ |
|
101
|
|
|
* 'index' => 1, |
|
102
|
|
|
* 'value' => '标题' |
|
103
|
|
|
* ], |
|
104
|
|
|
* [ |
|
105
|
|
|
* 'index' => 2, |
|
106
|
|
|
* 'value' => '名称' |
|
107
|
|
|
* ] |
|
108
|
|
|
* ], |
|
109
|
|
|
* [ |
|
110
|
|
|
* [ |
|
111
|
|
|
* 'index' => 1, |
|
112
|
|
|
* 'value' => '标题2' |
|
113
|
|
|
* ], |
|
114
|
|
|
* [ |
|
115
|
|
|
* 'index' => 2, |
|
116
|
|
|
* 'value' => '名称2' |
|
117
|
|
|
* ] |
|
118
|
|
|
* ] |
|
119
|
|
|
* ]; |
|
120
|
|
|
* @param array $content |
|
121
|
|
|
* @return ExcelService |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setContent(array $content = []): ExcelService |
|
124
|
|
|
{ |
|
125
|
|
|
$this->content = $content; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* 文件名 |
|
131
|
|
|
* @var string |
|
132
|
|
|
*/ |
|
133
|
|
|
private $file_name = ''; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* 设置文件名(不需要后缀名) |
|
137
|
|
|
* @param string $file_name |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setFileName(string $file_name = ''): self |
|
141
|
|
|
{ |
|
142
|
|
|
$this->file_name = $file_name; |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* 生成表格文件 |
|
148
|
|
|
* @return string |
|
149
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
|
150
|
|
|
* @throws Exception |
|
151
|
|
|
*/ |
|
152
|
|
|
public function generate(): string |
|
153
|
|
|
{ |
|
154
|
|
|
// 生成表格 |
|
155
|
|
|
$spreadsheet = new Spreadsheet(); |
|
156
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
|
157
|
|
|
if (empty($this->head)) { |
|
158
|
|
|
throw new Exception('头部内容未设置!'); |
|
159
|
|
|
} |
|
160
|
|
|
if (empty($this->head_length)) { |
|
161
|
|
|
throw new Exception('头部长度未设置!'); |
|
162
|
|
|
} |
|
163
|
|
|
if (empty($this->file_name)) { |
|
164
|
|
|
throw new Exception('文件保存路径未设置!'); |
|
165
|
|
|
} |
|
166
|
|
|
//设置工作表标题名称 |
|
167
|
|
|
//设置单元格内容 |
|
168
|
|
|
foreach ($this->head as $key => $value) { |
|
169
|
|
|
foreach ($value as $k => $v) { |
|
170
|
|
|
$sheet->setCellValueByColumnAndRow($v['index'], $key + 1, $v['value']); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
foreach ($this->content as $key => $value) { |
|
174
|
|
|
foreach ($value as $k => $v) { |
|
175
|
|
|
$sheet->setCellValueByColumnAndRow($v['index'], $key + $this->head_length, $v['value']); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
$writer = new Xlsx($spreadsheet); |
|
179
|
|
|
$writer->save("{$this->file_name}.xlsx"); |
|
180
|
|
|
|
|
181
|
|
|
return "{$this->file_name}.xlsx"; |
|
182
|
|
|
} |
|
183
|
|
|
} |
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..