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

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

会话ID应在登录时、敏感操作前和每30分钟定期重新生成。1.登录时重新生成会话ID可防会话固定攻击。2.敏感操作前重新生成提高安全性。3.定期重新生成降低长期利用风险,但需权衡用户体验。

在PHP中设置会话cookie参数可以通过session_set_cookie_params()函数实现。1)使用该函数设置参数,如过期时间、路径、域名、安全标志等;2)调用session_start()使参数生效;3)根据需求动态调整参数,如用户登录状态;4)注意设置secure和httponly标志以提升安全性。

在PHP中使用会话的主要目的是维护用户在不同页面之间的状态。1)会话通过session_start()函数启动,创建唯一会话ID并存储在用户cookie中。2)会话数据保存在服务器上,允许在不同请求间传递数据,如登录状态和购物车内容。

如何在子域名间共享会话?通过设置通用域名的会话cookie实现。1.在服务器端设置会话cookie的域为.example.com。2.选择合适的会话存储方式,如内存、数据库或分布式缓存。3.通过cookie传递会话ID,服务器根据ID检索和更新会话数据。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

Dreamweaver CS6
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!