欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHPExcel 表格 行 合并

程序员文章站 2022-05-17 14:45:24
...

                                                                               PHPExcel 表格 行 合并

1、效果

PHPExcel 表格 行 合并

2、代码

public function export(){
        $data = $this->getData();
        $titleName = '标题名-'.date('Y-m-d-H_i_s');
        header("Content-type:application/vnd.ms-excel;charset=GBK");
        header("Content-Disposition:filename={$titleName}.xls");

        //表格拼接
        $tableHtml = <<<EOF
<table border="1">
   <thead>
        <tr >
            <th>模型</th>
            <th>类名</th>
            <th>方法名</th>
            <th>方法参数</th>
            <th>方法返回值</th>
        </tr>
   </thead>
  <tbody>
EOF;

        foreach ($data as $k => $v){    //模型
            $rowCount = $this->getChildrenCount($v);
            $tableHtml .= '<tr>';
            $tableHtml .= "<td rowspan='{$rowCount}'>{$v['model']}</td>";

            foreach ($v['childrens'] as $kk => $vv){ //类名
                $rowCount = $this->getChildrenCount($vv);
                $tableHtml .= "<td rowspan='{$rowCount}'>{$vv['class']}</td>";

                foreach ($vv['childrens'] as $kkk => $vvv){  //方法详情
                    $tableHtml .= <<<EOF
<td>{$vvv['function']}</td>
<td>{$vvv['param']}</td>
<td>{$vvv['return']}</td>
</tr>
EOF;
                }
            }
        }

        $tableHtml .='</tbody></table>';
        echo $tableHtml;    //输出表格内容
        exit();
    }

    /**
     * 递归计算一个数据下的总行数
     * @param $data
     * @param int $count
     * @return int
     */
    public function getChildrenCount($data, &$count = 0){
        if(isset($data['childrens'])){
            $tempData = $data['childrens'];
            foreach ($tempData as $k1 => $v1){
                $this->getChildrenCount($v1, $count);
            }
        }else{
            $count++;
        }
        return $count;
    }

    public function getData(){
        $data = [
            [
                'id' => 1,
                'model' => '模块1',
                'childrens' => [
                    [
                        'id' => 2,
                        'class' => '模块1-类1',
                        'childrens' => [
                            [
                                'id'=>4,
                                'function'=>'模块1-类1-方法1',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块1-类1-方法2',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块1-类1-方法3',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块1-类1-方法4',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                        ]
                    ],
                    [
                        'id' => 2,
                        'class' => '模块1-类2',
                        'childrens' => [
                            [
                                'id'=>4,
                                'function'=>'模块1-类2-方法1',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块1-类2-方法2',
                                'param' => '参数',
                                'return' => '返回值1'
                            ]
                        ]
                    ]
                ]
            ],
            [
                'id' => 1,
                'model' => '模块2',
                'childrens' => [
                    [
                        'id' => 2,
                        'class' => '模块2-类1',
                        'childrens' => [
                            [
                                'id'=>4,
                                'function'=>'模块2-类1-方法1',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块2-类1-方法2',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块2-类1-方法3',
                                'param' => '参数',
                                'return' => '返回值1'
                            ]
                        ]
                    ],
                    [
                        'id' => 2,
                        'class' => '模块2-类2',
                        'childrens' => [
                            [
                                'id'=>4,
                                'function'=>'模块2-类2-方法1',
                                'param' => '参数',
                                'return' => '返回值1'
                            ],
                            [
                                'id'=>4,
                                'function'=>'模块2-类2-方法2',
                                'param' => '参数',
                                'return' => '返回值1'
                            ]
                        ]
                    ]
                ]
            ],
            [
                'id' => 1,
                'model' => '模块3',
                'childrens' => [
                    [
                        'id' => 2,
                        'class' => '模块3-类1',
                        'childrens' => [
                            [
                                'id'=>4,
                                'function'=>'模块2-类1-方法1',
                                'param' => '参数',
                                'return' => '返回值1'
                            ]
                        ]
                    ]
                ]
            ],
        ];
        return $data;
    }