为了账号安全,请及时绑定邮箱和手机立即绑定

用php把数据导出excel

标签:
PHP

一:先获取数据

<!--more-->

public function output($dataResult=array()){
    $headTitle = date('YmdHis');
    $title = date('YmdHis');
    $headtitle= "<tr style='height:50px;border-style:none;><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>";
    $titlename = '';
    $headtitle = "<tr>
           <th>ID</th>
           <th>条码号</th>
           <th>商品组合</th>
           <th>项目明细</th>
           <th>病人</th>
           <th>医生姓名</th>
           <th>医生折扣</th>
           <th>代理姓名</th>
           <th>业务员</th>
           <th>原价</th>
           <th>实付</th>
           <th>退款</th>
           <th>日期</th>
       </tr>";
    $filename = $title.".xls";
    $this->excelData($dataResult,$titlename,$headtitle,$filename);
}

二:输出

    /*
 *处理Excel导出
 *@param $datas array 设置表格数据
 *@param $titlename string 设置head
 *@param $title string 设置表头
 */
private function excelData($datas,$titlename,$title,$filename){
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
    $str .="<table border=1><head>".$titlename."</head>";
    $str .= $title;
    foreach ($datas  as $key=> $rt )
    {
        $str .= "<tr>";
        foreach ( $rt as $k => $v )
        {
            $str .= "<td>'{$v}</td>";
        }
        $str .= "</tr>\n";
    }
    $str .= "</table></body></html>";
    header("Content-type:application/vnd.ms-excel");
    //header( "Content-Type: application/vnd.ms-excel; name='excel'" );
    //header( "Content-type: application/octet-stream" );
    header( "Content-Disposition: attachment; filename=".$filename );
    //header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
    header( "Pragma: no-cache" );
    header( "Expires: 0" );
    exit( $str );
}
点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消