tp5 PHPExcel下载导出Excel文件

1、HTML代码

<table class="table table-border table-bordered table-bg">
        <thead>
            <tr>
                <th scope="col" colspan="7">数据列表<a href="__ROOT__/admin.php/Import/excel_data" style="margin-left: 30px;">导出</a></th>
            </tr>
            <tr class="text-c">
                <!-- <th width="25"><input type="checkbox" value="" id="CheckAll" name='CheckAll'></th> -->
                <th width="40">ID</th>
                <th width="150">姓名</th>
                <th width="150">电话</th>
                <th width="150">批次</th>
                <th width="130">工号</th>
                <th width="150">备注</th>
                <th width="100">操作</th>
            </tr>
        </thead>
        <tbody>
        {if condition="!empty($list)"}
            {foreach $list as $vo}
                <tr class="text-c" id="{$vo.id}">
                    <!-- <td><input type="checkbox" value="{$vo.id}" id='Check[]' name='Check[]'></td> -->
                    <td>{$vo.id}</td>
                    <td>{$vo.uname}</td>
                    <td>{$vo.utel1}</td>
                    <td>{$vo.upc}</td>
                    <td>{$vo.workno}</td>
                    <td>{$vo.uremark}</td>
                    <td class="td-manage">
                        <a title="编辑" href="javascript:;"style="text-decoration:none">查看</a>|<a title="删除" href="javascript:;" class="ml-5" style="text-decoration:none">删除
                        </a>
                    </td>
                </tr>
            {/foreach}
        {else /}
            <tr class="text-c"><td colspan="9">还没有添加数据</td></tr>
        {/if}
        </tbody>
    </table>
    {$page}

2、PHP代码

/**
     * 导出excel文件
     * @return mixed
     */
    public function excel_data(){
        vendor("PHPExcel.PHPExcel"); 
        $objPHPExcel = new \PHPExcel();
        // $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        // $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        $PHPSheet = $objPHPExcel->getActiveSheet();

        // $PHPSheet->setTitle("数据导出"); //给当前活动sheet设置名称
        $PHPSheet->setCellValue("A1", "ID")
            ->setCellValue("B1", "姓名")
            ->setCellValue("C1", "电话")
            ->setCellValue("D1", "批次")
            ->setCellValue("E1", "工号");

        $PHPSheet->getColumnDimension('C')->setWidth(15);//设置宽度
        $PHPSheet->getColumnDimension('D')->setWidth(15);
        // //设置水平居中 
        // $PHPSheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    
        // $PHPSheet->getStyle('B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // //垂直居中
        // $PHPSheet->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        // $PHPSheet->getStyle('B2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //左对齐
        $PHPSheet->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
        $PHPSheet->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
        //右对齐
        // $PHPSheet->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $where['entid'] = 1;
        $data = Db::table('cti_phone')
                    ->where($where)
                    ->order('id desc')
                    ->select();
        // dump($data);die;
        $d = 2;
        foreach($data as $key=>$vo){
            $PHPSheet->setCellValue("A".$d,$vo['id'])
                ->setCellValue("B".$d,$vo['uname'])
                ->setCellValue("C".$d,$vo['utel1'])
                ->setCellValue("D".$d,$vo['upc'])
                ->setCellValue("E".$d,$vo['workno']);
            $d++;
        }
        $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
        ob_end_clean(); // Added by me
        ob_start(); // Added by me
        header('Content-Disposition: attachment;filename="表单数据.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }

3、效果图

猜你喜欢

转载自blog.csdn.net/lfbin5566/article/details/81232322