您好,登錄后才能下訂單哦!
這篇文章主要介紹“PHP怎么導(dǎo)出報(bào)表”,在日常操作中,相信很多人在PHP怎么導(dǎo)出報(bào)表問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PHP怎么導(dǎo)出報(bào)表”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
效果
需求
為了實(shí)現(xiàn)報(bào)表效果,自己杜撰的需求。
主要是思路,思路通了實(shí)現(xiàn)其他效果也OK。
統(tǒng)計(jì)每個(gè)人在一年中每一天遲到早退的情況。
思路
用 PHP
語言進(jìn)行實(shí)現(xiàn)。
首先將報(bào)表樣式用 HTML
實(shí)現(xiàn),
然后利用PHP header
函數(shù)生成 xls 下載。
知識(shí)點(diǎn)
表格中的列合并與行合并
PHP 獲取一年中的每一天進(jìn)行展示
PHP header 函數(shù)
Smarty 模板函數(shù)
Smarty 自定義函數(shù)
...
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); //獲取天數(shù)
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); //獲取天數(shù)
}
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>';
}
//向模板中注冊一個(gè)函數(shù)
$this->smarty->registerPlugin('function','check_week','check_week');
//模擬數(shù)據(jù)如下:
$list[0]['name'] = 'Tom';
$list[1]['name'] = 'Joan';
$list[0]['sex'] = '男';
$list[1]['sex'] = '女';
$list[0]['age'] = '30';
$list[1]['age'] = '31';
//設(shè)置遲到
$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'
];
//設(shè)置早退
$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 = "報(bào)表-".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>
到此,關(guān)于“PHP怎么導(dǎo)出報(bào)表”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。