dede織夢系統(tǒng)怎樣導(dǎo)出后臺的文章或自定義模型中的數(shù)據(jù)到excel,并且不出現(xiàn)亂碼
在后臺目錄創(chuàng)建一個php文件toexcel.php,在最上面加入代碼;
1 |
require_once(dirname(__FILE__).'/config.php'); |
2 |
require_once(DEDEINC.'/typelink.class.php'); |
3 |
require_once(DEDEINC.'/datalistcp.class.php'); |
4 |
require_once(DEDEADMIN.'/inc/inc_list_functions.php'); |
|
加入導(dǎo)出到excel類;
05 |
public function addHeader($arr){ |
06 |
foreach($arr as $headVal){ |
07 |
$headVal = $this->charset($headVal); |
08 |
$this->head .= "{$headVal}t "; |
12 |
public function addBody($arr){ |
13 |
foreach($arr as $arrBody){ |
14 |
foreach($arrBody as $bodyVal){ |
15 |
$bodyVal = $this->charset($bodyVal); |
16 |
$this->body .= "{$bodyVal}t "; |
21 |
public function downLoad($filename=''){ |
23 |
$filename = date('YmdHis',time()).'.xls'; |
24 |
header("Content-type:application/vnd.ms-excel"); |
25 |
header("Content-Disposition:attachment;filename=$filename"); |
26 |
header("Content-Type:charset=gb2312"); |
31 |
public function charset($string){ |
32 |
return mb_convert_encoding($string,'GBK','auto'); |
|
代碼解釋:
1.輸出列名數(shù)組,并轉(zhuǎn)碼
1 |
public function addHeader($arr){ |
2 |
foreach($arr as $headVal){ |
3 |
$headVal = $this->charset($headVal); |
4 |
$this->head .= "{$headVal}t "; |
|
2.輸出導(dǎo)出內(nèi)容數(shù)組,并轉(zhuǎn)碼
1 |
public function addBody($arr){ |
2 |
foreach($arr as $arrBody){ |
3 |
foreach($arrBody as $bodyVal){ |
4 |
$bodyVal = $this->charset($bodyVal); |
5 |
$this->body .= "{$bodyVal}t "; |
|
3.設(shè)置header頭部信息和導(dǎo)出到excel內(nèi)容,并輸出到瀏覽器
01 |
public function downLoad($filename=''){ |
03 |
$filename = date('YmdHis',time()).'.xls'; |
04 |
header("Content-type:application/vnd.ms-excel"); |
05 |
header("Content-Disposition:attachment;filename=$filename"); |
06 |
header("Content-Type:charset=gb2312"); |
|
4.轉(zhuǎn)碼,這里不用iconv函數(shù),有可能會與gd沖突導(dǎo)致輸出空白。用
1 |
public function charset($string){ |
2 |
return mb_convert_encoding($string,'GBK','auto'); |
|
5.調(diào)用方法;
02 |
$excel->addHeader(array('列一','列二','列三','列四')); |
04 |
$sql="select 列一字段,列二字段,列三字段,列四字段 from 表名"; |
05 |
$dsql->SetQuery($sql); |
07 |
while($row = $dsql->GetArray()){ |
11 |
$excel->addBody($list); |
|
后天添加導(dǎo)出到excel代碼:
找到后臺目錄下的templets目錄,下面有個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>rn"; ?>
$channelid就是你的模型id,根據(jù)你導(dǎo)出的表填寫。填寫完之后打開后臺欄目列表就出現(xiàn)導(dǎo)出按鈕
注意事項
轉(zhuǎn)碼問題,根據(jù)自己的實際情況
導(dǎo)出字段,多表或自定義模型的表可以通過left join