@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | /** |
34 | 34 | * __construct |
35 | 35 | */ |
36 | - public function __construct($config=[]){ |
|
36 | + public function __construct($config = []) { |
|
37 | 37 | $this->setConfig($config); |
38 | 38 | |
39 | 39 | $this->spreadSheet = new Spreadsheet(); |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * @param $config |
47 | 47 | * @return $this |
48 | 48 | */ |
49 | - public function setConfig($config){ |
|
49 | + public function setConfig($config) { |
|
50 | 50 | foreach ($config as $key => $value) { |
51 | 51 | if (property_exists($this, $key)) { |
52 | 52 | $this->$key = $value; |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | { |
65 | 65 | /** 实例化定义默认excel **/ |
66 | 66 | $this->spreadSheet->getProperties()->setCreator($this->creator)->setLastModifiedBy($this->creator); |
67 | - if($this->horizontalCenter){ |
|
67 | + if ($this->horizontalCenter) { |
|
68 | 68 | $this->spreadSheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //默认水平居中 |
69 | 69 | $this->spreadSheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); //默认垂直居中 |
70 | 70 | $this->spreadSheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::VERTICAL_CENTER); //默认垂直居中 |
@@ -77,19 +77,19 @@ discard block |
||
77 | 77 | * @return $this |
78 | 78 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
79 | 79 | */ |
80 | - public function createWorkSheet($sheetName="Worksheet") |
|
80 | + public function createWorkSheet($sheetName = "Worksheet") |
|
81 | 81 | { |
82 | 82 | $this->sheetName = $sheetName; |
83 | 83 | /** @var workSheet */ |
84 | 84 | $this->workSheet = $this->greateWorkSheet($sheetName); |
85 | - if($this->workSheet == null){ |
|
86 | - if($this->sheetCount==1){ |
|
85 | + if ($this->workSheet == null) { |
|
86 | + if ($this->sheetCount == 1) { |
|
87 | 87 | $this->workSheet = $this->spreadSheet->getActiveSheet(); |
88 | - }else{ |
|
88 | + } else { |
|
89 | 89 | $this->workSheet = $this->spreadSheet->createSheet(); |
90 | 90 | } |
91 | - $this->sheetCount += 1;//总sheet数量 |
|
92 | - $this->workSheet->setTitle($sheetName);//设置sheet名称 |
|
91 | + $this->sheetCount += 1; //总sheet数量 |
|
92 | + $this->workSheet->setTitle($sheetName); //设置sheet名称 |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | /** 初始化当前workSheet */ |
@@ -115,13 +115,13 @@ discard block |
||
115 | 115 | public function generate() |
116 | 116 | { |
117 | 117 | /** 开启自动筛选 **/ |
118 | - if($this->autoFilter){ |
|
118 | + if ($this->autoFilter) { |
|
119 | 119 | $this->spreadSheet->getActiveSheet()->setAutoFilter( |
120 | 120 | $this->spreadSheet->getActiveSheet()->calculateWorksheetDimension() |
121 | 121 | ); |
122 | 122 | } |
123 | 123 | //文件存储 |
124 | - if(empty($this->fileName)){ |
|
124 | + if (empty($this->fileName)) { |
|
125 | 125 | $this->getFileName($this->sheetName); |
126 | 126 | } |
127 | 127 | return $this; |
@@ -132,8 +132,8 @@ discard block |
||
132 | 132 | * @param $file_name |
133 | 133 | * @return string |
134 | 134 | */ |
135 | - private function getFileName($sheetName){ |
|
136 | - $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111,999).'.xlsx'; |
|
135 | + private function getFileName($sheetName) { |
|
136 | + $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111, 999).'.xlsx'; |
|
137 | 137 | return $this->fileName; |
138 | 138 | } |
139 | 139 | |
@@ -143,18 +143,18 @@ discard block |
||
143 | 143 | * @return void |
144 | 144 | * @throws ExceptionAlias |
145 | 145 | */ |
146 | - public function download($filename=''){ |
|
147 | - if(empty($filename)){ |
|
146 | + public function download($filename = '') { |
|
147 | + if (empty($filename)) { |
|
148 | 148 | $filename = $this->fileName; |
149 | - }else{ |
|
149 | + } else { |
|
150 | 150 | $filename = $this->getFileName($filename); |
151 | 151 | } |
152 | 152 | |
153 | 153 | /** 输出下载 **/ |
154 | - ob_end_clean();//清除缓冲区,避免乱码 |
|
155 | - header( 'Access-Control-Allow-Headers:responsetype,content-type,usertoken'); |
|
156 | - header( 'Access-Control-Allow-Methods:GET,HEAD,PUT,POST,DELETE,PATCH'); |
|
157 | - header( 'Access-Control-Allow-Origin:*'); |
|
154 | + ob_end_clean(); //清除缓冲区,避免乱码 |
|
155 | + header('Access-Control-Allow-Headers:responsetype,content-type,usertoken'); |
|
156 | + header('Access-Control-Allow-Methods:GET,HEAD,PUT,POST,DELETE,PATCH'); |
|
157 | + header('Access-Control-Allow-Origin:*'); |
|
158 | 158 | header('Content-Type: application/vnd.ms-excel'); |
159 | 159 | header('Content-Disposition: attachment;filename="'.$filename); |
160 | 160 | header('Cache-Control: max-age=0'); |
@@ -171,12 +171,12 @@ discard block |
||
171 | 171 | * @return string |
172 | 172 | * @throws ExceptionAlias |
173 | 173 | */ |
174 | - public function save($filename='',$pathName=''): string |
|
174 | + public function save($filename = '', $pathName = ''): string |
|
175 | 175 | { |
176 | 176 | $pathName = $this->getPathName($pathName); |
177 | - if(empty($filename)){ |
|
177 | + if (empty($filename)) { |
|
178 | 178 | $filename = $this->fileName; |
179 | - }else{ |
|
179 | + } else { |
|
180 | 180 | $filename = $this->getFileName($filename); |
181 | 181 | } |
182 | 182 | FileTool::mkdir($pathName); |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * 注意:分组字段必须在title中定义 |
14 | 14 | */ |
15 | 15 | $fileTitle = [ |
16 | - 'title_row' => 3, // 表头占用行数 |
|
16 | + 'title_row' => 3, // 表头占用行数 |
|
17 | 17 | 'group_left' => ['id'], // 以id分组 |
18 | 18 | 'mergeColumns' => ['meeting_name', 'time'], // 需要自动合并的字段 |
19 | 19 | 'title' => [ |
@@ -96,13 +96,13 @@ discard block |
||
96 | 96 | |
97 | 97 | // 配置参数 |
98 | 98 | $config = [ |
99 | - 'horizontalCenter' => true, // 是否居中 |
|
100 | - 'titleHeight' => 22, // 定义表头行高 |
|
101 | - 'titleWidth' => 20, // 定义表头列宽 |
|
102 | - 'height' => 22, // 定义数据行高 |
|
103 | - 'autoFilter' => false, // 开启自动筛选 |
|
104 | - 'freezePane' => false, // 冻结窗格(首行首列) |
|
105 | - 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
99 | + 'horizontalCenter' => true, // 是否居中 |
|
100 | + 'titleHeight' => 22, // 定义表头行高 |
|
101 | + 'titleWidth' => 20, // 定义表头列宽 |
|
102 | + 'height' => 22, // 定义数据行高 |
|
103 | + 'autoFilter' => false, // 开启自动筛选 |
|
104 | + 'freezePane' => false, // 冻结窗格(首行首列) |
|
105 | + 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
106 | 106 | // 更多样式请查询 https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#styling-cells |
107 | 107 | 'sheetStyle' => [ |
108 | 108 | 'font' => [ |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | |
126 | 126 | // 第2行:导出时间 |
127 | 127 | $sheet->mergeCells('A2:E2'); |
128 | - $sheet->setCellValue('A2', '导出时间:' . date('Y-m-d')); |
|
128 | + $sheet->setCellValue('A2', '导出时间:'.date('Y-m-d')); |
|
129 | 129 | $sheet->getStyle('A2')->getFont()->setSize(10)->getColor()->setRGB('888888'); |
130 | 130 | $sheet->getStyle('A2')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT); |
131 | 131 |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | use tinymeng\tools\exception\StatusCode; |
13 | 13 | use tinymeng\tools\exception\TinymengException; |
14 | 14 | |
15 | -trait TWorkSheet{ |
|
15 | +trait TWorkSheet { |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * sheet名称 |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * 文件信息 |
46 | 46 | * @var array |
47 | 47 | */ |
48 | - private $fileTitle=[]; |
|
48 | + private $fileTitle = []; |
|
49 | 49 | |
50 | 50 | |
51 | 51 | /** |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | * @param $data |
95 | 95 | * @return $this |
96 | 96 | */ |
97 | - public function setData($data){ |
|
97 | + public function setData($data) { |
|
98 | 98 | $this->data = $data; |
99 | 99 | return $this; |
100 | 100 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | * @param $data |
104 | 104 | * @return $this |
105 | 105 | */ |
106 | - public function getData(){ |
|
106 | + public function getData() { |
|
107 | 107 | return $this->data; |
108 | 108 | } |
109 | 109 | |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | $this->fileTitle = []; |
120 | 120 | $this->data = []; |
121 | 121 | $this->field = []; |
122 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
122 | + if ($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | * @throws TinymengException |
130 | 130 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
131 | 131 | */ |
132 | - public function setWorkSheetData($fileTitle,$data) |
|
132 | + public function setWorkSheetData($fileTitle, $data) |
|
133 | 133 | { |
134 | - if(isset($fileTitle['title_row']) || isset($fileTitle['group_left'])){ |
|
134 | + if (isset($fileTitle['title_row']) || isset($fileTitle['group_left'])) { |
|
135 | 135 | /** |
136 | 136 | * $fileTitle = [ |
137 | 137 | * 'title_row'=>1, |
@@ -141,14 +141,14 @@ discard block |
||
141 | 141 | * ], |
142 | 142 | * ]; |
143 | 143 | */ |
144 | - $this->title_row = $fileTitle['title_row']??1; |
|
145 | - $this->group_left = $fileTitle['group_left']??[]; |
|
146 | - $titleData = $fileTitle['title']??[]; |
|
144 | + $this->title_row = $fileTitle['title_row'] ?? 1; |
|
145 | + $this->group_left = $fileTitle['group_left'] ?? []; |
|
146 | + $titleData = $fileTitle['title'] ?? []; |
|
147 | 147 | // 新增:读取mergeColumns配置 |
148 | 148 | if (isset($fileTitle['mergeColumns'])) { |
149 | 149 | $this->mergeColumns = $fileTitle['mergeColumns']; |
150 | 150 | } |
151 | - }else{ |
|
151 | + } else { |
|
152 | 152 | /** |
153 | 153 | * $fileTitle = [ |
154 | 154 | * '姓名'=>'name', |
@@ -158,14 +158,14 @@ discard block |
||
158 | 158 | } |
159 | 159 | // 根据字段映射方式处理 title |
160 | 160 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
161 | - $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
|
162 | - }else{ |
|
163 | - $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
|
161 | + $this->fileTitle = array_flip($titleData); // 字段对应名称方式 - 需要将键值对调 |
|
162 | + } else { |
|
163 | + $this->fileTitle = $titleData; // 名称对应字段方式 - 保持原样 |
|
164 | 164 | } |
165 | 165 | $this->data = $data; |
166 | 166 | |
167 | 167 | /** 设置第一行格式 */ |
168 | - if(!empty($this->mainTitle)){ |
|
168 | + if (!empty($this->mainTitle)) { |
|
169 | 169 | $this->excelHeader(); |
170 | 170 | } |
171 | 171 | |
@@ -173,17 +173,17 @@ discard block |
||
173 | 173 | $this->excelTitle(); |
174 | 174 | |
175 | 175 | /** 获取列表里所有字段 **/ |
176 | - foreach ($this->fileTitle as $key => $val){ |
|
177 | - if(is_array($val)){ |
|
178 | - foreach ($val as $k => $v){ |
|
176 | + foreach ($this->fileTitle as $key => $val) { |
|
177 | + if (is_array($val)) { |
|
178 | + foreach ($val as $k => $v) { |
|
179 | 179 | $this->field[] = $v; |
180 | 180 | } |
181 | - }else{ |
|
181 | + } else { |
|
182 | 182 | $this->field[] = $val; |
183 | 183 | } |
184 | 184 | } |
185 | 185 | /** 查询结果赋值 **/ |
186 | - if(!empty($this->data)){ |
|
186 | + if (!empty($this->data)) { |
|
187 | 187 | $this->excelSetValue(); |
188 | 188 | } |
189 | 189 | // 新增:应用全表样式 |
@@ -200,38 +200,38 @@ discard block |
||
200 | 200 | * @author: Tinymeng <[email protected]> |
201 | 201 | * @time: 2022/2/22 11:43 |
202 | 202 | */ |
203 | - public function excelSetValue(){ |
|
204 | - if(empty($this->group_left)){ //判断左侧是否分组 |
|
203 | + public function excelSetValue() { |
|
204 | + if (empty($this->group_left)) { //判断左侧是否分组 |
|
205 | 205 | $rowStart = $this->_row; |
206 | - foreach ($this->data as $key => $val){ |
|
206 | + foreach ($this->data as $key => $val) { |
|
207 | 207 | $this->excelSetCellValue($val); |
208 | 208 | } |
209 | 209 | // 新增:处理mergeColumns自动合并 |
210 | 210 | if (!empty($this->mergeColumns)) { |
211 | 211 | $this->autoMergeColumns($rowStart, $this->_row - 1); |
212 | 212 | } |
213 | - }else{ //根据设置分组字段进行分组 |
|
213 | + } else { //根据设置分组字段进行分组 |
|
214 | 214 | /** 数据分组 **/ |
215 | 215 | $data = []; |
216 | 216 | $group_left_count = count($this->group_left); |
217 | - if($group_left_count == 1){ |
|
218 | - foreach ($this->data as $k => $v){ |
|
217 | + if ($group_left_count == 1) { |
|
218 | + foreach ($this->data as $k => $v) { |
|
219 | 219 | $data[$v[$this->group_left[0]]][] = $v; |
220 | 220 | } |
221 | - foreach ($data as $k =>$v){ |
|
221 | + foreach ($data as $k =>$v) { |
|
222 | 222 | $data[$k] = [ |
223 | 223 | 'data' => $v, |
224 | 224 | 'count' => count($v) |
225 | 225 | ]; |
226 | 226 | } |
227 | 227 | $this->excelGroupLeft($data, $group_left_count); |
228 | - }elseif($group_left_count == 2){ |
|
228 | + }elseif ($group_left_count == 2) { |
|
229 | 229 | foreach ($this->data as $v) { |
230 | 230 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
231 | 231 | } |
232 | 232 | $this->data = $this->arrayCount($data); |
233 | 233 | $this->excelGroupLeft($this->data, $group_left_count); |
234 | - }else{ |
|
234 | + } else { |
|
235 | 235 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
236 | 236 | '左侧分组过多,导出失败!' |
237 | 237 | ); |
@@ -243,9 +243,9 @@ discard block |
||
243 | 243 | * @return void |
244 | 244 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
245 | 245 | */ |
246 | - public function excelHeader(){ |
|
246 | + public function excelHeader() { |
|
247 | 247 | $row = 1; |
248 | - if(!empty($this->mainTitle)){ |
|
248 | + if (!empty($this->mainTitle)) { |
|
249 | 249 | $this->workSheet->setCellValue('A'.$row, $this->mainTitle); |
250 | 250 | } |
251 | 251 | |
@@ -260,33 +260,33 @@ discard block |
||
260 | 260 | } |
261 | 261 | |
262 | 262 | // 使用实际的标题列数来合并单元格 |
263 | - $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount-1).$row); |
|
263 | + $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount - 1).$row); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
267 | 267 | * @return void |
268 | 268 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
269 | 269 | */ |
270 | - private function excelTitle(){ |
|
271 | - if(!empty($this->mainTitle)){ |
|
272 | - $this->_row ++;//当前行数 |
|
270 | + private function excelTitle() { |
|
271 | + if (!empty($this->mainTitle)) { |
|
272 | + $this->_row++; //当前行数 |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | $_merge = $this->cellName($this->_col); |
276 | 276 | foreach ($this->fileTitle as $key => $val) { |
277 | - if(!empty($this->titleHeight)) { |
|
278 | - $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight);//行高度 |
|
277 | + if (!empty($this->titleHeight)) { |
|
278 | + $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight); //行高度 |
|
279 | 279 | } |
280 | 280 | $rowName = $this->cellName($this->_col); |
281 | - $this->workSheet->getStyle($rowName . $this->_row)->getAlignment()->setWrapText(true);//自动换行 |
|
281 | + $this->workSheet->getStyle($rowName.$this->_row)->getAlignment()->setWrapText(true); //自动换行 |
|
282 | 282 | if (is_array($val)) { |
283 | 283 | $num = 1; |
284 | 284 | $_cols = $this->_col; |
285 | 285 | foreach ($val as $k => $v) { |
286 | - $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
|
287 | - if(!empty($this->titleWidth)) { |
|
286 | + $this->workSheet->setCellValue($this->cellName($_cols).($this->_row + 1), $k); |
|
287 | + if (!empty($this->titleWidth)) { |
|
288 | 288 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
289 | - }else{ |
|
289 | + } else { |
|
290 | 290 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
291 | 291 | } |
292 | 292 | if ($num < count($val)) { |
@@ -295,23 +295,23 @@ discard block |
||
295 | 295 | } |
296 | 296 | $_cols++; |
297 | 297 | } |
298 | - $this->workSheet->mergeCells($_merge . $this->_row.':' . $this->cellName($this->_col) .$this->_row); |
|
299 | - $this->workSheet->setCellValue($_merge . $this->_row, $key);//设置值 |
|
298 | + $this->workSheet->mergeCells($_merge.$this->_row.':'.$this->cellName($this->_col).$this->_row); |
|
299 | + $this->workSheet->setCellValue($_merge.$this->_row, $key); //设置值 |
|
300 | 300 | } else { |
301 | 301 | if ($this->title_row != 1) { |
302 | - $this->workSheet->mergeCells($rowName . $this->_row.':' . $rowName . ($this->_row + $this->title_row - 1)); |
|
302 | + $this->workSheet->mergeCells($rowName.$this->_row.':'.$rowName.($this->_row + $this->title_row - 1)); |
|
303 | 303 | } |
304 | - $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
|
305 | - if(!empty($this->titleWidth)){ |
|
304 | + $this->workSheet->setCellValue($rowName.$this->_row, $key); //设置值 |
|
305 | + if (!empty($this->titleWidth)) { |
|
306 | 306 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
307 | - }else{ |
|
307 | + } else { |
|
308 | 308 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
309 | 309 | } |
310 | 310 | } |
311 | 311 | $this->_col++; |
312 | 312 | $_merge = $this->cellName($this->_col); |
313 | 313 | } |
314 | - $this->_row += $this->title_row;//当前行数 |
|
314 | + $this->_row += $this->title_row; //当前行数 |
|
315 | 315 | } |
316 | 316 | |
317 | 317 | /** |
@@ -322,60 +322,60 @@ discard block |
||
322 | 322 | private function excelSetCellValue($val) |
323 | 323 | { |
324 | 324 | //设置单元格行高 |
325 | - if(!empty($this->height)){ |
|
325 | + if (!empty($this->height)) { |
|
326 | 326 | $this->workSheet->getRowDimension($this->_row)->setRowHeight($this->height); |
327 | 327 | } |
328 | 328 | $_lie = 0; |
329 | - foreach ($this->field as $v){ |
|
329 | + foreach ($this->field as $v) { |
|
330 | 330 | $rowName = $this->cellName($_lie); |
331 | 331 | |
332 | - if(strpos($v,'.') !== false){ |
|
333 | - $v = explode('.',$v); |
|
332 | + if (strpos($v, '.') !== false) { |
|
333 | + $v = explode('.', $v); |
|
334 | 334 | $content = $val; |
335 | - for ($i=0;$i<count($v);$i++){ |
|
336 | - $content = $content[$v[$i]]??''; |
|
335 | + for ($i = 0; $i < count($v); $i++) { |
|
336 | + $content = $content[$v[$i]] ?? ''; |
|
337 | 337 | } |
338 | - }elseif($v == '_id'){ |
|
339 | - $content = $this->_row-$this->title_row;//自增序号列 |
|
340 | - }else{ |
|
341 | - $content = ($val[$v]??''); |
|
338 | + }elseif ($v == '_id') { |
|
339 | + $content = $this->_row - $this->title_row; //自增序号列 |
|
340 | + } else { |
|
341 | + $content = ($val[$v] ?? ''); |
|
342 | 342 | } |
343 | - if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
|
344 | - if($content['type'] == 'image'){ |
|
343 | + if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
|
344 | + if ($content['type'] == 'image') { |
|
345 | 345 | $path = $this->verifyFile($content['content']); |
346 | 346 | $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
347 | 347 | $drawing->setPath($path); |
348 | - if(!empty($content['height'])) { |
|
348 | + if (!empty($content['height'])) { |
|
349 | 349 | $drawing->setHeight($content['height']); |
350 | 350 | } |
351 | - if(!empty($content['width'])) { |
|
352 | - $drawing->setWidth($content['width']);//只设置高,宽会自适应,如果设置宽后,高则失效 |
|
351 | + if (!empty($content['width'])) { |
|
352 | + $drawing->setWidth($content['width']); //只设置高,宽会自适应,如果设置宽后,高则失效 |
|
353 | 353 | } |
354 | - if(!empty($content['offsetX'])) { |
|
355 | - $drawing->setOffsetX($content['offsetX']);//设置X方向偏移量 |
|
354 | + if (!empty($content['offsetX'])) { |
|
355 | + $drawing->setOffsetX($content['offsetX']); //设置X方向偏移量 |
|
356 | 356 | } |
357 | - if(!empty($content['offsetY'])) { |
|
358 | - $drawing->setOffsetY($content['offsetY']);//设置Y方向偏移量 |
|
357 | + if (!empty($content['offsetY'])) { |
|
358 | + $drawing->setOffsetY($content['offsetY']); //设置Y方向偏移量 |
|
359 | 359 | } |
360 | 360 | |
361 | 361 | $drawing->setCoordinates($rowName.$this->_row); |
362 | 362 | $drawing->setWorksheet($this->workSheet); |
363 | 363 | } |
364 | - }else { |
|
365 | - $content = $this->formatValue($content);//格式化数据 |
|
366 | - if (is_numeric($content)){ |
|
367 | - if($this->autoDataType && strlen($content)<11){ |
|
368 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
|
369 | - }else{ |
|
370 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
364 | + } else { |
|
365 | + $content = $this->formatValue($content); //格式化数据 |
|
366 | + if (is_numeric($content)) { |
|
367 | + if ($this->autoDataType && strlen($content) < 11) { |
|
368 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_NUMERIC); |
|
369 | + } else { |
|
370 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
371 | 371 | } |
372 | - }else{ |
|
373 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
372 | + } else { |
|
373 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
374 | 374 | } |
375 | 375 | } |
376 | - $_lie ++; |
|
376 | + $_lie++; |
|
377 | 377 | } |
378 | - $this->_row ++; |
|
378 | + $this->_row++; |
|
379 | 379 | } |
380 | 380 | |
381 | 381 | /** |
@@ -388,22 +388,22 @@ discard block |
||
388 | 388 | { |
389 | 389 | // 获取分组字段在field中的实际位置 |
390 | 390 | $group_field_positions = []; |
391 | - foreach($this->group_left as $group_field){ |
|
391 | + foreach ($this->group_left as $group_field) { |
|
392 | 392 | $position = array_search($group_field, $this->field); |
393 | - if($position !== false){ |
|
393 | + if ($position !== false) { |
|
394 | 394 | $group_field_positions[] = $position; |
395 | 395 | } |
396 | 396 | } |
397 | 397 | |
398 | - if(empty($group_field_positions)){ |
|
398 | + if (empty($group_field_positions)) { |
|
399 | 399 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, '分组字段未在标题中定义'); |
400 | 400 | } |
401 | 401 | |
402 | 402 | $group_start = $this->_row; |
403 | - foreach ($data as $key => $val){ |
|
403 | + foreach ($data as $key => $val) { |
|
404 | 404 | // 第一级分组的合并单元格 |
405 | 405 | $rowName = $this->cellName($group_field_positions[0]); // 使用第一个分组字段的实际位置 |
406 | - $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row+$val['count']-1); |
|
406 | + $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row + $val['count'] - 1); |
|
407 | 407 | $this->workSheet->mergeCells($coordinate); |
408 | 408 | $this->workSheet->setCellValue($rowName.$this->_row, $key); |
409 | 409 | |
@@ -415,27 +415,27 @@ discard block |
||
415 | 415 | $colIdx = array_search($field, $this->field); |
416 | 416 | if ($colIdx !== false) { |
417 | 417 | $colLetter = $this->cellName($colIdx); |
418 | - $this->workSheet->mergeCells($colLetter.$this->_row.':'.$colLetter.($this->_row+$val['count']-1)); |
|
418 | + $this->workSheet->mergeCells($colLetter.$this->_row.':'.$colLetter.($this->_row + $val['count'] - 1)); |
|
419 | 419 | // 取本组第一个数据的值 |
420 | 420 | $this->workSheet->setCellValue($colLetter.$this->_row, $val['data'][0][$field] ?? ''); |
421 | 421 | } |
422 | 422 | } |
423 | 423 | } |
424 | 424 | |
425 | - if($group_left_count == 1){ |
|
426 | - foreach ($val['data'] as $dataRow){ |
|
425 | + if ($group_left_count == 1) { |
|
426 | + foreach ($val['data'] as $dataRow) { |
|
427 | 427 | $this->excelSetCellValue($dataRow); |
428 | 428 | } |
429 | - }else{ |
|
429 | + } else { |
|
430 | 430 | $sub_group_start = $this->_row; |
431 | 431 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
432 | 432 | |
433 | - foreach ($val['data'] as $k => $v){ |
|
434 | - $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start+$v['count']-1); |
|
433 | + foreach ($val['data'] as $k => $v) { |
|
434 | + $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start + $v['count'] - 1); |
|
435 | 435 | $this->workSheet->mergeCells($coordinate); |
436 | 436 | $this->workSheet->setCellValue($rowName.$sub_group_start, $k); |
437 | 437 | |
438 | - foreach ($v['data'] as $data){ |
|
438 | + foreach ($v['data'] as $data) { |
|
439 | 439 | $this->excelSetCellValue($data); |
440 | 440 | } |
441 | 441 | |
@@ -454,13 +454,13 @@ discard block |
||
454 | 454 | * @param array $data 二维数组原始数据 |
455 | 455 | * @return array |
456 | 456 | */ |
457 | - private function arrayCount($data=[]) |
|
457 | + private function arrayCount($data = []) |
|
458 | 458 | { |
459 | - foreach ($data as $key => $val){ |
|
459 | + foreach ($data as $key => $val) { |
|
460 | 460 | $num = 0; |
461 | - foreach ($val as $k => $v){ |
|
461 | + foreach ($val as $k => $v) { |
|
462 | 462 | $sub_num = count($v); |
463 | - $num = $num+$sub_num; |
|
463 | + $num = $num + $sub_num; |
|
464 | 464 | $val[$k] = [ |
465 | 465 | 'count' => $sub_num, |
466 | 466 | 'data' => $v |
@@ -489,10 +489,10 @@ discard block |
||
489 | 489 | $lastValue = null; |
490 | 490 | $mergeStart = $rowStart; |
491 | 491 | for ($row = $rowStart; $row <= $rowEnd; $row++) { |
492 | - $cellValue = $this->workSheet->getCell($colLetter . $row)->getValue(); |
|
492 | + $cellValue = $this->workSheet->getCell($colLetter.$row)->getValue(); |
|
493 | 493 | if ($lastValue !== null && $cellValue !== $lastValue) { |
494 | 494 | if ($row - $mergeStart > 1) { |
495 | - $this->workSheet->mergeCells($colLetter . $mergeStart . ':' . $colLetter . ($row - 1)); |
|
495 | + $this->workSheet->mergeCells($colLetter.$mergeStart.':'.$colLetter.($row - 1)); |
|
496 | 496 | } |
497 | 497 | $mergeStart = $row; |
498 | 498 | } |
@@ -500,7 +500,7 @@ discard block |
||
500 | 500 | } |
501 | 501 | // 处理最后一组 |
502 | 502 | if ($rowEnd - $mergeStart + 1 > 1) { |
503 | - $this->workSheet->mergeCells($colLetter . $mergeStart . ':' . $colLetter . $rowEnd); |
|
503 | + $this->workSheet->mergeCells($colLetter.$mergeStart.':'.$colLetter.$rowEnd); |
|
504 | 504 | } |
505 | 505 | } |
506 | 506 | } |
@@ -516,7 +516,7 @@ discard block |
||
516 | 516 | $endCol = $this->cellName(count($this->field) - 1); |
517 | 517 | $startRow = 1; |
518 | 518 | $endRow = $this->_row - 1; |
519 | - $cellRange = $startCol . $startRow . ':' . $endCol . $endRow; |
|
519 | + $cellRange = $startCol.$startRow.':'.$endCol.$endRow; |
|
520 | 520 | $this->workSheet->getStyle($cellRange)->applyFromArray($this->sheetStyle); |
521 | 521 | } |
522 | 522 |
@@ -119,7 +119,10 @@ discard block |
||
119 | 119 | $this->fileTitle = []; |
120 | 120 | $this->data = []; |
121 | 121 | $this->field = []; |
122 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
122 | + if($this->freezePane) { |
|
123 | + $this->workSheet->freezePane($this->freezePane); |
|
124 | + } |
|
125 | + //冻结窗格 |
|
123 | 126 | } |
124 | 127 | |
125 | 128 | /** |
@@ -148,7 +151,7 @@ discard block |
||
148 | 151 | if (isset($fileTitle['mergeColumns'])) { |
149 | 152 | $this->mergeColumns = $fileTitle['mergeColumns']; |
150 | 153 | } |
151 | - }else{ |
|
154 | + } else{ |
|
152 | 155 | /** |
153 | 156 | * $fileTitle = [ |
154 | 157 | * '姓名'=>'name', |
@@ -159,7 +162,7 @@ discard block |
||
159 | 162 | // 根据字段映射方式处理 title |
160 | 163 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
161 | 164 | $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
162 | - }else{ |
|
165 | + } else{ |
|
163 | 166 | $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
164 | 167 | } |
165 | 168 | $this->data = $data; |
@@ -178,7 +181,7 @@ discard block |
||
178 | 181 | foreach ($val as $k => $v){ |
179 | 182 | $this->field[] = $v; |
180 | 183 | } |
181 | - }else{ |
|
184 | + } else{ |
|
182 | 185 | $this->field[] = $val; |
183 | 186 | } |
184 | 187 | } |
@@ -210,7 +213,7 @@ discard block |
||
210 | 213 | if (!empty($this->mergeColumns)) { |
211 | 214 | $this->autoMergeColumns($rowStart, $this->_row - 1); |
212 | 215 | } |
213 | - }else{ //根据设置分组字段进行分组 |
|
216 | + } else{ //根据设置分组字段进行分组 |
|
214 | 217 | /** 数据分组 **/ |
215 | 218 | $data = []; |
216 | 219 | $group_left_count = count($this->group_left); |
@@ -225,13 +228,13 @@ discard block |
||
225 | 228 | ]; |
226 | 229 | } |
227 | 230 | $this->excelGroupLeft($data, $group_left_count); |
228 | - }elseif($group_left_count == 2){ |
|
231 | + } elseif($group_left_count == 2){ |
|
229 | 232 | foreach ($this->data as $v) { |
230 | 233 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
231 | 234 | } |
232 | 235 | $this->data = $this->arrayCount($data); |
233 | 236 | $this->excelGroupLeft($this->data, $group_left_count); |
234 | - }else{ |
|
237 | + } else{ |
|
235 | 238 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
236 | 239 | '左侧分组过多,导出失败!' |
237 | 240 | ); |
@@ -286,7 +289,7 @@ discard block |
||
286 | 289 | $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
287 | 290 | if(!empty($this->titleWidth)) { |
288 | 291 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
289 | - }else{ |
|
292 | + } else{ |
|
290 | 293 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
291 | 294 | } |
292 | 295 | if ($num < count($val)) { |
@@ -304,7 +307,7 @@ discard block |
||
304 | 307 | $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
305 | 308 | if(!empty($this->titleWidth)){ |
306 | 309 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
307 | - }else{ |
|
310 | + } else{ |
|
308 | 311 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
309 | 312 | } |
310 | 313 | } |
@@ -335,9 +338,9 @@ discard block |
||
335 | 338 | for ($i=0;$i<count($v);$i++){ |
336 | 339 | $content = $content[$v[$i]]??''; |
337 | 340 | } |
338 | - }elseif($v == '_id'){ |
|
341 | + } elseif($v == '_id'){ |
|
339 | 342 | $content = $this->_row-$this->title_row;//自增序号列 |
340 | - }else{ |
|
343 | + } else{ |
|
341 | 344 | $content = ($val[$v]??''); |
342 | 345 | } |
343 | 346 | if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
@@ -361,15 +364,15 @@ discard block |
||
361 | 364 | $drawing->setCoordinates($rowName.$this->_row); |
362 | 365 | $drawing->setWorksheet($this->workSheet); |
363 | 366 | } |
364 | - }else { |
|
367 | + } else { |
|
365 | 368 | $content = $this->formatValue($content);//格式化数据 |
366 | 369 | if (is_numeric($content)){ |
367 | 370 | if($this->autoDataType && strlen($content)<11){ |
368 | 371 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
369 | - }else{ |
|
372 | + } else{ |
|
370 | 373 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
371 | 374 | } |
372 | - }else{ |
|
375 | + } else{ |
|
373 | 376 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
374 | 377 | } |
375 | 378 | } |
@@ -411,7 +414,9 @@ discard block |
||
411 | 414 | if (!empty($this->mergeColumns)) { |
412 | 415 | foreach ($this->mergeColumns as $field) { |
413 | 416 | // 跳过分组字段本身 |
414 | - if (in_array($field, $this->group_left)) continue; |
|
417 | + if (in_array($field, $this->group_left)) { |
|
418 | + continue; |
|
419 | + } |
|
415 | 420 | $colIdx = array_search($field, $this->field); |
416 | 421 | if ($colIdx !== false) { |
417 | 422 | $colLetter = $this->cellName($colIdx); |
@@ -426,7 +431,7 @@ discard block |
||
426 | 431 | foreach ($val['data'] as $dataRow){ |
427 | 432 | $this->excelSetCellValue($dataRow); |
428 | 433 | } |
429 | - }else{ |
|
434 | + } else{ |
|
430 | 435 | $sub_group_start = $this->_row; |
431 | 436 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
432 | 437 | |
@@ -481,10 +486,14 @@ discard block |
||
481 | 486 | */ |
482 | 487 | private function autoMergeColumns($rowStart, $rowEnd) |
483 | 488 | { |
484 | - if ($rowEnd <= $rowStart) return; |
|
489 | + if ($rowEnd <= $rowStart) { |
|
490 | + return; |
|
491 | + } |
|
485 | 492 | foreach ($this->mergeColumns as $fieldName) { |
486 | 493 | $colIdx = array_search($fieldName, $this->field); |
487 | - if ($colIdx === false) continue; |
|
494 | + if ($colIdx === false) { |
|
495 | + continue; |
|
496 | + } |
|
488 | 497 | $colLetter = $this->cellName($colIdx); |
489 | 498 | $lastValue = null; |
490 | 499 | $mergeStart = $rowStart; |
@@ -510,7 +519,9 @@ discard block |
||
510 | 519 | */ |
511 | 520 | private function applySheetStyle() |
512 | 521 | { |
513 | - if (empty($this->sheetStyle)) return; |
|
522 | + if (empty($this->sheetStyle)) { |
|
523 | + return; |
|
524 | + } |
|
514 | 525 | // 计算数据区范围 |
515 | 526 | $startCol = 'A'; |
516 | 527 | $endCol = $this->cellName(count($this->field) - 1); |