php报表的查询打印导出,PHP导出报表(案例)

效果

2c3b6ff7ae0211e42a685d48c1f21f8e.png

d70359f288fb3ef562c687718c119884.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('+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 '

'

.date('d', strtotime($time['day']))

.'

';

}

return '

'.date('d', strtotime($time['day'])).'';

}

//向模板中注册一个函数

$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 代码

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns="http://www.w3.org/TR/REC-html40">

姓名

性别

年龄

{if $month}

{foreach $month.month as $k=>$m}

{$m}

{/foreach}

{/if}

{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}

{if $list}

{foreach $list as $s}

{$s.name|default:'--'}{$s.sex|default:'--'}{$s.age|default:'--'}

{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}

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}

{/section}

{/foreach}

{/if}

{/foreach}

*周末*正常*迟到*早退

{/if}

拓展阅读

若手机上展示样式不太好,可在PC上查看。

欢迎关注公众号,一起交流学习 ~

Thanks ~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值