Detailed explanation of how php uses header to export excel

小云云
Release: 2023-03-22 20:32:01
Original
3066 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



直接把查询条件,全部传值给另外一个连接
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['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();
    }
Copy after login

Since the exported excel does not use css, I directly write a pure table view



    

编号 姓名 工种 <{$date}> 合计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<{$key}> <{$value.info.pu_name}> <{$value.info.t_name}> <{$v}> <{$value.count}>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!