table导出excel php_php导出excel表格的方法分享(代码)
本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于php導(dǎo)出excel表格的方法分享(代碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。
網(wǎng)站后臺(tái)有很多列表數(shù)據(jù),常常都會(huì)有導(dǎo)出excel表格的需求,和大家分享一個(gè)實(shí)用的導(dǎo)出excel表格方法;
不多說(shuō),上代碼;/**
* @param array $data 要導(dǎo)出的數(shù)據(jù)
* @param array $title excel表格的表頭
* @param string $filename 文件名
*/
public function daochu_excel($data=array(),$title=array(),$filename='報(bào)表'){//導(dǎo)出excel表格
//處理中文文件名
ob_end_clean();
Header('content-Type:application/vnd.ms-excel;charset=utf-8');
header("Content-Disposition:attachment;filename=export_data.xls");
//處理中文文件名
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
if (preg_match("/MSIE/", $ua) || preg_match("/LCTE/", $ua) || $ua == 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko') {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
}else {
header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
}
header ( "Content-type:application/vnd.ms-excel" );
$html = "
".$filename."td{
text-align:center;
font-size:12px;
font-family:Arial, Helvetica, sans-serif;
border:#1C7A80 1px solid;
color:#152122;
width:auto;
}
table,tr{
border-style:none;
}
.title{
background:#7DDCF0;
color:#FFFFFF;
font-weight:bold;
}
foreach($title as $k=>$v){
$html .= "
".$v."";}
$html .= "
";foreach ($data as $key => $value) {
$html .= "
";foreach($value as $aa){
$html .= "
".$aa."";}
$html .= "
";}
$html .= "
";echo $html;
exit;
}
$title參數(shù)的數(shù)據(jù)是一個(gè)一維數(shù)組,如下:
$data參數(shù)是一個(gè)二維數(shù)組,如下:
調(diào)用方法:$daochuData = DB::table('scholarship_to_weixin as s')->leftJoin('users as u','s.uid','=','u.id')
->leftJoin('admin as a','a.id','=','s.tx_checkid')
->orderBy('s.times','desc')
->select('s.*','u.nickname','u.tel','u.id as u_id','a.name as a_name','u.admin_beizhu_name')
->get();
$title = array('序號(hào)','申請(qǐng)時(shí)間','申請(qǐng)人','備注名稱(chēng)','申請(qǐng)人手機(jī)號(hào)','提現(xiàn)金額','操作時(shí)間','操作人');
$arr = [];
foreach($daochuData as $k=>$v){
$arr[] = array(
$k+1,
$v->times,
$v->nickname,
$v->admin_beizhu_name,
$v->tel,
$v->money,
$v->s_times,
$v->a_name
);
}
$this->daochu_excel($arr,$title,'紅包提現(xiàn)到微信記錄');
結(jié)果:
希望對(duì)您有幫助。
總結(jié)
以上是生活随笔為你收集整理的table导出excel php_php导出excel表格的方法分享(代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python解析xml数据_数据开发_P
- 下一篇: [译] Bounds Check Eli