PHP导出报表(案例)

砾漠继承
• 阅读 2477
效果

PHP导出报表(案例)

PHP导出报表(案例)

需求

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

主要是思路,思路通了实现其他效果也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导出带样式的Excel

PHP header 的几种用法

Smarty 模板函数


Thanks ~

一起学习

PHP导出报表(案例)

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
皕杰报表(关于日期时间时分秒显示不出来)
在使用皕杰报表设计器时,数据据里面是日期型,但当你web预览时候,发现有日期时间类型的数据时分秒显示不出来,只有年月日能显示出来,时分秒显示为0:00:00。1.可以使用tochar解决,数据集用selecttochar(flowdate,"yyyyMMddHH:mm:ss")fromtablename2.也可以把数据库日期类型date改成timestamp
梦
4年前
微信小程序new Date()转换时间异常问题
微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT
Stella981 Stella981
3年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Wesley13 Wesley13
3年前
MySQL总结(十一)子查询
!(https://oscimg.oschina.net/oscnet/upa344f41e81d3568e3310b5da00c57ced8ea.png)子查询1\.什么是子查询需求:查询开发部中有哪些员工selectfromemp;通
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n