美文网首页
php导出Excel表格

php导出Excel表格

作者: observerb | 来源:发表于2019-03-06 10:29 被阅读0次

把结果集丢入exect()就完事了

public function exect($dataResult){

        // $dataResult = array();      //todo:导出数据(自行设置)

        $headTitle = "信息";

        $title = "信息";

        $headtitle= "<tr style='height:10px;border-style:none;><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>";

                //差多少个字段就对应多少个th

        $titlename = "<tr>

                        <th style='width:70px;' >姓名</th>

                        <th style='width:70px;' >昵称</th>

                        <th style='width:70px;'>手机号</th>

                        <th style='width:150px;'>生日</th>

                        <th style='width:150px;'>籍貫</th>

                        <th style='width:150px;'>家鄉</th>

                        <th style='width:150px;'>會員卡號</th>

                        <th style='width:150px;'>性别</th>

                        <th style='width:150px;'>申請日期</th>

                    </tr>";

            $filename ='./'.$title.".xls";

            $this->excelData($dataResult,$titlename,$headtitle,$filename);

    }

    public 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 style='vnd.ms-excel.numberformat:@'>{$v}</td>";  //style='vnd.ms-excel.numberformat:@' excel显示数字前的0

            }

        $str .= "</tr>\n";

        }

        $str .= "</table></body></html>";

        // print_r($str);exit;

        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" );

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

        exit( $str );

    }

相关文章

网友评论

      本文标题:php导出Excel表格

      本文链接:https://www.haomeiwen.com/subject/wkzzuqtx.html