欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php如何用header导出excel详解

程序员文章站 2022-04-15 12:59:10
...
本文主要和大家分享php如何用header导出excel详解,希望能帮助到大家,我们先来看一下示例图。

php如何用header导出excel详解

这个是导出execl 按钮

<button class="btn btn-success" type="button" id="execl"><i class="icon-search"></i>导出execl</button>
<script>
        $("#execl").click(function(){
           var  data = $("form").serialize();
            location.href = "<{:U('excel')}>?"+data;
        });
    </script>
直接把查询条件,全部传值给另外一个连接

lists 是查询的列表方法, excel 是导出 方法, 把原来的方法调用一遍

添加 2个 header 代码 然后 改 写 view html 的文件 就直接导出了

function lists()
    {

        I("date") ? $this->date = I("date"):$this->date =date("Y-m");

        $where = $this->query();

        $model = M("project_sign");
        $model->join("JOIN  __PROJECT_USER__ on s_user_id = pu_id");
        $model->join("JOIN  __PROJECT_TEAM__ on s_team_id = t_id");
        $model->join("JOIN  __PROJECT__ on s_project_id = p_id");
        $model->order("s_id desc");
        $results = $model->where($where)->select();

        $res = array();
        foreach($results as $value)
        {
            if(!isset($res[$value['s_user_id']]['info']))
            {
                $res[$value['s_user_id']]['info'] = $value;
            }
            if(!isset($res[$value['s_user_id']]['sign']))
            {
                $res[$value['s_user_id']]['sign'] = array_pad(array(),32,"");
                unset($res[$value['s_user_id']]['sign'][0]);
            }

            $res[$value['s_user_id']]['sign'][$value['s_day']] = "是";
            $res[$value['s_user_id']]['count']++;
        }

        $this->results = $res;
        $this->display();
    }


    function excel()
    {
        header("Content-type: application/vnd.ms-excel; charset=utf8");
        header("Content-Disposition: attachment; filename=filename.xls");
        $this->lists();
    }

由于导出的excel 不用css 所以直接写一个 纯表格的view

<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<table border="1" class="table table-border table-bordered table-hover table-bg">
            <tr class="text-c"  >
                <th rowspan="2" >编号</th>
                <th rowspan="2">姓名</th>
                <th rowspan="2">工种</th>
                <th colspan="31"><{$date}></th>
                <th rowspan="2">合计</th>
            </tr>
             <volist name="results" id="value" key="key">
                 <tr   class="text-c">
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                    <td>6</td>
                    <td>7</td>
                    <td>8</td>
                    <td>9</td>
                    <td>10</td>
                    <td>11</td>
                    <td>12</td>
                    <td>13</td>
                    <td>14</td>
                    <td>15</td>
                    <td>16</td>
                    <td>17</td>
                    <td>18</td>
                    <td>19</td>
                    <td>20</td>
                    <td>21</td>
                    <td>22</td>
                    <td>23</td>
                    <td>24</td>
                    <td>25</td>
                    <td>26</td>
                    <td>27</td>
                    <td>28</td>
                    <td>29</td>
                    <td>30</td>
                    <td>31</td>
                </tr>
                <tr class="text-c">
                    <td><{$key}></td>
                    <td><{$value.info.pu_name}></td>
                    <td><{$value.info.t_name}></td>
                    <volist name="value.sign" id="v">
                        <td><{$v}></td>
                    </volist>
                    <td><{$value.count}></td>
                </tr>

            </volist>
        </table>
</body>
</html>

点击导出后显示

php如何用header导出excel详解

php如何用header导出excel详解

相关推荐:

js实现导出Excel代码

PHP创建或导出Excel数据表格的方法

五种JS导出Excel的方法

以上就是php如何用header导出excel详解的详细内容,更多请关注其它相关文章!

相关标签: header excel php