thinkphp 实现下载excel表格数据 直接使用 不需要phpexcel 插件

/**
 * 导出excel
 * @param $strTable    表格内容
 * @param $filename 文件名
 */
   class Tool extends Controller{
    public function export()
    {
    
        $data = M('board')->order('add_time desc')->select();
        $strTable ='<table width="800" border="1">';
        // $strTable .= '<tr><td colspan="7" style="text-align:center;">'.date("Y-m-d",$this->begin).'至'.date("Y-m-d",$this->end).'信息</td></tr>';
        $strTable .= '<tr><td colspan="4" style="text-align:center;">留言信息</td></tr>';
        $strTable .= '<tr>';
        
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">公司</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">查询结果</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">查询时间</td>';
        $strTable .= '</tr>';

        foreach($data as $key => $value){
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:center;font-size:12px;">'.$value['mobile'].'</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;">'.$value['company'].'</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;">'.$value['result'].'</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;">'.date("Y-m-d H:i",$value['add_time']).'</td>';
            $strTable .= '</tr>';

        }

        $strTable .='</table>';
        // unset($orderList);
        $this->downloadExcel($strTable,'boards');
        exit();
            

    }


    public function downloadExcel($strTable,$filename)
    {
       header("Content-type: application/vnd.ms-excel");
       header("Content-Type: application/force-download");
       header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
       header('Expires:0');
       header('Pragma:public');
       echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
    }

}

前台

<a href="{:U('tool/export')}"><button class="layui-btn export" style="float: right;clear: right;margin-bottom: 15px;">导出</button></a>

获取更多学习资源加下面公众号

猜你喜欢

转载自blog.csdn.net/limingyue0312/article/details/81239804