PHP匯出報表(案例)

來源:互聯網
上載者:User
效果

需求

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

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

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

思路

用 PHP 語言進行實現。

首先將報表樣式用 HTML 實現,

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

知識點

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

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

  • PHP header 函數

  • Smarty 模板函數

  • Smarty 自訂函數

  • ...

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('+1 month', $time_start)) <= $time_end) {        $month_arr['month'][]   = date('Y-m',$time_start); //取得遞增月        $month_arr['numbers'][] = date('t',$time_start);     //擷取天數    }    function check_week($time = [])    {        if (empty($time['day'])) {            return '';        }        $w = intval(date('w' , strtotime($time['day'])));        if( $w === 0 || $w === 6){            return '<td style="background-color: red;">'                   .date('d', strtotime($time['day']))                   .'</td>';        }        return '<td>'.date('d', strtotime($time['day'])).'</td>';    }    //向模板中註冊一個函數    $this->smarty->registerPlugin('function','check_week','check_week');    //類比資料如下:    $list[0]['name'] = 'Tom';    $list[1]['name'] = 'Joan';    $list[0]['sex'] = '男';    $list[1]['sex'] = '女';    $list[0]['age'] = '30';    $list[1]['age'] = '31';    //設定遲到    $list[0]['late'] = [        '2016-01-08',        '2016-01-09',        '2016-02-09',        '2016-03-09',        '2016-04-09',        '2016-05-09'    ];    $list[1]['late'] = [        '2016-02-12',        '2016-03-15',        '2016-04-13',        '2016-05-19',        '2016-05-19'    ];    //設定早退    $list[0]['leave'] = [        '2016-03-09',        '2016-04-11',        '2016-05-15',        '2016-06-18',        '2016-07-21',        '2016-08-23',        '2016-09-22',        '2016-10-20',        '2016-11-17',        '2016-12-14',    ];    $list[1]['leave'] = [        '2016-05-09',        '2016-06-11',        '2016-07-13',        '2016-08-15',        '2016-09-17',        '2016-10-19',        '2016-11-20',        '2016-12-23',        '2016-03-18',        '2016-02-19',        '2016-01-23',    ];    $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('list', $list);    $this->_assign('month', $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:'--'}</td>        <td>{$s.sex|default:'--'}</td>        <td>{$s.age|default:'--'}</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['late']}                        {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['late']}                            background-color: #5a0099;                        {/if}                    {/if}                    {if $s['leave']}                        {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['leave']}                            background-color: yellow;                        {/if}                    {/if}                ">                {if $s['late']}                    {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['late']}                        1                    {/if}                {/if}                {if $s['leave']}                    {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['leave']}                        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匯出報表(案例)的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.