@@ -30,9 +30,9 @@ |
||
| 30 | 30 | * @return $this |
| 31 | 31 | * @throws Exception |
| 32 | 32 | */ |
| 33 | - public function initWorkSheet($filename='') |
|
| 33 | + public function initWorkSheet($filename = '') |
|
| 34 | 34 | { |
| 35 | - if(!empty($filename)){ |
|
| 35 | + if (!empty($filename)) { |
|
| 36 | 36 | $this->setFileName($filename); |
| 37 | 37 | } |
| 38 | 38 | $this->loadFile($this->fileName); |
@@ -44,8 +44,8 @@ discard block |
||
| 44 | 44 | * @author: Tinymeng <[email protected]> |
| 45 | 45 | * @time: 2022/4/24 17:35 |
| 46 | 46 | */ |
| 47 | - protected function cellName($columnIndex){ |
|
| 48 | - $columnIndex =(int)$columnIndex+1; |
|
| 47 | + protected function cellName($columnIndex) { |
|
| 48 | + $columnIndex = (int) $columnIndex + 1; |
|
| 49 | 49 | static $indexCache = []; |
| 50 | 50 | |
| 51 | 51 | if (!isset($indexCache[$columnIndex])) { |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | do { |
| 55 | 55 | $characterValue = ($indexValue % 26) ?: 26; |
| 56 | 56 | $indexValue = ($indexValue - $characterValue) / 26; |
| 57 | - $base26 = chr($characterValue + 64) . ($base26 ?: ''); |
|
| 57 | + $base26 = chr($characterValue + 64).($base26 ?: ''); |
|
| 58 | 58 | } while ($indexValue > 0); |
| 59 | 59 | $indexCache[$columnIndex] = $base26; |
| 60 | 60 | } |
@@ -67,10 +67,10 @@ discard block |
||
| 67 | 67 | * @param string |
| 68 | 68 | * @return mixed |
| 69 | 69 | */ |
| 70 | - protected function formatValue($v){ |
|
| 71 | - if($this->format === false) return $v; |
|
| 72 | - if(is_numeric($v) && strlen($v)===10){ |
|
| 73 | - $v = date($this->format_date,$v);//时间戳转时间格式 |
|
| 70 | + protected function formatValue($v) { |
|
| 71 | + if ($this->format === false) return $v; |
|
| 72 | + if (is_numeric($v) && strlen($v) === 10) { |
|
| 73 | + $v = date($this->format_date, $v); //时间戳转时间格式 |
|
| 74 | 74 | } |
| 75 | 75 | return $v; |
| 76 | 76 | } |
@@ -80,9 +80,9 @@ discard block |
||
| 80 | 80 | * @param $lastCell |
| 81 | 81 | * @return array |
| 82 | 82 | */ |
| 83 | - protected function getCellName($lastCell){ |
|
| 83 | + protected function getCellName($lastCell) { |
|
| 84 | 84 | $cellName = array(); |
| 85 | - for($i='A'; $i!=$lastCell; $i++) { |
|
| 85 | + for ($i = 'A'; $i != $lastCell; $i++) { |
|
| 86 | 86 | $cellName[] = $i; |
| 87 | 87 | } |
| 88 | 88 | $cellName[] = $i++; |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * @return array|string|string[] |
| 97 | 97 | * @throws PhpSpreadsheetException |
| 98 | 98 | */ |
| 99 | - protected function verifyFile($path, $verifyFile = true, $zip = null){ |
|
| 99 | + protected function verifyFile($path, $verifyFile = true, $zip = null) { |
|
| 100 | 100 | if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) { |
| 101 | 101 | // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979 |
| 102 | 102 | if (filter_var($path, FILTER_VALIDATE_URL)) { |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * @return $this |
| 144 | 144 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 145 | 145 | */ |
| 146 | - public function selectWorkSheet($sheetIndex=0) |
|
| 146 | + public function selectWorkSheet($sheetIndex = 0) |
|
| 147 | 147 | { |
| 148 | 148 | $this->workSheet = $this->spreadSheet->getSheet($sheetIndex); |
| 149 | 149 | return $this; |
@@ -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,42 +206,42 @@ 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){ |
|
| 227 | + if ($group_left_count == 1) { |
|
| 228 | + foreach ($this->data as $k => $v) { |
|
| 229 | 229 | $data[$v[$this->group_left[0]]][] = $v; |
| 230 | 230 | } |
| 231 | - foreach ($data as $k =>$v){ |
|
| 231 | + foreach ($data as $k =>$v) { |
|
| 232 | 232 | $data[$k] = [ |
| 233 | 233 | 'data' => $v, |
| 234 | 234 | 'count' => count($v) |
| 235 | 235 | ]; |
| 236 | 236 | } |
| 237 | 237 | $this->excelGroupLeft($data, $group_left_count); |
| 238 | - }elseif($group_left_count == 2){ |
|
| 238 | + }elseif ($group_left_count == 2) { |
|
| 239 | 239 | foreach ($this->data as $v) { |
| 240 | 240 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
| 241 | 241 | } |
| 242 | 242 | $this->data = $this->arrayCount($data); |
| 243 | 243 | $this->excelGroupLeft($this->data, $group_left_count); |
| 244 | - }else{ |
|
| 244 | + } else { |
|
| 245 | 245 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
| 246 | 246 | '左侧分组过多,导出失败!' |
| 247 | 247 | ); |
@@ -253,9 +253,9 @@ discard block |
||
| 253 | 253 | * @return void |
| 254 | 254 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 255 | 255 | */ |
| 256 | - public function excelHeader(){ |
|
| 256 | + public function excelHeader() { |
|
| 257 | 257 | $row = 1; |
| 258 | - if(!empty($this->mainTitle)){ |
|
| 258 | + if (!empty($this->mainTitle)) { |
|
| 259 | 259 | $this->workSheet->setCellValue('A'.$row, $this->mainTitle); |
| 260 | 260 | } |
| 261 | 261 | |
@@ -270,36 +270,36 @@ discard block |
||
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | // 使用实际的标题列数来合并单元格 |
| 273 | - $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount-1).$row); |
|
| 273 | + $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount - 1).$row); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | /** |
| 277 | 277 | * @return void |
| 278 | 278 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 279 | 279 | */ |
| 280 | - private function excelTitle(){ |
|
| 281 | - if(isset($this->titleConfig['title_show']) && $this->titleConfig['title_show']===false){ |
|
| 280 | + private function excelTitle() { |
|
| 281 | + if (isset($this->titleConfig['title_show']) && $this->titleConfig['title_show'] === false) { |
|
| 282 | 282 | return; |
| 283 | 283 | } |
| 284 | - if(!empty($this->titleConfig['title_start_row'])){ |
|
| 284 | + if (!empty($this->titleConfig['title_start_row'])) { |
|
| 285 | 285 | $this->_row = $this->titleConfig['title_start_row']; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | $_merge = $this->cellName($this->_col); |
| 289 | 289 | foreach ($this->fileTitle as $key => $val) { |
| 290 | - if(!empty($this->titleHeight)) { |
|
| 291 | - $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight);//行高度 |
|
| 290 | + if (!empty($this->titleHeight)) { |
|
| 291 | + $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight); //行高度 |
|
| 292 | 292 | } |
| 293 | 293 | $rowName = $this->cellName($this->_col); |
| 294 | - $this->workSheet->getStyle($rowName . $this->_row)->getAlignment()->setWrapText(true);//自动换行 |
|
| 294 | + $this->workSheet->getStyle($rowName.$this->_row)->getAlignment()->setWrapText(true); //自动换行 |
|
| 295 | 295 | if (is_array($val)) { |
| 296 | 296 | $num = 1; |
| 297 | 297 | $_cols = $this->_col; |
| 298 | 298 | foreach ($val as $k => $v) { |
| 299 | - $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
|
| 300 | - if(!empty($this->titleWidth)) { |
|
| 299 | + $this->workSheet->setCellValue($this->cellName($_cols).($this->_row + 1), $k); |
|
| 300 | + if (!empty($this->titleWidth)) { |
|
| 301 | 301 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
| 302 | - }else{ |
|
| 302 | + } else { |
|
| 303 | 303 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
| 304 | 304 | } |
| 305 | 305 | if ($num < count($val)) { |
@@ -308,23 +308,23 @@ discard block |
||
| 308 | 308 | } |
| 309 | 309 | $_cols++; |
| 310 | 310 | } |
| 311 | - $this->workSheet->mergeCells($_merge . $this->_row.':' . $this->cellName($this->_col) .$this->_row); |
|
| 312 | - $this->workSheet->setCellValue($_merge . $this->_row, $key);//设置值 |
|
| 311 | + $this->workSheet->mergeCells($_merge.$this->_row.':'.$this->cellName($this->_col).$this->_row); |
|
| 312 | + $this->workSheet->setCellValue($_merge.$this->_row, $key); //设置值 |
|
| 313 | 313 | } else { |
| 314 | 314 | if ($this->title_row != 1) { |
| 315 | - $this->workSheet->mergeCells($rowName . $this->_row.':' . $rowName . ($this->_row + $this->title_row - 1)); |
|
| 315 | + $this->workSheet->mergeCells($rowName.$this->_row.':'.$rowName.($this->_row + $this->title_row - 1)); |
|
| 316 | 316 | } |
| 317 | - $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
|
| 318 | - if(!empty($this->titleWidth)){ |
|
| 317 | + $this->workSheet->setCellValue($rowName.$this->_row, $key); //设置值 |
|
| 318 | + if (!empty($this->titleWidth)) { |
|
| 319 | 319 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
| 320 | - }else{ |
|
| 320 | + } else { |
|
| 321 | 321 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
| 322 | 322 | } |
| 323 | 323 | } |
| 324 | 324 | $this->_col++; |
| 325 | 325 | $_merge = $this->cellName($this->_col); |
| 326 | 326 | } |
| 327 | - $this->_row += $this->title_row;//当前行数 |
|
| 327 | + $this->_row += $this->title_row; //当前行数 |
|
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | /** |
@@ -335,63 +335,63 @@ discard block |
||
| 335 | 335 | private function excelSetCellValue($val) |
| 336 | 336 | { |
| 337 | 337 | //设置单元格行高 |
| 338 | - if(!empty($this->height)){ |
|
| 338 | + if (!empty($this->height)) { |
|
| 339 | 339 | $this->workSheet->getRowDimension($this->_row)->setRowHeight($this->height); |
| 340 | 340 | } |
| 341 | 341 | $_lie = 0; |
| 342 | - foreach ($this->field as $v){ |
|
| 342 | + foreach ($this->field as $v) { |
|
| 343 | 343 | $rowName = $this->cellName($_lie); |
| 344 | 344 | |
| 345 | - if(strpos($v,'.') !== false){ |
|
| 346 | - $v = explode('.',$v); |
|
| 345 | + if (strpos($v, '.') !== false) { |
|
| 346 | + $v = explode('.', $v); |
|
| 347 | 347 | $content = $val; |
| 348 | - for ($i=0;$i<count($v);$i++){ |
|
| 349 | - $content = $content[$v[$i]]??''; |
|
| 348 | + for ($i = 0; $i < count($v); $i++) { |
|
| 349 | + $content = $content[$v[$i]] ?? ''; |
|
| 350 | 350 | } |
| 351 | - }elseif($v == '_id'){ |
|
| 352 | - $content = $this->_row-$this->title_row;//自增序号列 |
|
| 353 | - }else{ |
|
| 354 | - $content = ($val[$v]??''); |
|
| 351 | + }elseif ($v == '_id') { |
|
| 352 | + $content = $this->_row - $this->title_row; //自增序号列 |
|
| 353 | + } else { |
|
| 354 | + $content = ($val[$v] ?? ''); |
|
| 355 | 355 | } |
| 356 | - if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
|
| 357 | - if($content['type'] == 'image'){ |
|
| 356 | + if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
|
| 357 | + if ($content['type'] == 'image') { |
|
| 358 | 358 | $path = $this->verifyFile($content['content']); |
| 359 | 359 | $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
| 360 | 360 | $drawing->setPath($path); |
| 361 | - if(!empty($content['height'])) { |
|
| 361 | + if (!empty($content['height'])) { |
|
| 362 | 362 | $drawing->setHeight($content['height']); |
| 363 | 363 | } |
| 364 | - if(!empty($content['width'])) { |
|
| 365 | - $drawing->setWidth($content['width']);//只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 364 | + if (!empty($content['width'])) { |
|
| 365 | + $drawing->setWidth($content['width']); //只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 366 | 366 | } |
| 367 | - if(!empty($content['offsetX'])) { |
|
| 368 | - $drawing->setOffsetX($content['offsetX']);//设置X方向偏移量 |
|
| 367 | + if (!empty($content['offsetX'])) { |
|
| 368 | + $drawing->setOffsetX($content['offsetX']); //设置X方向偏移量 |
|
| 369 | 369 | } |
| 370 | - if(!empty($content['offsetY'])) { |
|
| 371 | - $drawing->setOffsetY($content['offsetY']);//设置Y方向偏移量 |
|
| 370 | + if (!empty($content['offsetY'])) { |
|
| 371 | + $drawing->setOffsetY($content['offsetY']); //设置Y方向偏移量 |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | $drawing->setCoordinates($rowName.$this->_row); |
| 375 | 375 | $drawing->setWorksheet($this->workSheet); |
| 376 | 376 | } |
| 377 | - }elseif(is_array($content) && isset($content['formula'])){ |
|
| 377 | + }elseif (is_array($content) && isset($content['formula'])) { |
|
| 378 | 378 | // 新增:支持 ['formula' => '公式'] 写法 |
| 379 | 379 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content['formula'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA); |
| 380 | - }else { |
|
| 381 | - $content = $this->formatValue($content);//格式化数据 |
|
| 382 | - if (is_numeric($content)){ |
|
| 383 | - if($this->autoDataType && strlen($content)<11){ |
|
| 384 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
|
| 385 | - }else{ |
|
| 386 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 380 | + } else { |
|
| 381 | + $content = $this->formatValue($content); //格式化数据 |
|
| 382 | + if (is_numeric($content)) { |
|
| 383 | + if ($this->autoDataType && strlen($content) < 11) { |
|
| 384 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_NUMERIC); |
|
| 385 | + } else { |
|
| 386 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 387 | 387 | } |
| 388 | - }else{ |
|
| 389 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 388 | + } else { |
|
| 389 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 390 | 390 | } |
| 391 | 391 | } |
| 392 | - $_lie ++; |
|
| 392 | + $_lie++; |
|
| 393 | 393 | } |
| 394 | - $this->_row ++; |
|
| 394 | + $this->_row++; |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /** |
@@ -404,22 +404,22 @@ discard block |
||
| 404 | 404 | { |
| 405 | 405 | // 获取分组字段在field中的实际位置 |
| 406 | 406 | $group_field_positions = []; |
| 407 | - foreach($this->group_left as $group_field){ |
|
| 407 | + foreach ($this->group_left as $group_field) { |
|
| 408 | 408 | $position = array_search($group_field, $this->field); |
| 409 | - if($position !== false){ |
|
| 409 | + if ($position !== false) { |
|
| 410 | 410 | $group_field_positions[] = $position; |
| 411 | 411 | } |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - if(empty($group_field_positions)){ |
|
| 414 | + if (empty($group_field_positions)) { |
|
| 415 | 415 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, '分组字段未在标题中定义'); |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | $group_start = $this->_row; |
| 419 | - foreach ($data as $key => $val){ |
|
| 419 | + foreach ($data as $key => $val) { |
|
| 420 | 420 | // 第一级分组的合并单元格 |
| 421 | 421 | $rowName = $this->cellName($group_field_positions[0]); // 使用第一个分组字段的实际位置 |
| 422 | - $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row+$val['count']-1); |
|
| 422 | + $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row + $val['count'] - 1); |
|
| 423 | 423 | $this->workSheet->mergeCells($coordinate); |
| 424 | 424 | $this->workSheet->setCellValue($rowName.$this->_row, $key); |
| 425 | 425 | |
@@ -431,27 +431,27 @@ discard block |
||
| 431 | 431 | $colIdx = array_search($field, $this->field); |
| 432 | 432 | if ($colIdx !== false) { |
| 433 | 433 | $colLetter = $this->cellName($colIdx); |
| 434 | - $this->workSheet->mergeCells($colLetter.$this->_row.':'.$colLetter.($this->_row+$val['count']-1)); |
|
| 434 | + $this->workSheet->mergeCells($colLetter.$this->_row.':'.$colLetter.($this->_row + $val['count'] - 1)); |
|
| 435 | 435 | // 取本组第一个数据的值 |
| 436 | 436 | $this->workSheet->setCellValue($colLetter.$this->_row, $val['data'][0][$field] ?? ''); |
| 437 | 437 | } |
| 438 | 438 | } |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | - if($group_left_count == 1){ |
|
| 442 | - foreach ($val['data'] as $dataRow){ |
|
| 441 | + if ($group_left_count == 1) { |
|
| 442 | + foreach ($val['data'] as $dataRow) { |
|
| 443 | 443 | $this->excelSetCellValue($dataRow); |
| 444 | 444 | } |
| 445 | - }else{ |
|
| 445 | + } else { |
|
| 446 | 446 | $sub_group_start = $this->_row; |
| 447 | 447 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
| 448 | 448 | |
| 449 | - foreach ($val['data'] as $k => $v){ |
|
| 450 | - $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start+$v['count']-1); |
|
| 449 | + foreach ($val['data'] as $k => $v) { |
|
| 450 | + $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start + $v['count'] - 1); |
|
| 451 | 451 | $this->workSheet->mergeCells($coordinate); |
| 452 | 452 | $this->workSheet->setCellValue($rowName.$sub_group_start, $k); |
| 453 | 453 | |
| 454 | - foreach ($v['data'] as $data){ |
|
| 454 | + foreach ($v['data'] as $data) { |
|
| 455 | 455 | $this->excelSetCellValue($data); |
| 456 | 456 | } |
| 457 | 457 | |
@@ -470,13 +470,13 @@ discard block |
||
| 470 | 470 | * @param array $data 二维数组原始数据 |
| 471 | 471 | * @return array |
| 472 | 472 | */ |
| 473 | - private function arrayCount($data=[]) |
|
| 473 | + private function arrayCount($data = []) |
|
| 474 | 474 | { |
| 475 | - foreach ($data as $key => $val){ |
|
| 475 | + foreach ($data as $key => $val) { |
|
| 476 | 476 | $num = 0; |
| 477 | - foreach ($val as $k => $v){ |
|
| 477 | + foreach ($val as $k => $v) { |
|
| 478 | 478 | $sub_num = count($v); |
| 479 | - $num = $num+$sub_num; |
|
| 479 | + $num = $num + $sub_num; |
|
| 480 | 480 | $val[$k] = [ |
| 481 | 481 | 'count' => $sub_num, |
| 482 | 482 | 'data' => $v |
@@ -505,10 +505,10 @@ discard block |
||
| 505 | 505 | $lastValue = null; |
| 506 | 506 | $mergeStart = $rowStart; |
| 507 | 507 | for ($row = $rowStart; $row <= $rowEnd; $row++) { |
| 508 | - $cellValue = $this->workSheet->getCell($colLetter . $row)->getValue(); |
|
| 508 | + $cellValue = $this->workSheet->getCell($colLetter.$row)->getValue(); |
|
| 509 | 509 | if ($lastValue !== null && $cellValue !== $lastValue) { |
| 510 | 510 | if ($row - $mergeStart > 1) { |
| 511 | - $this->workSheet->mergeCells($colLetter . $mergeStart . ':' . $colLetter . ($row - 1)); |
|
| 511 | + $this->workSheet->mergeCells($colLetter.$mergeStart.':'.$colLetter.($row - 1)); |
|
| 512 | 512 | } |
| 513 | 513 | $mergeStart = $row; |
| 514 | 514 | } |
@@ -516,7 +516,7 @@ discard block |
||
| 516 | 516 | } |
| 517 | 517 | // 处理最后一组 |
| 518 | 518 | if ($rowEnd - $mergeStart + 1 > 1) { |
| 519 | - $this->workSheet->mergeCells($colLetter . $mergeStart . ':' . $colLetter . $rowEnd); |
|
| 519 | + $this->workSheet->mergeCells($colLetter.$mergeStart.':'.$colLetter.$rowEnd); |
|
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | 522 | } |
@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | $endCol = $this->cellName(count($this->field) - 1); |
| 533 | 533 | $startRow = 1; |
| 534 | 534 | $endRow = $this->_row - 1; |
| 535 | - $cellRange = $startCol . $startRow . ':' . $endCol . $endRow; |
|
| 535 | + $cellRange = $startCol.$startRow.':'.$endCol.$endRow; |
|
| 536 | 536 | $this->workSheet->getStyle($cellRange)->applyFromArray($this->sheetStyle); |
| 537 | 537 | } |
| 538 | 538 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | $startRow = 4; |
| 16 | 16 | $titleConfig = [ |
| 17 | 17 | 'title_show' => true, |
| 18 | - 'data_start_row' => $startRow, // 内容开始行数 |
|
| 18 | + 'data_start_row' => $startRow, // 内容开始行数 |
|
| 19 | 19 | 'group_left' => ['id'], // 以id分组 |
| 20 | 20 | 'mergeColumns' => ['meeting_name', 'time'], // 需要自动合并的字段 |
| 21 | 21 | 'title' => [ |
@@ -94,18 +94,18 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | $data[] = [ |
| 96 | 96 | 'id' => '合计', |
| 97 | - 'price' => ['formula' => '=SUM(E'.$startRow.':E'.($startRow+count($data)-1).')'], |
|
| 97 | + 'price' => ['formula' => '=SUM(E'.$startRow.':E'.($startRow + count($data) - 1).')'], |
|
| 98 | 98 | ]; |
| 99 | 99 | |
| 100 | 100 | // 配置参数 |
| 101 | 101 | $config = [ |
| 102 | - 'horizontalCenter' => true, // 是否居中 |
|
| 103 | - 'titleHeight' => 22, // 定义表头行高 |
|
| 104 | - 'titleWidth' => 20, // 定义表头列宽 |
|
| 105 | - 'height' => 22, // 定义数据行高 |
|
| 106 | - 'autoFilter' => false, // 开启自动筛选 |
|
| 107 | - 'freezePane' => false, // 冻结窗格(首行首列) |
|
| 108 | - 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
| 102 | + 'horizontalCenter' => true, // 是否居中 |
|
| 103 | + 'titleHeight' => 22, // 定义表头行高 |
|
| 104 | + 'titleWidth' => 20, // 定义表头列宽 |
|
| 105 | + 'height' => 22, // 定义数据行高 |
|
| 106 | + 'autoFilter' => false, // 开启自动筛选 |
|
| 107 | + 'freezePane' => false, // 冻结窗格(首行首列) |
|
| 108 | + 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
| 109 | 109 | // 更多样式请查询 https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#styling-cells |
| 110 | 110 | 'sheetStyle' => [ |
| 111 | 111 | 'font' => [ |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | ], |
| 119 | 119 | ]; |
| 120 | 120 | |
| 121 | -$complexFormat = function($sheet) use ($startRow,$data) { |
|
| 121 | +$complexFormat = function($sheet) use ($startRow, $data) { |
|
| 122 | 122 | $endRow = $startRow + count($data) - 1; |
| 123 | 123 | $cellRange = "A{$startRow}:E{$endRow}"; |
| 124 | 124 | |
@@ -13,12 +13,12 @@ discard block |
||
| 13 | 13 | * 注意:分组字段必须在title中定义 |
| 14 | 14 | */ |
| 15 | 15 | $titleConfig = [ |
| 16 | - 'title_row' => 2, // 表头行号 |
|
| 17 | - 'group_left' => ['user_id', 'day'], // 左侧分组字段,最多支持两级分组 |
|
| 16 | + 'title_row' => 2, // 表头行号 |
|
| 17 | + 'group_left' => ['user_id', 'day'], // 左侧分组字段,最多支持两级分组 |
|
| 18 | 18 | 'title' => [ |
| 19 | 19 | 'ID' => 'id', |
| 20 | - '用户ID' => 'user_id', // 分组字段1 |
|
| 21 | - '结算日期' => 'day', // 分组字段2 |
|
| 20 | + '用户ID' => 'user_id', // 分组字段1 |
|
| 21 | + '结算日期' => 'day', // 分组字段2 |
|
| 22 | 22 | '订单编号' => 'order_sn', |
| 23 | 23 | '下单时间' => 'create_time', |
| 24 | 24 | ] |
@@ -30,43 +30,43 @@ discard block |
||
| 30 | 30 | $data = [ |
| 31 | 31 | [ |
| 32 | 32 | 'id' => '1', |
| 33 | - 'user_id' => '1000', // 第一组用户 |
|
| 34 | - 'day' => '20220101', // 第一天 |
|
| 33 | + 'user_id' => '1000', // 第一组用户 |
|
| 34 | + 'day' => '20220101', // 第一天 |
|
| 35 | 35 | 'order_sn' => '20180101465464', |
| 36 | 36 | 'create_time' => '1687140376', |
| 37 | 37 | ], |
| 38 | 38 | [ |
| 39 | 39 | 'id' => '2', |
| 40 | - 'user_id' => '1000', // 第一组用户 |
|
| 41 | - 'day' => '20220101', // 第一天 |
|
| 40 | + 'user_id' => '1000', // 第一组用户 |
|
| 41 | + 'day' => '20220101', // 第一天 |
|
| 42 | 42 | 'order_sn' => '20180101465465', |
| 43 | 43 | 'create_time' => '1687140377', |
| 44 | 44 | ], |
| 45 | 45 | [ |
| 46 | 46 | 'id' => '3', |
| 47 | - 'user_id' => '1000', // 第一组用户 |
|
| 48 | - 'day' => '20220102', // 第二天 |
|
| 47 | + 'user_id' => '1000', // 第一组用户 |
|
| 48 | + 'day' => '20220102', // 第二天 |
|
| 49 | 49 | 'order_sn' => '20180102465466', |
| 50 | 50 | 'create_time' => '1687140378', |
| 51 | 51 | ], |
| 52 | 52 | [ |
| 53 | 53 | 'id' => '4', |
| 54 | - 'user_id' => '1001', // 第二组用户 |
|
| 55 | - 'day' => '20220101', // 第一天 |
|
| 54 | + 'user_id' => '1001', // 第二组用户 |
|
| 55 | + 'day' => '20220101', // 第一天 |
|
| 56 | 56 | 'order_sn' => '20180101465467', |
| 57 | 57 | 'create_time' => '1687140379', |
| 58 | 58 | ], |
| 59 | 59 | [ |
| 60 | 60 | 'id' => '5', |
| 61 | - 'user_id' => '1000', // 第二组用户 |
|
| 62 | - 'day' => '20220101', // 第一天 |
|
| 61 | + 'user_id' => '1000', // 第二组用户 |
|
| 62 | + 'day' => '20220101', // 第一天 |
|
| 63 | 63 | 'order_sn' => '20180101465468', |
| 64 | 64 | 'create_time' => '1687140379', |
| 65 | 65 | ], |
| 66 | 66 | [ |
| 67 | 67 | 'id' => '6', |
| 68 | - 'user_id' => '1001', // 第二组用户 |
|
| 69 | - 'day' => '20220101', // 第一天 |
|
| 68 | + 'user_id' => '1001', // 第二组用户 |
|
| 69 | + 'day' => '20220101', // 第一天 |
|
| 70 | 70 | 'order_sn' => '20180101465469', |
| 71 | 71 | 'create_time' => '1687140379', |
| 72 | 72 | ], |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | // 配置参数 |
| 76 | 76 | $config = [ |
| 77 | - 'horizontalCenter' => true, // 是否居中 |
|
| 78 | - 'titleHeight' => 22, // 定义表头行高 |
|
| 79 | - 'titleWidth' => 20, // 定义表头列宽 |
|
| 80 | - 'height' => 22, // 定义数据行高 |
|
| 81 | - 'autoFilter' => true, // 开启自动筛选 |
|
| 82 | - 'freezePane' => false, // 冻结窗格(首行首列) |
|
| 83 | - 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
| 77 | + 'horizontalCenter' => true, // 是否居中 |
|
| 78 | + 'titleHeight' => 22, // 定义表头行高 |
|
| 79 | + 'titleWidth' => 20, // 定义表头列宽 |
|
| 80 | + 'height' => 22, // 定义数据行高 |
|
| 81 | + 'autoFilter' => true, // 开启自动筛选 |
|
| 82 | + 'freezePane' => false, // 冻结窗格(首行首列) |
|
| 83 | + 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
| 84 | 84 | ]; |
| 85 | 85 | |
| 86 | 86 | // 创建导出实例并设置数据 |
@@ -13,8 +13,8 @@ discard block |
||
| 13 | 13 | * 注意:分组字段必须在title中定义 |
| 14 | 14 | */ |
| 15 | 15 | $titleConfig = [ |
| 16 | - 'title_row' => 2, // 表头占用行数 |
|
| 17 | - 'title_start_row' => null, // 表头开始行数 |
|
| 16 | + 'title_row' => 2, // 表头占用行数 |
|
| 17 | + 'title_start_row' => null, // 表头开始行数 |
|
| 18 | 18 | 'group_left' => ['id'], // 以id分组 |
| 19 | 19 | 'mergeColumns' => ['meeting_name', 'time'], // 需要自动合并的字段 |
| 20 | 20 | 'title' => [ |
@@ -97,13 +97,13 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | // 配置参数 |
| 99 | 99 | $config = [ |
| 100 | - 'horizontalCenter' => true, // 是否居中 |
|
| 101 | - 'titleHeight' => 22, // 定义表头行高 |
|
| 102 | - 'titleWidth' => 20, // 定义表头列宽 |
|
| 103 | - 'height' => 22, // 定义数据行高 |
|
| 104 | - 'autoFilter' => false, // 开启自动筛选 |
|
| 105 | - 'freezePane' => false, // 冻结窗格(首行首列) |
|
| 106 | - 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
| 100 | + 'horizontalCenter' => true, // 是否居中 |
|
| 101 | + 'titleHeight' => 22, // 定义表头行高 |
|
| 102 | + 'titleWidth' => 20, // 定义表头列宽 |
|
| 103 | + 'height' => 22, // 定义数据行高 |
|
| 104 | + 'autoFilter' => false, // 开启自动筛选 |
|
| 105 | + 'freezePane' => false, // 冻结窗格(首行首列) |
|
| 106 | + 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
| 107 | 107 | // 更多样式请查询 https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#styling-cells |
| 108 | 108 | 'sheetStyle' => [ |
| 109 | 109 | 'font' => [ |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | // 第2行:导出时间 |
| 128 | 128 | $sheet->mergeCells('A2:E2'); |
| 129 | - $sheet->setCellValue('A2', '导出时间:' . date('Y-m-d')); |
|
| 129 | + $sheet->setCellValue('A2', '导出时间:'.date('Y-m-d')); |
|
| 130 | 130 | $sheet->getStyle('A2')->getFont()->setSize(10)->getColor()->setRGB('888888'); |
| 131 | 131 | $sheet->getStyle('A2')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT); |
| 132 | 132 | |