dede織夢(mèng)系統(tǒng)怎樣導(dǎo)出后臺(tái)的文章或自定義模型中的數(shù)據(jù)到excel,并且不出現(xiàn)亂碼
在后臺(tái)目錄創(chuàng)建一個(gè)php文件toexcel.php,在最上面加入代碼;
require_once(dirname(__FILE__).'/config.php');require_once(DEDEINC.'/typelink.class.php');require_once(DEDEINC.'/datalistcp.class.php');require_once(DEDEADMIN.'/inc/inc_list_functions.php'); |
加入導(dǎo)出到excel類;
class Excel{ private $head; private $body; public function addHeader($arr){ foreach($arr as $headVal){ $headVal = $this->charset($headVal); $this->head .= "{$headVal}/t "; } $this->head .= "/n"; } public function addBody($arr){ foreach($arr as $arrBody){ foreach($arrBody as $bodyVal){ $bodyVal = $this->charset($bodyVal); $this->body .= "{$bodyVal}/t "; } $this->body .= "/n"; } } public function downLoad($filename=''){ if(!$filename) $filename = date('YmdHis',time()).'.xls'; header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=$filename"); header("Content-Type:charset=gb2312"); if($this->head) echo $this->head; echo $this->body; } public function charset($string){ return mb_convert_encoding($string,'GBK','auto'); } } |
代碼解釋:
1.輸出列名數(shù)組,并轉(zhuǎn)碼
public function addHeader($arr){ foreach($arr as $headVal){ $headVal = $this->charset($headVal); $this->head .= "{$headVal}/t "; } $this->head .= "/n"; } |
2.輸出導(dǎo)出內(nèi)容數(shù)組,并轉(zhuǎn)碼
public function addBody($arr){ foreach($arr as $arrBody){ foreach($arrBody as $bodyVal){ $bodyVal = $this->charset($bodyVal); $this->body .= "{$bodyVal}/t "; } $this->body .= "/n"; } } |
3.設(shè)置header頭部信息和導(dǎo)出到excel內(nèi)容,并輸出到瀏覽器
public function downLoad($filename=''){ if(!$filename) $filename = date('YmdHis',time()).'.xls'; header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=$filename"); header("Content-Type:charset=gb2312"); if($this->head) echo $this->head; echo $this->body; } |
4.轉(zhuǎn)碼,這里不用iconv函數(shù),有可能會(huì)與gd沖突導(dǎo)致輸出空白。用
public function charset($string){ return mb_convert_encoding($string,'GBK','auto'); } |
7.調(diào)用方法;
$excel = new Excel(); $excel->addHeader(array('列一','列二','列三','列四')); global $dsql; $sql="select 列一字段,列二字段,列三字段,列四字段 from 表名"; $dsql->SetQuery($sql);$dsql->Execute();while($row = $dsql->GetArray()){$list[]=$row;}unset($row);$excel->addBody($list); $excel->downLoad(); |
后天添加導(dǎo)出到excel代碼:
找到后臺(tái)目錄下的templets目錄,下面有個(gè)content_list.htm文件,
找到<a href="javascript:;" onClick="cAtts('attsDel',event,this)" class="coolbg"> 刪除屬性 </a>
在后面加一段代碼
<?php if($channelid==1) echo " <a href=/"toexcel.php/" class=/"coolbg/" target=/"_blank/">導(dǎo)出到excel</a>/r/n"; ?>
$channelid就是你的模型id,根據(jù)你導(dǎo)出的表填寫。填寫完之后打開后臺(tái)欄目列表就出現(xiàn)導(dǎo)出按鈕
注意事項(xiàng)
轉(zhuǎn)碼問題,根據(jù)自己的實(shí)際情況
導(dǎo)出字段,多表或自定義模型的表可以通過left join
新聞熱點(diǎn)
疑難解答
圖片精選