Home > Backend Development > PHP Tutorial > Detailed explanation of how php uses header to export excel

Detailed explanation of how php uses header to export excel

小云云
Release: 2023-03-22 20:32:01
Original
3192 people have browsed it

This article mainly shares with you the detailed explanation of how to use header to export excel in PHP. I hope it can help you. Let's take a look at the example picture first.

This is the export execl button

<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(&#39;excel&#39;)}>?"+data;
        });
    </script>
直接把查询条件,全部传值给另外一个连接
Copy after login

lists is the query list method, excel is the export method, call the original method again

Add 2 header codes and then rewrite the view html file and export it directly

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[&#39;s_user_id&#39;]][&#39;info&#39;]))
            {
                $res[$value[&#39;s_user_id&#39;]][&#39;info&#39;] = $value;
            }
            if(!isset($res[$value[&#39;s_user_id&#39;]][&#39;sign&#39;]))
            {
                $res[$value[&#39;s_user_id&#39;]][&#39;sign&#39;] = array_pad(array(),32,"");
                unset($res[$value[&#39;s_user_id&#39;]][&#39;sign&#39;][0]);
            }

            $res[$value[&#39;s_user_id&#39;]][&#39;sign&#39;][$value[&#39;s_day&#39;]] = "是";
            $res[$value[&#39;s_user_id&#39;]][&#39;count&#39;]++;
        }

        $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();
    }
Copy after login

Since the exported excel does not use css, I directly write a pure table 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>
Copy after login

Click to export and display



Related recommendations:

js implementation to export Excel code

How to create or export Excel data tables with PHP

Five ways to export Excel with JS

The above is the detailed content of Detailed explanation of how php uses header to export excel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template