PHP怎么导出报表

发布时间:2021-07-14 15:01:32 作者:chen
来源:亿速云 阅读:134

这篇文章主要介绍“PHP怎么导出报表”,在日常操作中,相信很多人在PHP怎么导出报表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PHP怎么导出报表”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

效果

PHP怎么导出报表

PHP怎么导出报表

需求

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

主要是思路,思路通了实现其他效果也OK。

统计每个人在一年中每一天迟到早退的情况。

思路

用 PHP 语言进行实现。

首先将报表样式用 HTML 实现,

然后利用PHP header 函数生成 xls 下载。

知识点

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 >'
                  .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  align="center" rowspan="2">
           <b>姓名</b>
       </td>        <td  align="center" rowspan="2">
           <b>性别</b>
       </td>        <td  align="center" rowspan="2">
           <b>年龄</b>
       </td>        {if $month}            {foreach $month.month as $k=>$m}                <td  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 ></td>        <td>*周末</td>    </tr>    <tr>        <td ></td>        <td>*正常</td>    </tr>    <tr>        <td ></td>        <td>*迟到</td>    </tr>    <tr>        <td ></td>        <td>*早退</td>    </tr>    {/if} </table> </body> </html>

到此,关于“PHP怎么导出报表”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. php如何导出excel
  2. MVC如何用Aspose.Word导出Word报表

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Python中怎么传递函数参数

下一篇:Linux中遇到device is busy怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》