@@ -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 | /** |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * @param $data |
| 99 | 99 | * @return $this |
| 100 | 100 | */ |
| 101 | - public function setData($data){ |
|
| 101 | + public function setData($data) { |
|
| 102 | 102 | $this->data = $data; |
| 103 | 103 | return $this; |
| 104 | 104 | } |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * @param $data |
| 108 | 108 | * @return $this |
| 109 | 109 | */ |
| 110 | - public function getData(){ |
|
| 110 | + public function getData() { |
|
| 111 | 111 | return $this->data; |
| 112 | 112 | } |
| 113 | 113 | |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | $this->fileTitle = []; |
| 124 | 124 | $this->data = []; |
| 125 | 125 | $this->field = []; |
| 126 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 126 | + if ($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | * @throws TinymengException |
| 134 | 134 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 135 | 135 | */ |
| 136 | - public function setWorkSheetData($titleConfig,$data) |
|
| 136 | + public function setWorkSheetData($titleConfig, $data) |
|
| 137 | 137 | { |
| 138 | 138 | $this->titleConfig = $titleConfig; |
| 139 | - if(isset($titleConfig['title_row']) || isset($titleConfig['group_left'])){ |
|
| 139 | + if (isset($titleConfig['title_row']) || isset($titleConfig['group_left'])) { |
|
| 140 | 140 | /** |
| 141 | 141 | * $titleConfig = [ |
| 142 | 142 | * 'title_row'=>1, |
@@ -146,14 +146,14 @@ discard block |
||
| 146 | 146 | * ], |
| 147 | 147 | * ]; |
| 148 | 148 | */ |
| 149 | - $this->title_row = $titleConfig['title_row']??1; |
|
| 150 | - $this->group_left = $titleConfig['group_left']??[]; |
|
| 151 | - $titleData = $titleConfig['title']??[]; |
|
| 149 | + $this->title_row = $titleConfig['title_row'] ?? 1; |
|
| 150 | + $this->group_left = $titleConfig['group_left'] ?? []; |
|
| 151 | + $titleData = $titleConfig['title'] ?? []; |
|
| 152 | 152 | // 新增:读取mergeColumns配置 |
| 153 | 153 | if (isset($titleConfig['mergeColumns'])) { |
| 154 | 154 | $this->mergeColumns = $titleConfig['mergeColumns']; |
| 155 | 155 | } |
| 156 | - }else{ |
|
| 156 | + } else { |
|
| 157 | 157 | /** |
| 158 | 158 | * $titleConfig = [ |
| 159 | 159 | * '姓名'=>'name', |
@@ -163,33 +163,33 @@ discard block |
||
| 163 | 163 | } |
| 164 | 164 | // 根据字段映射方式处理 title |
| 165 | 165 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
| 166 | - $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
|
| 167 | - }else{ |
|
| 168 | - $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
|
| 166 | + $this->fileTitle = array_flip($titleData); // 字段对应名称方式 - 需要将键值对调 |
|
| 167 | + } else { |
|
| 168 | + $this->fileTitle = $titleData; // 名称对应字段方式 - 保持原样 |
|
| 169 | 169 | } |
| 170 | 170 | $this->data = $data; |
| 171 | 171 | |
| 172 | 172 | /** 设置第一行格式 */ |
| 173 | - if(!empty($this->mainTitle)){ |
|
| 173 | + if (!empty($this->mainTitle)) { |
|
| 174 | 174 | $this->excelHeader(); |
| 175 | - $this->_row ++;//当前行数 |
|
| 175 | + $this->_row++; //当前行数 |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** 设置表头 **/ |
| 179 | 179 | $this->excelTitle(); |
| 180 | 180 | |
| 181 | 181 | /** 获取列表里所有字段 **/ |
| 182 | - foreach ($this->fileTitle as $key => $val){ |
|
| 183 | - if(is_array($val)){ |
|
| 184 | - foreach ($val as $k => $v){ |
|
| 182 | + foreach ($this->fileTitle as $key => $val) { |
|
| 183 | + if (is_array($val)) { |
|
| 184 | + foreach ($val as $k => $v) { |
|
| 185 | 185 | $this->field[] = $v; |
| 186 | 186 | } |
| 187 | - }else{ |
|
| 187 | + } else { |
|
| 188 | 188 | $this->field[] = $val; |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | /** 查询结果赋值 **/ |
| 192 | - if(!empty($this->data)){ |
|
| 192 | + if (!empty($this->data)) { |
|
| 193 | 193 | $this->excelSetValue(); |
| 194 | 194 | } |
| 195 | 195 | // 新增:应用全表样式 |
@@ -206,46 +206,46 @@ discard block |
||
| 206 | 206 | * @author: Tinymeng <[email protected]> |
| 207 | 207 | * @time: 2022/2/22 11:43 |
| 208 | 208 | */ |
| 209 | - public function excelSetValue(){ |
|
| 210 | - if(!empty($this->titleConfig['data_start_row'])){ |
|
| 209 | + public function excelSetValue() { |
|
| 210 | + if (!empty($this->titleConfig['data_start_row'])) { |
|
| 211 | 211 | $this->_row = $this->titleConfig['data_start_row']; |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - if(empty($this->group_left)){ //判断左侧是否分组 |
|
| 214 | + if (empty($this->group_left)) { //判断左侧是否分组 |
|
| 215 | 215 | $rowStart = $this->_row; |
| 216 | - foreach ($this->data as $key => $val){ |
|
| 216 | + foreach ($this->data as $key => $val) { |
|
| 217 | 217 | $this->excelSetCellValue($val); |
| 218 | 218 | } |
| 219 | 219 | // 新增:处理mergeColumns自动合并 |
| 220 | 220 | if (!empty($this->mergeColumns)) { |
| 221 | 221 | $this->autoMergeColumns($rowStart, $this->_row - 1); |
| 222 | 222 | } |
| 223 | - }else{ //根据设置分组字段进行分组 |
|
| 223 | + } else { //根据设置分组字段进行分组 |
|
| 224 | 224 | /** 数据分组 **/ |
| 225 | 225 | $data = []; |
| 226 | 226 | $group_left_count = count($this->group_left); |
| 227 | - if($group_left_count == 1){ |
|
| 228 | - foreach ($this->data as $k => $v){ |
|
| 229 | - if(isset($v[$this->group_left[0]])){ |
|
| 227 | + if ($group_left_count == 1) { |
|
| 228 | + foreach ($this->data as $k => $v) { |
|
| 229 | + if (isset($v[$this->group_left[0]])) { |
|
| 230 | 230 | $data[$v[$this->group_left[0]]][] = $v; |
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | - foreach ($data as $k =>$v){ |
|
| 233 | + foreach ($data as $k =>$v) { |
|
| 234 | 234 | $data[$k] = [ |
| 235 | 235 | 'data' => $v, |
| 236 | 236 | 'count' => count($v) |
| 237 | 237 | ]; |
| 238 | 238 | } |
| 239 | 239 | $this->excelGroupLeft($data, $group_left_count); |
| 240 | - }elseif($group_left_count == 2){ |
|
| 240 | + }elseif ($group_left_count == 2) { |
|
| 241 | 241 | foreach ($this->data as $v) { |
| 242 | - if(isset($v[$this->group_left[0]]) && isset($v[$this->group_left[1]])){ |
|
| 242 | + if (isset($v[$this->group_left[0]]) && isset($v[$this->group_left[1]])) { |
|
| 243 | 243 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
| 244 | 244 | } |
| 245 | 245 | } |
| 246 | 246 | $this->data = $this->arrayCount($data); |
| 247 | 247 | $this->excelGroupLeft($this->data, $group_left_count); |
| 248 | - }else{ |
|
| 248 | + } else { |
|
| 249 | 249 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
| 250 | 250 | '左侧分组过多,导出失败!' |
| 251 | 251 | ); |
@@ -257,9 +257,9 @@ discard block |
||
| 257 | 257 | * @return void |
| 258 | 258 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 259 | 259 | */ |
| 260 | - public function excelHeader(){ |
|
| 260 | + public function excelHeader() { |
|
| 261 | 261 | $row = 1; |
| 262 | - if(!empty($this->mainTitle)){ |
|
| 262 | + if (!empty($this->mainTitle)) { |
|
| 263 | 263 | $this->workSheet->setCellValue('A'.$row, $this->mainTitle); |
| 264 | 264 | } |
| 265 | 265 | |
@@ -274,35 +274,35 @@ discard block |
||
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | // 使用实际的标题列数来合并单元格 |
| 277 | - $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount-1).$row); |
|
| 277 | + $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount - 1).$row); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
| 281 | 281 | * @return void |
| 282 | 282 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 283 | 283 | */ |
| 284 | - private function excelTitle(){ |
|
| 285 | - if(!empty($this->titleConfig['title_start_row'])){ |
|
| 284 | + private function excelTitle() { |
|
| 285 | + if (!empty($this->titleConfig['title_start_row'])) { |
|
| 286 | 286 | $this->_row = $this->titleConfig['title_start_row']; |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | $_merge = $this->cellName($this->_col); |
| 290 | 290 | foreach ($this->fileTitle as $key => $val) { |
| 291 | - if(!empty($this->titleHeight)) { |
|
| 292 | - $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight);//行高度 |
|
| 291 | + if (!empty($this->titleHeight)) { |
|
| 292 | + $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight); //行高度 |
|
| 293 | 293 | } |
| 294 | 294 | $rowName = $this->cellName($this->_col); |
| 295 | - $this->workSheet->getStyle($rowName . $this->_row)->getAlignment()->setWrapText(true);//自动换行 |
|
| 295 | + $this->workSheet->getStyle($rowName.$this->_row)->getAlignment()->setWrapText(true); //自动换行 |
|
| 296 | 296 | if (is_array($val)) { |
| 297 | 297 | $num = 1; |
| 298 | 298 | $_cols = $this->_col; |
| 299 | 299 | foreach ($val as $k => $v) { |
| 300 | - if(!isset($this->titleConfig['title_show']) || $this->titleConfig['title_show']!==false) { |
|
| 301 | - $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
|
| 300 | + if (!isset($this->titleConfig['title_show']) || $this->titleConfig['title_show'] !== false) { |
|
| 301 | + $this->workSheet->setCellValue($this->cellName($_cols).($this->_row + 1), $k); |
|
| 302 | 302 | } |
| 303 | - if(!empty($this->titleWidth)) { |
|
| 303 | + if (!empty($this->titleWidth)) { |
|
| 304 | 304 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
| 305 | - }else{ |
|
| 305 | + } else { |
|
| 306 | 306 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
| 307 | 307 | } |
| 308 | 308 | if ($num < count($val)) { |
@@ -311,27 +311,27 @@ discard block |
||
| 311 | 311 | } |
| 312 | 312 | $_cols++; |
| 313 | 313 | } |
| 314 | - $this->workSheet->mergeCells($_merge . $this->_row.':' . $this->cellName($this->_col) .$this->_row); |
|
| 315 | - if(!isset($this->titleConfig['title_show']) || $this->titleConfig['title_show']!==false) { |
|
| 316 | - $this->workSheet->setCellValue($_merge . $this->_row, $key);//设置值 |
|
| 314 | + $this->workSheet->mergeCells($_merge.$this->_row.':'.$this->cellName($this->_col).$this->_row); |
|
| 315 | + if (!isset($this->titleConfig['title_show']) || $this->titleConfig['title_show'] !== false) { |
|
| 316 | + $this->workSheet->setCellValue($_merge.$this->_row, $key); //设置值 |
|
| 317 | 317 | } |
| 318 | 318 | } else { |
| 319 | 319 | if ($this->title_row != 1) { |
| 320 | - $this->workSheet->mergeCells($rowName . $this->_row.':' . $rowName . ($this->_row + $this->title_row - 1)); |
|
| 320 | + $this->workSheet->mergeCells($rowName.$this->_row.':'.$rowName.($this->_row + $this->title_row - 1)); |
|
| 321 | 321 | } |
| 322 | - if(!isset($this->titleConfig['title_show']) || $this->titleConfig['title_show']!==false) { |
|
| 323 | - $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
|
| 322 | + if (!isset($this->titleConfig['title_show']) || $this->titleConfig['title_show'] !== false) { |
|
| 323 | + $this->workSheet->setCellValue($rowName.$this->_row, $key); //设置值 |
|
| 324 | 324 | } |
| 325 | - if(!empty($this->titleWidth)){ |
|
| 325 | + if (!empty($this->titleWidth)) { |
|
| 326 | 326 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
| 327 | - }else{ |
|
| 327 | + } else { |
|
| 328 | 328 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
| 329 | 329 | } |
| 330 | 330 | } |
| 331 | 331 | $this->_col++; |
| 332 | 332 | $_merge = $this->cellName($this->_col); |
| 333 | 333 | } |
| 334 | - $this->_row += $this->title_row;//当前行数 |
|
| 334 | + $this->_row += $this->title_row; //当前行数 |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | /** |
@@ -342,63 +342,63 @@ discard block |
||
| 342 | 342 | private function excelSetCellValue($val) |
| 343 | 343 | { |
| 344 | 344 | //设置单元格行高 |
| 345 | - if(!empty($this->height)){ |
|
| 345 | + if (!empty($this->height)) { |
|
| 346 | 346 | $this->workSheet->getRowDimension($this->_row)->setRowHeight($this->height); |
| 347 | 347 | } |
| 348 | 348 | $_lie = 0; |
| 349 | - foreach ($this->field as $v){ |
|
| 349 | + foreach ($this->field as $v) { |
|
| 350 | 350 | $rowName = $this->cellName($_lie); |
| 351 | 351 | |
| 352 | - if(strpos($v,'.') !== false){ |
|
| 353 | - $v = explode('.',$v); |
|
| 352 | + if (strpos($v, '.') !== false) { |
|
| 353 | + $v = explode('.', $v); |
|
| 354 | 354 | $content = $val; |
| 355 | - for ($i=0;$i<count($v);$i++){ |
|
| 356 | - $content = $content[$v[$i]]??''; |
|
| 355 | + for ($i = 0; $i < count($v); $i++) { |
|
| 356 | + $content = $content[$v[$i]] ?? ''; |
|
| 357 | 357 | } |
| 358 | - }elseif($v == '_id'){ |
|
| 359 | - $content = $this->_row-$this->title_row;//自增序号列 |
|
| 360 | - }else{ |
|
| 361 | - $content = ($val[$v]??''); |
|
| 358 | + }elseif ($v == '_id') { |
|
| 359 | + $content = $this->_row - $this->title_row; //自增序号列 |
|
| 360 | + } else { |
|
| 361 | + $content = ($val[$v] ?? ''); |
|
| 362 | 362 | } |
| 363 | - if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
|
| 364 | - if($content['type'] == 'image'){ |
|
| 363 | + if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
|
| 364 | + if ($content['type'] == 'image') { |
|
| 365 | 365 | $path = $this->verifyFile($content['content']); |
| 366 | 366 | $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
| 367 | 367 | $drawing->setPath($path); |
| 368 | - if(!empty($content['height'])) { |
|
| 368 | + if (!empty($content['height'])) { |
|
| 369 | 369 | $drawing->setHeight($content['height']); |
| 370 | 370 | } |
| 371 | - if(!empty($content['width'])) { |
|
| 372 | - $drawing->setWidth($content['width']);//只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 371 | + if (!empty($content['width'])) { |
|
| 372 | + $drawing->setWidth($content['width']); //只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 373 | 373 | } |
| 374 | - if(!empty($content['offsetX'])) { |
|
| 375 | - $drawing->setOffsetX($content['offsetX']);//设置X方向偏移量 |
|
| 374 | + if (!empty($content['offsetX'])) { |
|
| 375 | + $drawing->setOffsetX($content['offsetX']); //设置X方向偏移量 |
|
| 376 | 376 | } |
| 377 | - if(!empty($content['offsetY'])) { |
|
| 378 | - $drawing->setOffsetY($content['offsetY']);//设置Y方向偏移量 |
|
| 377 | + if (!empty($content['offsetY'])) { |
|
| 378 | + $drawing->setOffsetY($content['offsetY']); //设置Y方向偏移量 |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | $drawing->setCoordinates($rowName.$this->_row); |
| 382 | 382 | $drawing->setWorksheet($this->workSheet); |
| 383 | 383 | } |
| 384 | - }elseif(is_array($content) && isset($content['formula'])){ |
|
| 384 | + }elseif (is_array($content) && isset($content['formula'])) { |
|
| 385 | 385 | // 新增:支持 ['formula' => '公式'] 写法 |
| 386 | 386 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content['formula'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA); |
| 387 | - }else { |
|
| 388 | - $content = $this->formatValue($content);//格式化数据 |
|
| 389 | - if (is_numeric($content)){ |
|
| 390 | - if($this->autoDataType && strlen($content)<11){ |
|
| 391 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
|
| 392 | - }else{ |
|
| 393 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 387 | + } else { |
|
| 388 | + $content = $this->formatValue($content); //格式化数据 |
|
| 389 | + if (is_numeric($content)) { |
|
| 390 | + if ($this->autoDataType && strlen($content) < 11) { |
|
| 391 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_NUMERIC); |
|
| 392 | + } else { |
|
| 393 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 394 | 394 | } |
| 395 | - }else{ |
|
| 396 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 395 | + } else { |
|
| 396 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 397 | 397 | } |
| 398 | 398 | } |
| 399 | - $_lie ++; |
|
| 399 | + $_lie++; |
|
| 400 | 400 | } |
| 401 | - $this->_row ++; |
|
| 401 | + $this->_row++; |
|
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /** |
@@ -411,22 +411,22 @@ discard block |
||
| 411 | 411 | { |
| 412 | 412 | // 获取分组字段在field中的实际位置 |
| 413 | 413 | $group_field_positions = []; |
| 414 | - foreach($this->group_left as $group_field){ |
|
| 414 | + foreach ($this->group_left as $group_field) { |
|
| 415 | 415 | $position = array_search($group_field, $this->field); |
| 416 | - if($position !== false){ |
|
| 416 | + if ($position !== false) { |
|
| 417 | 417 | $group_field_positions[] = $position; |
| 418 | 418 | } |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | - if(empty($group_field_positions)){ |
|
| 421 | + if (empty($group_field_positions)) { |
|
| 422 | 422 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, '分组字段未在标题中定义'); |
| 423 | 423 | } |
| 424 | 424 | |
| 425 | 425 | $group_start = $this->_row; |
| 426 | - foreach ($data as $key => $val){ |
|
| 426 | + foreach ($data as $key => $val) { |
|
| 427 | 427 | // 第一级分组的合并单元格 |
| 428 | 428 | $rowName = $this->cellName($group_field_positions[0]); // 使用第一个分组字段的实际位置 |
| 429 | - $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row+$val['count']-1); |
|
| 429 | + $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row + $val['count'] - 1); |
|
| 430 | 430 | $this->workSheet->mergeCells($coordinate); |
| 431 | 431 | $this->workSheet->setCellValue($rowName.$this->_row, $key); |
| 432 | 432 | |
@@ -438,27 +438,27 @@ discard block |
||
| 438 | 438 | $colIdx = array_search($field, $this->field); |
| 439 | 439 | if ($colIdx !== false) { |
| 440 | 440 | $colLetter = $this->cellName($colIdx); |
| 441 | - $this->workSheet->mergeCells($colLetter.$this->_row.':'.$colLetter.($this->_row+$val['count']-1)); |
|
| 441 | + $this->workSheet->mergeCells($colLetter.$this->_row.':'.$colLetter.($this->_row + $val['count'] - 1)); |
|
| 442 | 442 | // 取本组第一个数据的值 |
| 443 | 443 | $this->workSheet->setCellValue($colLetter.$this->_row, $val['data'][0][$field] ?? ''); |
| 444 | 444 | } |
| 445 | 445 | } |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | - if($group_left_count == 1){ |
|
| 449 | - foreach ($val['data'] as $dataRow){ |
|
| 448 | + if ($group_left_count == 1) { |
|
| 449 | + foreach ($val['data'] as $dataRow) { |
|
| 450 | 450 | $this->excelSetCellValue($dataRow); |
| 451 | 451 | } |
| 452 | - }else{ |
|
| 452 | + } else { |
|
| 453 | 453 | $sub_group_start = $this->_row; |
| 454 | 454 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
| 455 | 455 | |
| 456 | - foreach ($val['data'] as $k => $v){ |
|
| 457 | - $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start+$v['count']-1); |
|
| 456 | + foreach ($val['data'] as $k => $v) { |
|
| 457 | + $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start + $v['count'] - 1); |
|
| 458 | 458 | $this->workSheet->mergeCells($coordinate); |
| 459 | 459 | $this->workSheet->setCellValue($rowName.$sub_group_start, $k); |
| 460 | 460 | |
| 461 | - foreach ($v['data'] as $data){ |
|
| 461 | + foreach ($v['data'] as $data) { |
|
| 462 | 462 | $this->excelSetCellValue($data); |
| 463 | 463 | } |
| 464 | 464 | |
@@ -477,13 +477,13 @@ discard block |
||
| 477 | 477 | * @param array $data 二维数组原始数据 |
| 478 | 478 | * @return array |
| 479 | 479 | */ |
| 480 | - private function arrayCount($data=[]) |
|
| 480 | + private function arrayCount($data = []) |
|
| 481 | 481 | { |
| 482 | - foreach ($data as $key => $val){ |
|
| 482 | + foreach ($data as $key => $val) { |
|
| 483 | 483 | $num = 0; |
| 484 | - foreach ($val as $k => $v){ |
|
| 484 | + foreach ($val as $k => $v) { |
|
| 485 | 485 | $sub_num = count($v); |
| 486 | - $num = $num+$sub_num; |
|
| 486 | + $num = $num + $sub_num; |
|
| 487 | 487 | $val[$k] = [ |
| 488 | 488 | 'count' => $sub_num, |
| 489 | 489 | 'data' => $v |
@@ -512,10 +512,10 @@ discard block |
||
| 512 | 512 | $lastValue = null; |
| 513 | 513 | $mergeStart = $rowStart; |
| 514 | 514 | for ($row = $rowStart; $row <= $rowEnd; $row++) { |
| 515 | - $cellValue = $this->workSheet->getCell($colLetter . $row)->getValue(); |
|
| 515 | + $cellValue = $this->workSheet->getCell($colLetter.$row)->getValue(); |
|
| 516 | 516 | if ($lastValue !== null && $cellValue !== $lastValue) { |
| 517 | 517 | if ($row - $mergeStart > 1) { |
| 518 | - $this->workSheet->mergeCells($colLetter . $mergeStart . ':' . $colLetter . ($row - 1)); |
|
| 518 | + $this->workSheet->mergeCells($colLetter.$mergeStart.':'.$colLetter.($row - 1)); |
|
| 519 | 519 | } |
| 520 | 520 | $mergeStart = $row; |
| 521 | 521 | } |
@@ -523,7 +523,7 @@ discard block |
||
| 523 | 523 | } |
| 524 | 524 | // 处理最后一组 |
| 525 | 525 | if ($rowEnd - $mergeStart + 1 > 1) { |
| 526 | - $this->workSheet->mergeCells($colLetter . $mergeStart . ':' . $colLetter . $rowEnd); |
|
| 526 | + $this->workSheet->mergeCells($colLetter.$mergeStart.':'.$colLetter.$rowEnd); |
|
| 527 | 527 | } |
| 528 | 528 | } |
| 529 | 529 | } |
@@ -539,7 +539,7 @@ discard block |
||
| 539 | 539 | $endCol = $this->cellName(count($this->field) - 1); |
| 540 | 540 | $startRow = 1; |
| 541 | 541 | $endRow = $this->_row - 1; |
| 542 | - $cellRange = $startCol . $startRow . ':' . $endCol . $endRow; |
|
| 542 | + $cellRange = $startCol.$startRow.':'.$endCol.$endRow; |
|
| 543 | 543 | $this->workSheet->getStyle($cellRange)->applyFromArray($this->sheetStyle); |
| 544 | 544 | } |
| 545 | 545 | |
@@ -123,7 +123,10 @@ discard block |
||
| 123 | 123 | $this->fileTitle = []; |
| 124 | 124 | $this->data = []; |
| 125 | 125 | $this->field = []; |
| 126 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 126 | + if($this->freezePane) { |
|
| 127 | + $this->workSheet->freezePane($this->freezePane); |
|
| 128 | + } |
|
| 129 | + //冻结窗格 |
|
| 127 | 130 | } |
| 128 | 131 | |
| 129 | 132 | /** |
@@ -153,7 +156,7 @@ discard block |
||
| 153 | 156 | if (isset($titleConfig['mergeColumns'])) { |
| 154 | 157 | $this->mergeColumns = $titleConfig['mergeColumns']; |
| 155 | 158 | } |
| 156 | - }else{ |
|
| 159 | + } else{ |
|
| 157 | 160 | /** |
| 158 | 161 | * $titleConfig = [ |
| 159 | 162 | * '姓名'=>'name', |
@@ -164,7 +167,7 @@ discard block |
||
| 164 | 167 | // 根据字段映射方式处理 title |
| 165 | 168 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
| 166 | 169 | $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
| 167 | - }else{ |
|
| 170 | + } else{ |
|
| 168 | 171 | $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
| 169 | 172 | } |
| 170 | 173 | $this->data = $data; |
@@ -184,7 +187,7 @@ discard block |
||
| 184 | 187 | foreach ($val as $k => $v){ |
| 185 | 188 | $this->field[] = $v; |
| 186 | 189 | } |
| 187 | - }else{ |
|
| 190 | + } else{ |
|
| 188 | 191 | $this->field[] = $val; |
| 189 | 192 | } |
| 190 | 193 | } |
@@ -220,7 +223,7 @@ discard block |
||
| 220 | 223 | if (!empty($this->mergeColumns)) { |
| 221 | 224 | $this->autoMergeColumns($rowStart, $this->_row - 1); |
| 222 | 225 | } |
| 223 | - }else{ //根据设置分组字段进行分组 |
|
| 226 | + } else{ //根据设置分组字段进行分组 |
|
| 224 | 227 | /** 数据分组 **/ |
| 225 | 228 | $data = []; |
| 226 | 229 | $group_left_count = count($this->group_left); |
@@ -237,7 +240,7 @@ discard block |
||
| 237 | 240 | ]; |
| 238 | 241 | } |
| 239 | 242 | $this->excelGroupLeft($data, $group_left_count); |
| 240 | - }elseif($group_left_count == 2){ |
|
| 243 | + } elseif($group_left_count == 2){ |
|
| 241 | 244 | foreach ($this->data as $v) { |
| 242 | 245 | if(isset($v[$this->group_left[0]]) && isset($v[$this->group_left[1]])){ |
| 243 | 246 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
@@ -245,7 +248,7 @@ discard block |
||
| 245 | 248 | } |
| 246 | 249 | $this->data = $this->arrayCount($data); |
| 247 | 250 | $this->excelGroupLeft($this->data, $group_left_count); |
| 248 | - }else{ |
|
| 251 | + } else{ |
|
| 249 | 252 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
| 250 | 253 | '左侧分组过多,导出失败!' |
| 251 | 254 | ); |
@@ -302,7 +305,7 @@ discard block |
||
| 302 | 305 | } |
| 303 | 306 | if(!empty($this->titleWidth)) { |
| 304 | 307 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
| 305 | - }else{ |
|
| 308 | + } else{ |
|
| 306 | 309 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
| 307 | 310 | } |
| 308 | 311 | if ($num < count($val)) { |
@@ -324,7 +327,7 @@ discard block |
||
| 324 | 327 | } |
| 325 | 328 | if(!empty($this->titleWidth)){ |
| 326 | 329 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
| 327 | - }else{ |
|
| 330 | + } else{ |
|
| 328 | 331 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
| 329 | 332 | } |
| 330 | 333 | } |
@@ -355,9 +358,9 @@ discard block |
||
| 355 | 358 | for ($i=0;$i<count($v);$i++){ |
| 356 | 359 | $content = $content[$v[$i]]??''; |
| 357 | 360 | } |
| 358 | - }elseif($v == '_id'){ |
|
| 361 | + } elseif($v == '_id'){ |
|
| 359 | 362 | $content = $this->_row-$this->title_row;//自增序号列 |
| 360 | - }else{ |
|
| 363 | + } else{ |
|
| 361 | 364 | $content = ($val[$v]??''); |
| 362 | 365 | } |
| 363 | 366 | if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
@@ -381,18 +384,18 @@ discard block |
||
| 381 | 384 | $drawing->setCoordinates($rowName.$this->_row); |
| 382 | 385 | $drawing->setWorksheet($this->workSheet); |
| 383 | 386 | } |
| 384 | - }elseif(is_array($content) && isset($content['formula'])){ |
|
| 387 | + } elseif(is_array($content) && isset($content['formula'])){ |
|
| 385 | 388 | // 新增:支持 ['formula' => '公式'] 写法 |
| 386 | 389 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content['formula'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA); |
| 387 | - }else { |
|
| 390 | + } else { |
|
| 388 | 391 | $content = $this->formatValue($content);//格式化数据 |
| 389 | 392 | if (is_numeric($content)){ |
| 390 | 393 | if($this->autoDataType && strlen($content)<11){ |
| 391 | 394 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
| 392 | - }else{ |
|
| 395 | + } else{ |
|
| 393 | 396 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
| 394 | 397 | } |
| 395 | - }else{ |
|
| 398 | + } else{ |
|
| 396 | 399 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
| 397 | 400 | } |
| 398 | 401 | } |
@@ -434,7 +437,9 @@ discard block |
||
| 434 | 437 | if (!empty($this->mergeColumns)) { |
| 435 | 438 | foreach ($this->mergeColumns as $field) { |
| 436 | 439 | // 跳过分组字段本身 |
| 437 | - if (in_array($field, $this->group_left)) continue; |
|
| 440 | + if (in_array($field, $this->group_left)) { |
|
| 441 | + continue; |
|
| 442 | + } |
|
| 438 | 443 | $colIdx = array_search($field, $this->field); |
| 439 | 444 | if ($colIdx !== false) { |
| 440 | 445 | $colLetter = $this->cellName($colIdx); |
@@ -449,7 +454,7 @@ discard block |
||
| 449 | 454 | foreach ($val['data'] as $dataRow){ |
| 450 | 455 | $this->excelSetCellValue($dataRow); |
| 451 | 456 | } |
| 452 | - }else{ |
|
| 457 | + } else{ |
|
| 453 | 458 | $sub_group_start = $this->_row; |
| 454 | 459 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
| 455 | 460 | |
@@ -504,10 +509,14 @@ discard block |
||
| 504 | 509 | */ |
| 505 | 510 | private function autoMergeColumns($rowStart, $rowEnd) |
| 506 | 511 | { |
| 507 | - if ($rowEnd <= $rowStart) return; |
|
| 512 | + if ($rowEnd <= $rowStart) { |
|
| 513 | + return; |
|
| 514 | + } |
|
| 508 | 515 | foreach ($this->mergeColumns as $fieldName) { |
| 509 | 516 | $colIdx = array_search($fieldName, $this->field); |
| 510 | - if ($colIdx === false) continue; |
|
| 517 | + if ($colIdx === false) { |
|
| 518 | + continue; |
|
| 519 | + } |
|
| 511 | 520 | $colLetter = $this->cellName($colIdx); |
| 512 | 521 | $lastValue = null; |
| 513 | 522 | $mergeStart = $rowStart; |
@@ -533,7 +542,9 @@ discard block |
||
| 533 | 542 | */ |
| 534 | 543 | private function applySheetStyle() |
| 535 | 544 | { |
| 536 | - if (empty($this->sheetStyle)) return; |
|
| 545 | + if (empty($this->sheetStyle)) { |
|
| 546 | + return; |
|
| 547 | + } |
|
| 537 | 548 | // 计算数据区范围 |
| 538 | 549 | $startCol = 'A'; |
| 539 | 550 | $endCol = $this->cellName(count($this->field) - 1); |