@@ -1,16 +1,16 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | use \tinymeng\spreadsheet\Util\ConstCode; |
| 3 | 3 | return [ |
| 4 | - 'creator'=>'tinymeng', //文件创建者 |
|
| 5 | - 'pathName'=>null, //文件存储位置 |
|
| 6 | - 'fileName'=>null, //文件名称 |
|
| 7 | - 'horizontalCenter'=>true, //是否居中 |
|
| 8 | - 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 9 | - 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 10 | - 'height'=>null, //定义数据行高,常用22 |
|
| 11 | - 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 12 | - 'autoDataType'=>true, //自动适应文本类型 |
|
| 13 | - 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 4 | + 'creator'=>'tinymeng', //文件创建者 |
|
| 5 | + 'pathName'=>null, //文件存储位置 |
|
| 6 | + 'fileName'=>null, //文件名称 |
|
| 7 | + 'horizontalCenter'=>true, //是否居中 |
|
| 8 | + 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 9 | + 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 10 | + 'height'=>null, //定义数据行高,常用22 |
|
| 11 | + 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 12 | + 'autoDataType'=>true, //自动适应文本类型 |
|
| 13 | + 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 14 | 14 | /** |
| 15 | 15 | * 字段映射方式 |
| 16 | 16 | * ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME = 1;//字段对应名称 |
@@ -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名称 |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * 文件信息 |
| 56 | 56 | * @var array |
| 57 | 57 | */ |
| 58 | - private $fileTitle=[]; |
|
| 58 | + private $fileTitle = []; |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @param $data |
| 82 | 82 | * @return $this |
| 83 | 83 | */ |
| 84 | - public function setData($data){ |
|
| 84 | + public function setData($data) { |
|
| 85 | 85 | $this->data = $data; |
| 86 | 86 | return $this; |
| 87 | 87 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * @param $data |
| 91 | 91 | * @return $this |
| 92 | 92 | */ |
| 93 | - public function getData(){ |
|
| 93 | + public function getData() { |
|
| 94 | 94 | return $this->data; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | $this->fileTitle = []; |
| 107 | 107 | $this->data = []; |
| 108 | 108 | $this->field = []; |
| 109 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 109 | + if ($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -116,9 +116,9 @@ discard block |
||
| 116 | 116 | * @throws TinymengException |
| 117 | 117 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 118 | 118 | */ |
| 119 | - public function setWorkSheetData($fileTitle,$data) |
|
| 119 | + public function setWorkSheetData($fileTitle, $data) |
|
| 120 | 120 | { |
| 121 | - if(isset($fileTitle['title_row']) || isset($fileTitle['group_left'])){ |
|
| 121 | + if (isset($fileTitle['title_row']) || isset($fileTitle['group_left'])) { |
|
| 122 | 122 | /** |
| 123 | 123 | * $fileTitle = [ |
| 124 | 124 | * 'title_row'=>1, |
@@ -128,10 +128,10 @@ discard block |
||
| 128 | 128 | * ], |
| 129 | 129 | * ]; |
| 130 | 130 | */ |
| 131 | - $this->title_row = $fileTitle['title_row']??1; |
|
| 132 | - $this->group_left = $fileTitle['group_left']??[]; |
|
| 133 | - $titleData = $fileTitle['title']??[]; |
|
| 134 | - }else{ |
|
| 131 | + $this->title_row = $fileTitle['title_row'] ?? 1; |
|
| 132 | + $this->group_left = $fileTitle['group_left'] ?? []; |
|
| 133 | + $titleData = $fileTitle['title'] ?? []; |
|
| 134 | + } else { |
|
| 135 | 135 | /** |
| 136 | 136 | * $fileTitle = [ |
| 137 | 137 | * '姓名'=>'name', |
@@ -141,14 +141,14 @@ discard block |
||
| 141 | 141 | } |
| 142 | 142 | // 根据字段映射方式处理 title |
| 143 | 143 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
| 144 | - $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
|
| 145 | - }else{ |
|
| 146 | - $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
|
| 144 | + $this->fileTitle = array_flip($titleData); // 字段对应名称方式 - 需要将键值对调 |
|
| 145 | + } else { |
|
| 146 | + $this->fileTitle = $titleData; // 名称对应字段方式 - 保持原样 |
|
| 147 | 147 | } |
| 148 | 148 | $this->data = $data; |
| 149 | 149 | |
| 150 | 150 | /** 设置第一行格式 */ |
| 151 | - if($this->mainTitleLine == true){ |
|
| 151 | + if ($this->mainTitleLine == true) { |
|
| 152 | 152 | $this->excelHeader(); |
| 153 | 153 | } |
| 154 | 154 | |
@@ -156,17 +156,17 @@ discard block |
||
| 156 | 156 | $this->excelTitle(); |
| 157 | 157 | |
| 158 | 158 | /** 获取列表里所有字段 **/ |
| 159 | - foreach ($this->fileTitle as $key => $val){ |
|
| 160 | - if(is_array($val)){ |
|
| 161 | - foreach ($val as $k => $v){ |
|
| 159 | + foreach ($this->fileTitle as $key => $val) { |
|
| 160 | + if (is_array($val)) { |
|
| 161 | + foreach ($val as $k => $v) { |
|
| 162 | 162 | $this->field[] = $v; |
| 163 | 163 | } |
| 164 | - }else{ |
|
| 164 | + } else { |
|
| 165 | 165 | $this->field[] = $val; |
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | /** 查询结果赋值 **/ |
| 169 | - if(!empty($this->data)){ |
|
| 169 | + if (!empty($this->data)) { |
|
| 170 | 170 | $this->excelSetValue(); |
| 171 | 171 | } |
| 172 | 172 | return $this; |
@@ -177,33 +177,33 @@ discard block |
||
| 177 | 177 | * @author: Tinymeng <[email protected]> |
| 178 | 178 | * @time: 2022/2/22 11:43 |
| 179 | 179 | */ |
| 180 | - public function excelSetValue(){ |
|
| 181 | - if(empty($this->group_left)){ //判断左侧是否分组 |
|
| 182 | - foreach ($this->data as $key => $val){ |
|
| 180 | + public function excelSetValue() { |
|
| 181 | + if (empty($this->group_left)) { //判断左侧是否分组 |
|
| 182 | + foreach ($this->data as $key => $val) { |
|
| 183 | 183 | $this->excelSetCellValue($val); |
| 184 | 184 | } |
| 185 | - }else{ //根据设置分组字段进行分组 |
|
| 185 | + } else { //根据设置分组字段进行分组 |
|
| 186 | 186 | /** 数据分组 **/ |
| 187 | 187 | $data = []; |
| 188 | 188 | $group_left_count = count($this->group_left); |
| 189 | - if($group_left_count == 1){ |
|
| 190 | - foreach ($this->data as $k => $v){ |
|
| 189 | + if ($group_left_count == 1) { |
|
| 190 | + foreach ($this->data as $k => $v) { |
|
| 191 | 191 | $data[$v[$this->group_left[0]]][] = $v; |
| 192 | 192 | } |
| 193 | - foreach ($data as $k =>$v){ |
|
| 193 | + foreach ($data as $k =>$v) { |
|
| 194 | 194 | $data[$k] = [ |
| 195 | 195 | 'data' => $v, |
| 196 | 196 | 'count' => count($v) |
| 197 | 197 | ]; |
| 198 | 198 | } |
| 199 | 199 | $this->excelGroupLeft($data, $group_left_count); |
| 200 | - }elseif($group_left_count == 2){ |
|
| 200 | + }elseif ($group_left_count == 2) { |
|
| 201 | 201 | foreach ($this->data as $v) { |
| 202 | 202 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
| 203 | 203 | } |
| 204 | 204 | $this->data = $this->arrayCount($data); |
| 205 | 205 | $this->excelGroupLeft($this->data, $group_left_count); |
| 206 | - }else{ |
|
| 206 | + } else { |
|
| 207 | 207 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
| 208 | 208 | '左侧分组过多,导出失败!' |
| 209 | 209 | ); |
@@ -215,36 +215,36 @@ discard block |
||
| 215 | 215 | * @return void |
| 216 | 216 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 217 | 217 | */ |
| 218 | - public function excelHeader(){ |
|
| 218 | + public function excelHeader() { |
|
| 219 | 219 | $row = 1; |
| 220 | 220 | $this->workSheet->setCellValue('A'.$row, $this->mainTitle); |
| 221 | - $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($this->_col-1).$row); |
|
| 221 | + $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($this->_col - 1).$row); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | 225 | * @return void |
| 226 | 226 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 227 | 227 | */ |
| 228 | - private function excelTitle(){ |
|
| 229 | - if($this->mainTitleLine == true){ |
|
| 230 | - $this->_row ++;//当前行数 |
|
| 228 | + private function excelTitle() { |
|
| 229 | + if ($this->mainTitleLine == true) { |
|
| 230 | + $this->_row++; //当前行数 |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | $_merge = $this->cellName($this->_col); |
| 234 | 234 | foreach ($this->fileTitle as $key => $val) { |
| 235 | - if(!empty($this->titleHeight)) { |
|
| 236 | - $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight);//行高度 |
|
| 235 | + if (!empty($this->titleHeight)) { |
|
| 236 | + $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight); //行高度 |
|
| 237 | 237 | } |
| 238 | 238 | $rowName = $this->cellName($this->_col); |
| 239 | - $this->workSheet->getStyle($rowName . $this->_row)->getAlignment()->setWrapText(true);//自动换行 |
|
| 239 | + $this->workSheet->getStyle($rowName.$this->_row)->getAlignment()->setWrapText(true); //自动换行 |
|
| 240 | 240 | if (is_array($val)) { |
| 241 | 241 | $num = 1; |
| 242 | 242 | $_cols = $this->_col; |
| 243 | 243 | foreach ($val as $k => $v) { |
| 244 | - $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
|
| 245 | - if(!empty($this->titleWidth)) { |
|
| 244 | + $this->workSheet->setCellValue($this->cellName($_cols).($this->_row + 1), $k); |
|
| 245 | + if (!empty($this->titleWidth)) { |
|
| 246 | 246 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
| 247 | - }else{ |
|
| 247 | + } else { |
|
| 248 | 248 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
| 249 | 249 | } |
| 250 | 250 | if ($num < count($val)) { |
@@ -253,23 +253,23 @@ discard block |
||
| 253 | 253 | } |
| 254 | 254 | $_cols++; |
| 255 | 255 | } |
| 256 | - $this->workSheet->mergeCells($_merge . $this->_row.':' . $this->cellName($this->_col) .$this->_row); |
|
| 257 | - $this->workSheet->setCellValue($_merge . $this->_row, $key);//设置值 |
|
| 256 | + $this->workSheet->mergeCells($_merge.$this->_row.':'.$this->cellName($this->_col).$this->_row); |
|
| 257 | + $this->workSheet->setCellValue($_merge.$this->_row, $key); //设置值 |
|
| 258 | 258 | } else { |
| 259 | 259 | if ($this->title_row != 1) { |
| 260 | - $this->workSheet->mergeCells($rowName . $this->_row.':' . $rowName . ($this->_row + $this->title_row - 1)); |
|
| 260 | + $this->workSheet->mergeCells($rowName.$this->_row.':'.$rowName.($this->_row + $this->title_row - 1)); |
|
| 261 | 261 | } |
| 262 | - $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
|
| 263 | - if(!empty($this->titleWidth)){ |
|
| 262 | + $this->workSheet->setCellValue($rowName.$this->_row, $key); //设置值 |
|
| 263 | + if (!empty($this->titleWidth)) { |
|
| 264 | 264 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
| 265 | - }else{ |
|
| 265 | + } else { |
|
| 266 | 266 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
| 267 | 267 | } |
| 268 | 268 | } |
| 269 | 269 | $this->_col++; |
| 270 | 270 | $_merge = $this->cellName($this->_col); |
| 271 | 271 | } |
| 272 | - $this->_row += $this->title_row;//当前行数 |
|
| 272 | + $this->_row += $this->title_row; //当前行数 |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
@@ -280,60 +280,60 @@ discard block |
||
| 280 | 280 | private function excelSetCellValue($val) |
| 281 | 281 | { |
| 282 | 282 | //设置单元格行高 |
| 283 | - if(!empty($this->height)){ |
|
| 283 | + if (!empty($this->height)) { |
|
| 284 | 284 | $this->workSheet->getRowDimension($this->_row)->setRowHeight($this->height); |
| 285 | 285 | } |
| 286 | 286 | $_lie = 0; |
| 287 | - foreach ($this->field as $v){ |
|
| 287 | + foreach ($this->field as $v) { |
|
| 288 | 288 | $rowName = $this->cellName($_lie); |
| 289 | 289 | |
| 290 | - if(strpos($v,'.') !== false){ |
|
| 291 | - $v = explode('.',$v); |
|
| 290 | + if (strpos($v, '.') !== false) { |
|
| 291 | + $v = explode('.', $v); |
|
| 292 | 292 | $content = $val; |
| 293 | - for ($i=0;$i<count($v);$i++){ |
|
| 294 | - $content = $content[$v[$i]]??''; |
|
| 293 | + for ($i = 0; $i < count($v); $i++) { |
|
| 294 | + $content = $content[$v[$i]] ?? ''; |
|
| 295 | 295 | } |
| 296 | - }elseif($v == '_id'){ |
|
| 297 | - $content = $this->_row-$this->title_row;//自增序号列 |
|
| 298 | - }else{ |
|
| 299 | - $content = ($val[$v]??''); |
|
| 296 | + }elseif ($v == '_id') { |
|
| 297 | + $content = $this->_row - $this->title_row; //自增序号列 |
|
| 298 | + } else { |
|
| 299 | + $content = ($val[$v] ?? ''); |
|
| 300 | 300 | } |
| 301 | - if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
|
| 302 | - if($content['type'] == 'image'){ |
|
| 301 | + if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
|
| 302 | + if ($content['type'] == 'image') { |
|
| 303 | 303 | $path = $this->verifyFile($content['content']); |
| 304 | 304 | $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
| 305 | 305 | $drawing->setPath($path); |
| 306 | - if(!empty($content['height'])) { |
|
| 306 | + if (!empty($content['height'])) { |
|
| 307 | 307 | $drawing->setHeight($content['height']); |
| 308 | 308 | } |
| 309 | - if(!empty($content['width'])) { |
|
| 310 | - $drawing->setWidth($content['width']);//只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 309 | + if (!empty($content['width'])) { |
|
| 310 | + $drawing->setWidth($content['width']); //只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 311 | 311 | } |
| 312 | - if(!empty($content['offsetX'])) { |
|
| 313 | - $drawing->setOffsetX($content['offsetX']);//设置X方向偏移量 |
|
| 312 | + if (!empty($content['offsetX'])) { |
|
| 313 | + $drawing->setOffsetX($content['offsetX']); //设置X方向偏移量 |
|
| 314 | 314 | } |
| 315 | - if(!empty($content['offsetY'])) { |
|
| 316 | - $drawing->setOffsetY($content['offsetY']);//设置Y方向偏移量 |
|
| 315 | + if (!empty($content['offsetY'])) { |
|
| 316 | + $drawing->setOffsetY($content['offsetY']); //设置Y方向偏移量 |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | $drawing->setCoordinates($rowName.$this->_row); |
| 320 | 320 | $drawing->setWorksheet($this->workSheet); |
| 321 | 321 | } |
| 322 | - }else { |
|
| 323 | - $content = $this->formatValue($content);//格式化数据 |
|
| 324 | - if (is_numeric($content)){ |
|
| 325 | - if($this->autoDataType && strlen($content)<11){ |
|
| 326 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
|
| 327 | - }else{ |
|
| 328 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 322 | + } else { |
|
| 323 | + $content = $this->formatValue($content); //格式化数据 |
|
| 324 | + if (is_numeric($content)) { |
|
| 325 | + if ($this->autoDataType && strlen($content) < 11) { |
|
| 326 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_NUMERIC); |
|
| 327 | + } else { |
|
| 328 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 329 | 329 | } |
| 330 | - }else{ |
|
| 331 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 330 | + } else { |
|
| 331 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 332 | 332 | } |
| 333 | 333 | } |
| 334 | - $_lie ++; |
|
| 334 | + $_lie++; |
|
| 335 | 335 | } |
| 336 | - $this->_row ++; |
|
| 336 | + $this->_row++; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
@@ -346,39 +346,39 @@ discard block |
||
| 346 | 346 | { |
| 347 | 347 | // 获取分组字段在field中的实际位置 |
| 348 | 348 | $group_field_positions = []; |
| 349 | - foreach($this->group_left as $group_field){ |
|
| 349 | + foreach ($this->group_left as $group_field) { |
|
| 350 | 350 | $position = array_search($group_field, $this->field); |
| 351 | - if($position !== false){ |
|
| 351 | + if ($position !== false) { |
|
| 352 | 352 | $group_field_positions[] = $position; |
| 353 | 353 | } |
| 354 | 354 | } |
| 355 | 355 | |
| 356 | - if(empty($group_field_positions)){ |
|
| 356 | + if (empty($group_field_positions)) { |
|
| 357 | 357 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, '分组字段未在标题中定义'); |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | $group_start = $this->_row; |
| 361 | - foreach ($data as $key => $val){ |
|
| 361 | + foreach ($data as $key => $val) { |
|
| 362 | 362 | // 第一级分组的合并单元格 |
| 363 | 363 | $rowName = $this->cellName($group_field_positions[0]); // 使用第一个分组字段的实际位置 |
| 364 | - $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row+$val['count']-1); |
|
| 364 | + $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row + $val['count'] - 1); |
|
| 365 | 365 | $this->workSheet->mergeCells($coordinate); |
| 366 | 366 | $this->workSheet->setCellValue($rowName.$this->_row, $key); |
| 367 | 367 | |
| 368 | - if($group_left_count == 1){ |
|
| 369 | - foreach ($val['data'] as $data){ |
|
| 368 | + if ($group_left_count == 1) { |
|
| 369 | + foreach ($val['data'] as $data) { |
|
| 370 | 370 | $this->excelSetCellValue($data); |
| 371 | 371 | } |
| 372 | - }else{ |
|
| 372 | + } else { |
|
| 373 | 373 | $sub_group_start = $this->_row; |
| 374 | 374 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
| 375 | 375 | |
| 376 | - foreach ($val['data'] as $k => $v){ |
|
| 377 | - $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start+$v['count']-1); |
|
| 376 | + foreach ($val['data'] as $k => $v) { |
|
| 377 | + $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start + $v['count'] - 1); |
|
| 378 | 378 | $this->workSheet->mergeCells($coordinate); |
| 379 | 379 | $this->workSheet->setCellValue($rowName.$sub_group_start, $k); |
| 380 | 380 | |
| 381 | - foreach ($v['data'] as $data){ |
|
| 381 | + foreach ($v['data'] as $data) { |
|
| 382 | 382 | $this->excelSetCellValue($data); |
| 383 | 383 | } |
| 384 | 384 | |
@@ -397,13 +397,13 @@ discard block |
||
| 397 | 397 | * @param array $data 二维数组原始数据 |
| 398 | 398 | * @return array |
| 399 | 399 | */ |
| 400 | - private function arrayCount($data=[]) |
|
| 400 | + private function arrayCount($data = []) |
|
| 401 | 401 | { |
| 402 | - foreach ($data as $key => $val){ |
|
| 402 | + foreach ($data as $key => $val) { |
|
| 403 | 403 | $num = 0; |
| 404 | - foreach ($val as $k => $v){ |
|
| 404 | + foreach ($val as $k => $v) { |
|
| 405 | 405 | $sub_num = count($v); |
| 406 | - $num = $num+$sub_num; |
|
| 406 | + $num = $num + $sub_num; |
|
| 407 | 407 | $val[$k] = [ |
| 408 | 408 | 'count' => $sub_num, |
| 409 | 409 | 'data' => $v |
@@ -106,7 +106,10 @@ discard block |
||
| 106 | 106 | $this->fileTitle = []; |
| 107 | 107 | $this->data = []; |
| 108 | 108 | $this->field = []; |
| 109 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 109 | + if($this->freezePane) { |
|
| 110 | + $this->workSheet->freezePane($this->freezePane); |
|
| 111 | + } |
|
| 112 | + //冻结窗格 |
|
| 110 | 113 | } |
| 111 | 114 | |
| 112 | 115 | /** |
@@ -131,7 +134,7 @@ discard block |
||
| 131 | 134 | $this->title_row = $fileTitle['title_row']??1; |
| 132 | 135 | $this->group_left = $fileTitle['group_left']??[]; |
| 133 | 136 | $titleData = $fileTitle['title']??[]; |
| 134 | - }else{ |
|
| 137 | + } else{ |
|
| 135 | 138 | /** |
| 136 | 139 | * $fileTitle = [ |
| 137 | 140 | * '姓名'=>'name', |
@@ -142,7 +145,7 @@ discard block |
||
| 142 | 145 | // 根据字段映射方式处理 title |
| 143 | 146 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
| 144 | 147 | $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
| 145 | - }else{ |
|
| 148 | + } else{ |
|
| 146 | 149 | $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
| 147 | 150 | } |
| 148 | 151 | $this->data = $data; |
@@ -161,7 +164,7 @@ discard block |
||
| 161 | 164 | foreach ($val as $k => $v){ |
| 162 | 165 | $this->field[] = $v; |
| 163 | 166 | } |
| 164 | - }else{ |
|
| 167 | + } else{ |
|
| 165 | 168 | $this->field[] = $val; |
| 166 | 169 | } |
| 167 | 170 | } |
@@ -182,7 +185,7 @@ discard block |
||
| 182 | 185 | foreach ($this->data as $key => $val){ |
| 183 | 186 | $this->excelSetCellValue($val); |
| 184 | 187 | } |
| 185 | - }else{ //根据设置分组字段进行分组 |
|
| 188 | + } else{ //根据设置分组字段进行分组 |
|
| 186 | 189 | /** 数据分组 **/ |
| 187 | 190 | $data = []; |
| 188 | 191 | $group_left_count = count($this->group_left); |
@@ -197,13 +200,13 @@ discard block |
||
| 197 | 200 | ]; |
| 198 | 201 | } |
| 199 | 202 | $this->excelGroupLeft($data, $group_left_count); |
| 200 | - }elseif($group_left_count == 2){ |
|
| 203 | + } elseif($group_left_count == 2){ |
|
| 201 | 204 | foreach ($this->data as $v) { |
| 202 | 205 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
| 203 | 206 | } |
| 204 | 207 | $this->data = $this->arrayCount($data); |
| 205 | 208 | $this->excelGroupLeft($this->data, $group_left_count); |
| 206 | - }else{ |
|
| 209 | + } else{ |
|
| 207 | 210 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
| 208 | 211 | '左侧分组过多,导出失败!' |
| 209 | 212 | ); |
@@ -244,7 +247,7 @@ discard block |
||
| 244 | 247 | $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
| 245 | 248 | if(!empty($this->titleWidth)) { |
| 246 | 249 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
| 247 | - }else{ |
|
| 250 | + } else{ |
|
| 248 | 251 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
| 249 | 252 | } |
| 250 | 253 | if ($num < count($val)) { |
@@ -262,7 +265,7 @@ discard block |
||
| 262 | 265 | $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
| 263 | 266 | if(!empty($this->titleWidth)){ |
| 264 | 267 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
| 265 | - }else{ |
|
| 268 | + } else{ |
|
| 266 | 269 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
| 267 | 270 | } |
| 268 | 271 | } |
@@ -293,9 +296,9 @@ discard block |
||
| 293 | 296 | for ($i=0;$i<count($v);$i++){ |
| 294 | 297 | $content = $content[$v[$i]]??''; |
| 295 | 298 | } |
| 296 | - }elseif($v == '_id'){ |
|
| 299 | + } elseif($v == '_id'){ |
|
| 297 | 300 | $content = $this->_row-$this->title_row;//自增序号列 |
| 298 | - }else{ |
|
| 301 | + } else{ |
|
| 299 | 302 | $content = ($val[$v]??''); |
| 300 | 303 | } |
| 301 | 304 | if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
@@ -319,15 +322,15 @@ discard block |
||
| 319 | 322 | $drawing->setCoordinates($rowName.$this->_row); |
| 320 | 323 | $drawing->setWorksheet($this->workSheet); |
| 321 | 324 | } |
| 322 | - }else { |
|
| 325 | + } else { |
|
| 323 | 326 | $content = $this->formatValue($content);//格式化数据 |
| 324 | 327 | if (is_numeric($content)){ |
| 325 | 328 | if($this->autoDataType && strlen($content)<11){ |
| 326 | 329 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
| 327 | - }else{ |
|
| 330 | + } else{ |
|
| 328 | 331 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
| 329 | 332 | } |
| 330 | - }else{ |
|
| 333 | + } else{ |
|
| 331 | 334 | $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
| 332 | 335 | } |
| 333 | 336 | } |
@@ -369,7 +372,7 @@ discard block |
||
| 369 | 372 | foreach ($val['data'] as $data){ |
| 370 | 373 | $this->excelSetCellValue($data); |
| 371 | 374 | } |
| 372 | - }else{ |
|
| 375 | + } else{ |
|
| 373 | 376 | $sub_group_start = $this->_row; |
| 374 | 377 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
| 375 | 378 | |
@@ -45,11 +45,11 @@ discard block |
||
| 45 | 45 | 'create_time'=>'1687140376', |
| 46 | 46 | 'image'=>[ |
| 47 | 47 | 'type'=>'image', |
| 48 | - 'content'=>'http://static.majiameng.com/main/img/portrait.jpg',//网络图片确保存在 |
|
| 48 | + 'content'=>'http://static.majiameng.com/main/img/portrait.jpg', //网络图片确保存在 |
|
| 49 | 49 | 'height'=>100, |
| 50 | 50 | // 'width'=>100,//只设置高,宽会自适应,如果设置宽后,高则失效 |
| 51 | 51 | ], |
| 52 | - ],[ |
|
| 52 | + ], [ |
|
| 53 | 53 | 'id'=>'2', |
| 54 | 54 | 'order_sn'=>'20190101465464', |
| 55 | 55 | 'user_id'=>'1000', |
@@ -57,16 +57,16 @@ discard block |
||
| 57 | 57 | 'create_time'=>'1687140376', |
| 58 | 58 | 'image'=>[ |
| 59 | 59 | 'type'=>'image', |
| 60 | - 'content'=>'./text.png',//本地图片确保存在 |
|
| 60 | + 'content'=>'./text.png', //本地图片确保存在 |
|
| 61 | 61 | 'height'=>100, |
| 62 | 62 | ], |
| 63 | - ],[ |
|
| 63 | + ], [ |
|
| 64 | 64 | 'id'=>'3', |
| 65 | 65 | 'order_sn'=>'20200101465464', |
| 66 | 66 | 'user_id'=>'1000', |
| 67 | 67 | 'day'=>'20220101', |
| 68 | 68 | 'create_time'=>'1687140376', |
| 69 | - ],[ |
|
| 69 | + ], [ |
|
| 70 | 70 | 'id'=>'4', |
| 71 | 71 | 'order_sn'=>'20210101465464', |
| 72 | 72 | 'user_id'=>'1001', |
@@ -76,12 +76,12 @@ discard block |
||
| 76 | 76 | ]; |
| 77 | 77 | $TSpreadSheet = TSpreadSheet::export() |
| 78 | 78 | //创建一个sheet,设置sheet表头,并给表格赋值 |
| 79 | - ->createWorkSheet($sheetName)->setWorkSheetData($title,$data); |
|
| 79 | + ->createWorkSheet($sheetName)->setWorkSheetData($title, $data); |
|
| 80 | 80 | // ->createWorkSheet($sheetName1)->setWorkSheetData($title1,$data1);//如果多个sheet可多次创建 |
| 81 | 81 | |
| 82 | 82 | //文件存储本地 |
| 83 | 83 | $path = $TSpreadSheet->generate()->save($filename); |
| 84 | -echo '生成excel路径:'.$path;exit(); |
|
| 84 | +echo '生成excel路径:'.$path; exit(); |
|
| 85 | 85 | //生成excel路径:E:\spreadsheet-php\example\public\export\20240402\export_demo_2024-04-02_351.xlsx |
| 86 | 86 | |
| 87 | 87 | //这样直接输出到浏览器中下载 |
@@ -89,16 +89,16 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | //配置参数可以通过配置文件在初始化时传入 |
| 91 | 91 | $config = [ |
| 92 | - 'pathName'=>null, //文件存储位置 |
|
| 93 | - 'fileName'=>null, //文件名称 |
|
| 94 | - 'horizontalCenter'=>true, //是否居中 |
|
| 95 | - 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 96 | - 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 97 | - 'height'=>null, //定义数据行高,常用22 |
|
| 98 | - 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 99 | - 'autoDataType'=>true, //自动适应文本类型 |
|
| 100 | - 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 101 | - 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD,//字段映射方式 |
|
| 92 | + 'pathName'=>null, //文件存储位置 |
|
| 93 | + 'fileName'=>null, //文件名称 |
|
| 94 | + 'horizontalCenter'=>true, //是否居中 |
|
| 95 | + 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 96 | + 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 97 | + 'height'=>null, //定义数据行高,常用22 |
|
| 98 | + 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 99 | + 'autoDataType'=>true, //自动适应文本类型 |
|
| 100 | + 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 101 | + 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, //字段映射方式 |
|
| 102 | 102 | ]; |
| 103 | 103 | $TSpreadSheet = TSpreadSheet::export($config); |
| 104 | 104 | //配置参数也可以后期赋值 |
@@ -13,12 +13,12 @@ discard block |
||
| 13 | 13 | * 注意:分组字段必须在title中定义 |
| 14 | 14 | */ |
| 15 | 15 | $fileTitle = [ |
| 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 | // 创建导出实例并设置数据 |