PHP export report (case)

黄舟
Release: 2023-03-05 12:26:01
Original
1381 people have browsed it

Effect

PHP export report (case)

PHP export report (case)

Requirements

In order to achieve the report effect, I made up my own requirements.

It’s mainly about ideas. Once the ideas are clear, other effects can be achieved.

Count how many people arrive late and leave early every day of the year.

Ideas

Use PHP language to implement.

First implement the report style using HTML,

Then use the PHP header function to generate xls download.

Knowledge points

  • Column merging and row merging in tables

  • PHP gets every day of the year for display

  • PHP header function

  • Smarty template function

  • Smarty custom function

  • ...

PHP code

public function export()
{

    //获取2016年日期
    $time_start = strtotime('2016-01-01');
    $time_end   = strtotime('2016-12-31');

    $month_arr = [];
    $month_arr['month'][]   = '2016-01';
    $month_arr['numbers'][] = date('t',$time_start); //获取天数

    while (($time_start = strtotime(&#39;+1 month&#39;, $time_start)) <= $time_end) {
        $month_arr[&#39;month&#39;][]   = date(&#39;Y-m&#39;,$time_start); //取得递增月
        $month_arr[&#39;numbers&#39;][] = date(&#39;t&#39;,$time_start);     //获取天数
    }

    function check_week($time = [])
    {
        if (empty($time[&#39;day&#39;])) {
            return &#39;&#39;;
        }
        $w = intval(date(&#39;w&#39; , strtotime($time[&#39;day&#39;])));
        if( $w === 0 || $w === 6){
            return &#39;<td style="background-color: red;">&#39;
                   .date(&#39;d&#39;, strtotime($time[&#39;day&#39;]))
                   .&#39;</td>&#39;;
        }
        return &#39;<td>&#39;.date(&#39;d&#39;, strtotime($time[&#39;day&#39;])).&#39;</td>&#39;;
    }

    //向模板中注册一个函数
    $this->smarty->registerPlugin(&#39;function&#39;,&#39;check_week&#39;,&#39;check_week&#39;);

    //模拟数据如下:
    $list[0][&#39;name&#39;] = &#39;Tom&#39;;
    $list[1][&#39;name&#39;] = &#39;Joan&#39;;

    $list[0][&#39;sex&#39;] = &#39;男&#39;;
    $list[1][&#39;sex&#39;] = &#39;女&#39;;

    $list[0][&#39;age&#39;] = &#39;30&#39;;
    $list[1][&#39;age&#39;] = &#39;31&#39;;

    //设置迟到
    $list[0][&#39;late&#39;] = [
        &#39;2016-01-08&#39;,
        &#39;2016-01-09&#39;,
        &#39;2016-02-09&#39;,
        &#39;2016-03-09&#39;,
        &#39;2016-04-09&#39;,
        &#39;2016-05-09&#39;
    ];

    $list[1][&#39;late&#39;] = [
        &#39;2016-02-12&#39;,
        &#39;2016-03-15&#39;,
        &#39;2016-04-13&#39;,
        &#39;2016-05-19&#39;,
        &#39;2016-05-19&#39;
    ];

    //设置早退
    $list[0][&#39;leave&#39;] = [
        &#39;2016-03-09&#39;,
        &#39;2016-04-11&#39;,
        &#39;2016-05-15&#39;,
        &#39;2016-06-18&#39;,
        &#39;2016-07-21&#39;,
        &#39;2016-08-23&#39;,
        &#39;2016-09-22&#39;,
        &#39;2016-10-20&#39;,
        &#39;2016-11-17&#39;,
        &#39;2016-12-14&#39;,
    ];
    $list[1][&#39;leave&#39;] = [
        &#39;2016-05-09&#39;,
        &#39;2016-06-11&#39;,
        &#39;2016-07-13&#39;,
        &#39;2016-08-15&#39;,
        &#39;2016-09-17&#39;,
        &#39;2016-10-19&#39;,
        &#39;2016-11-20&#39;,
        &#39;2016-12-23&#39;,
        &#39;2016-03-18&#39;,
        &#39;2016-02-19&#39;,
        &#39;2016-01-23&#39;,
    ];

    $file_name   = "报表-".date("YmdHis",time());
    $file_suffix = "xls";
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$file_name.$file_suffix");
    $this->_assign(&#39;list&#39;, $list);
    $this->_assign(&#39;month&#39;, $month_arr);
    $this->_display();
}
Copy after login

HTML code

<html xmlns:o="urn:schemas-microsoft-com:office:office"
      xmlns:x="urn:schemas-microsoft-com:office:excel"
      xmlns="http://www.w3.org/TR/REC-html40">

<head>
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <meta name=ProgId content=Excel.Sheet>
    <meta name=Generator content="Microsoft Excel 11">
</head>

<body>
<table border=1 cellpadding=0 cellspacing=0 width="100%">
    <tr>
        <td style="vertical-align:middle;" align="center" rowspan="2">
            <b>姓名</b>
        </td>
        <td style="vertical-align:middle;" align="center" rowspan="2">
            <b>性别</b>
        </td>
        <td style="vertical-align:middle;" align="center" rowspan="2">
            <b>年龄</b>
        </td>
        {if $month}
            {foreach $month.month as $k=>$m}
                <td style="text-align: center;" colspan="{$month.numbers.$k}">
                    <b>{$m}</b>
                </td>
            {/foreach}
        {/if}
    </tr>
    <tr>
        {if $month}
        {foreach $month.month as $k=>$m}
            {section name=count loop=$month.numbers.$k+1 start=1}
                {check_week day=$m|cat:"-"|cat:$smarty.section.count.index}
            {/section}
        {/foreach}
        {/if}
    </tr>

    {if $list}
    {foreach $list as $s}
    <tr>
        <td>{$s.name|default:&#39;--&#39;}</td>
        <td>{$s.sex|default:&#39;--&#39;}</td>
        <td>{$s.age|default:&#39;--&#39;}</td>
        {if $month}
        {foreach $month.month as $k=>$m}
            {section name=count loop=$month.numbers.$k+1 start=1}
                {if $smarty.section.count.index <10 }
                     {$str = ""}
                     {$smarty.section.count.index = $str|cat:"0"|cat:$smarty.section.count.index}
                {/if}
                <td style="
                    {if $s[&#39;late&#39;]}
                        {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s[&#39;late&#39;]}
                            background-color: #5a0099;
                        {/if}
                    {/if}

                    {if $s[&#39;leave&#39;]}
                        {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s[&#39;leave&#39;]}
                            background-color: yellow;
                        {/if}
                    {/if}
                ">

                {if $s[&#39;late&#39;]}
                    {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s[&#39;late&#39;]}
                        1
                    {/if}
                {/if}

                {if $s[&#39;leave&#39;]}
                    {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s[&#39;leave&#39;]}
                        1
                    {/if}
                {/if}

                </td>

            {/section}
        {/foreach}
        {/if}
    </tr>
    {/foreach}
    <tr>
        <td style="background-color: red"></td>
        <td>*周末</td>
    </tr>
    <tr>
        <td style="background-color: white"></td>
        <td>*正常</td>
    </tr>
    <tr>
        <td style="background-color: #5a0099"></td>
        <td>*迟到</td>
    </tr>
    <tr>
        <td style="background-color: yellow"></td>
        <td>*早退</td>
    </tr>
    {/if}
</table>
</body>
</html>
Copy after login

The above is the PHP export report (case) Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


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