PHP export report (case)
Effect
Requirements
In order to achieve the report effect, I made up my own requirements.
It’s mainly about ideas. Once the ideas are clear, other effects can be achieved.
Count how many people arrive late and leave early every day of the year.
Ideas
Use PHP language to implement.
First implement the report style using HTML,
Then use the PHP header function to generate xls download.
Knowledge points
Column merging and row merging in tables
PHP gets every day of the year for display
PHP header function
Smarty template function
Smarty custom function
...
PHP code
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 code
<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>
The above is the PHP export report (case) Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
