Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP导出报表(案例)

PHP导出报表(案例)

黄舟
黄舟asal
2017-02-06 09:59:091313semak imbas

效果

871.jpg

872.gif

需求

为了实现报表效果,自己杜撰的需求。

主要是思路,思路通了实现其他效果也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(&#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导出报表(案例)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn