使用thinkphp5對(duì)excel文件進(jìn)行導(dǎo)出

使用thinkphp5對(duì)excel文件進(jìn)行導(dǎo)出

你好!有些人覺得excel導(dǎo)出很復(fù)雜,但其實(shí)很簡(jiǎn)單,兩步操作即可完成想要的數(shù)據(jù)導(dǎo)出,趕緊試試把。

第一步 將excel配置到tp框架的慣例配置common中 ? ? (推薦學(xué)習(xí):thinkphp5

//?應(yīng)用公共文件 /** ?*?導(dǎo)出excel ?*?@param?$strTable????表格內(nèi)容 ?*?@param?$filename?文件名 ?*/ function?downloadExcel($strTable,?$filename) { ????header("Content-type:?application/vnd.ms-excel"); ????header("Content-Type:?application/force-download"); ????header("Content-Disposition:?attachment;?filename="?.?$filename?.?"_"?.?date('Y-m-d')?.?".xls"); ????header('Expires:0'); ????header('Pragma:public'); ????echo?'<meta>'?.?$strTable?.?''; }

第二步 控制器代碼

可以查詢出想要道出的數(shù)據(jù),在下列td中遍歷出來即可。簡(jiǎn)單方便你學(xué)會(huì)了嗎?

立即學(xué)習(xí)PHP免費(fèi)學(xué)習(xí)筆記(深入)”;

public?function?excel_daochu(){  ????????$strTable?='
‘; ????????$strTable?.=?‘

‘; ????????$strTable?.=?‘

‘; ????????$strTable?.=?‘

‘; ????????$strTable?.=?‘

‘; ????????$strTable?.=?‘

‘; ????????$strTable?.=?‘

‘; ????????????$aa?=?db(“user”)->select(); ???foreach?($aa?as?$k?=>?&$v)?{ ???????????????$v[‘add_time’]?=?date(‘Y-m-d?H:i:s’,$v[‘add_time’]); ????????????} ?????????? ????????foreach($aa?as?$k=>$val){ ????????????$strTable?.=?‘

‘; ????????????$strTable?.=?‘

‘; ????????????$strTable?.=?‘

‘; ????????????$strTable?.=?‘

‘; ????????????$strTable?.=?‘

‘; ????????????$strTable?.=?‘

‘; ????????} ????????$strTable?.=’

ID 用戶賬號(hào) 昵稱 時(shí)間
‘.$val[‘user_id’].’ ‘.$val[‘tel’].’ ‘.$val[‘ni_name’].’? ‘.$val[‘add_time’].’

‘; ????????downloadExcel($strTable,’用戶列表’); ????????exit(); ????}

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊11 分享