php原生导出简单word表格(TP为例) (原)

 

后台:

# 菲律宾名单word导出
    public function export_word(){
            $tids = $_GET['tids'];
            $userinfo=M("philippines_visa")->where(["philippines_id"=>$tids])->select();
            foreach ($userinfo as $key => $value) {
                $userinfo[$key]['birth'] = explode('-', $value['date_of_birth']);   # 出生日期
                $userinfo[$key]['birth_m'] = $this->get_month($userinfo[$key]['birth']['1']);#月份英文
                $userinfo[$key]['lssue'] = explode('-', $value['lssue_date']);      # 签发日期
                $userinfo[$key]['lssue_m'] = $this->get_month($userinfo[$key]['lssue']['1']);#月份英文
                $userinfo[$key]['expiration'] = explode('-', $value['expiration_date']); # 有效日期
                $userinfo[$key]['expiration_m'] = $this->get_month($userinfo[$key]['expiration']['1']);#月份英文
            }
            $user_info = $userinfo;
            $user_info = array_chunk($user_info, 20, true); # 拆分为20个元素一组的多维数组 (需求而已)
            $this->assign('user_info',$user_info);
            $filename = '名单';
            ob_start(); //打开缓冲区
            $this->display();
            header("Cache-Control: public");
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) {
                header('Content-Disposition: attachment; filename='.$filename.'.doc');
            }else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) {
                Header('Content-Disposition: attachment; filename='.$filename.'.doc');
            } else {
                header('Content-Disposition: attachment; filename='.$filename.'.doc');
            }
            header("Pragma:no-cache");
            header("Expires:0");
            ob_end_flush();//输出全部内容到浏览器 
    }

 

前端页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菲律宾签证</title>
    <style>
        @page{
            margin: 30px;
        }
        .box{page-break-after:always;}
        input{font-family: '宋体';}
        table{
             border-collapse: collapse;     /*设置列、行间距*/
        }
        table td,table th{
            text-align:center;
        }
    </style>
</head>
<body>
<!-- 名单模板三 -->
            <foreach name="user_info" item="user_page" key="key">
                    <div class="box box7 page" style="border-bottom:none;">
                        <table>
                            <tr>
                                <th style="width:4%;border:1px solid #000"></th>
                                <th style="width:10%;border:1px solid #000">SURNAME</th>
                                <th style="width:12%;border:1px solid #000">GIVEN NAME</th>
                                <th style="width:5%;border:1px solid #000">SEX</th>
                                <th style="width:5%;border:1px solid #000">(Birth Month) MM</th>
                                <th style="width:5%;border:1px solid #000">(Birth Day) DD</th>
                                <th style="width:5%;border:1px solid #000">(Birth Year) YY</th>
                                <th style="width:12%;border:1px solid #000">Passport Number</th>
                            </tr>
                            <tbody class="">
                                <foreach name="user_page" item="vo" key="k">
                                    <tr>
                                        <td style="border:1px solid #000">{$k+1}</td>
                                        <td style="border:1px solid #000">{$vo.english_name}</td>
                                        <td style="border:1px solid #000">{$vo.english_name_s}</td>
                                        <td style="border:1px solid #000"><eq name='vo.m_sex' value='男'>M<else />F</eq></td>
                                        <td style="border:1px solid #000">{$vo['birth']['1']}</td>
                                        <td style="border:1px solid #000">{$vo['birth']['2']}</td>
                                        <td style="border:1px solid #000">{$vo['birth']['0']}</td>
                                        <td style="border:1px solid #000">{$vo.m_visa_number}</td>
                                    </tr>
                                </foreach>
                            </tbody>
                        </table>
                    </div>
                </foreach>
</body>
</html>                

 呈现:

 

posted @ 2019-11-01 16:27  戈丫汝  阅读(1049)  评论(0编辑  收藏  举报