1、在織夢(mèng)/dede/templets下面的member_main.htm,在全選按鈕那里添加一個(gè)導(dǎo)出excel按鈕;代碼如下:
<a href="toexcel.php" class="coolbg" target="_blank">導(dǎo)出到excel</a>
2、在織夢(mèng)后臺(tái)dede文件夾下面新建toexcel.php;
toexcel.php的代碼如下:
<?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');
class Excel
{
private $head;
private $body;
//輸出列名數(shù)組,并轉(zhuǎn)碼
public function addHeader($arr){
foreach($arr as $headVal){
$headVal = $this->charset($headVal);
$this->head .= "{$headVal}\t ";
}
$this->head .= "\n";
}
//輸出導(dǎo)出內(nèi)容數(shù)組
public function addBody($arr){
foreach($arr as $arrBody){
foreach($arrBody as $bodyVal){
//$bodyVal = $this->charset($bodyVal); (這個(gè)將信息內(nèi)容轉(zhuǎn)碼的這句是不需要的,這個(gè)導(dǎo)出excel的代碼也是我百度的,但是測(cè)試的時(shí)候,導(dǎo)出的內(nèi)容總是有部分的漢字是??的格式,找問(wèn)題測(cè)試了半天發(fā)現(xiàn)其實(shí)這個(gè)內(nèi)容是不需要轉(zhuǎn)碼的,直接導(dǎo)出就不會(huì)出現(xiàn)亂碼的格式了;據(jù)大神給我說(shuō)的是看編碼,有的是需要轉(zhuǎn)碼的有的是不需要轉(zhuǎn)碼的)
$this->body .= "{$bodyVal}\t ";
}
$this->body .= "\n";
}
}
//設(shè)置header頭部信息和導(dǎo)出到excel內(nèi)容,并輸出到瀏覽器
public function downLoad($filename=''){
if(!$filename)
$filename = date('YmdHis',time()).'.xls';
ob_end_clean();
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;
}
//轉(zhuǎn)碼,這里不用iconv函數(shù),有可能會(huì)與gd沖突導(dǎo)致輸出空白。用
public function charset($string){
return mb_convert_encoding($string,'GBK','auto');
}
}
$excel = new Excel();
$excel->addHeader(array('id','用戶類型','用戶帳號(hào)','密碼','用戶昵稱','性別','帳號(hào)有效期','級(jí)別','email','積分','添加時(shí)間',
'登錄時(shí)間','登錄IP'));
global $dsql;
$sql="select `mid`,`mtype`,`userid`,`pwd`,`uname`,`sex`,`exptime`,`rank`,`email`,`scores`,`jointime`,`logintime`,`loginip` from `dede_member`";
$dsql->SetQuery($sql);
$dsql->Execute();
while($row = $dsql->GetArray()){
//將添加時(shí)間和登錄時(shí)間轉(zhuǎn)化為2017 16:30 的格式,這樣在表格中更容易讓人懂,不這樣操作的話那么顯示出來(lái)的是E
foreach($row as $key=>$val){
if($key=='jointime' || $key=='logintime'){
$row[$key]=date("Y-m-d H:i:s",$val);
}
}
$list[]=$row;
}
unset($row);
$excel->addBody($list);
$excel->downLoad();
?>