PHPExcel 表格 行 合併

                                                                               PHPExcel 表格 行 合併

1、效果

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;
    }

 

發佈了209 篇原創文章 · 獲贊 35 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章