首頁 >後端開發 >php教程 >PHP匯出報表(案例)

PHP匯出報表(案例)

黄舟
黄舟原創
2017-02-06 09:59:091403瀏覽

效果

PHP匯出報表(案例)

PHP匯出報表(案例)

需求

為了實現報表效果,自己杜撰的需求。

主要是思路,思路通了實現其他效果也OK。

統計每個人在一年中每一天遲到早退的情況。

思維

用 PHP 語言實現。

先將報表樣式用 HTML 實現,

然後利用PHP header 函數產生 xls 下載。

知識點

  • 表格中的列合併與行合併

  • PHP 取得一年中的每一天進行展示

  • PHP header 函數函數的每一天進行展示

  • marty 自定義函數
  • ...
  • PHP 程式碼
  • 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();
    }

    HTML 程式碼

    <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>
  • 以上就是PHP匯出報表(案例)的內容,有更多內容內容請注意PHPcnPcn. !

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn