濮阳杆衣贸易有限公司

主頁 > 網(wǎng)站建設 > 建站知識 > dede織夢后臺增加php導出到excel功能不出現(xiàn)亂碼

dede織夢后臺增加php導出到excel功能不出現(xiàn)亂碼

POST TIME:2017-11-12 23:54

dede織夢系統(tǒng)怎樣導出后臺的文章或自定義模型中的數(shù)據(jù)到excel,并且不出現(xiàn)亂碼

在后臺目錄創(chuàng)建一個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');

加入導出到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ù)組,并轉碼

public function addHeader($arr){

foreach($arr as $headVal){

$headVal = $this->charset($headVal);

$this->head .= "{$headVal}\t ";

}

$this->head .= "\n";

}

2.輸出導出內(nèi)容數(shù)組,并轉碼

public function addBody($arr){

foreach($arr as $arrBody){

foreach($arrBody as $bodyVal){

$bodyVal = $this->charset($bodyVal);

$this->body .= "{$bodyVal}\t ";

}

$this->body .= "\n";

}

}

3.設置header頭部信息和導出到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.轉碼,這里不用iconv函數(shù),有可能會與gd沖突導致輸出空白。用

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();

后天添加導出到excel代碼:

找到后臺目錄下的templets目錄,下面有個content_list.htm文件,

找到<a href="javascript:;" onClick="cAtts('attsDel',event,this)" class="coolbg">&nbsp;刪除屬性&nbsp;</a>

在后面加一段代碼

<?php if($channelid==1) echo " <a href=\&;toexcel.php\&; class=\&;coolbg\&; target=\&;_blank\&;>導出到excel</a>\r\n"; ?>

$channelid就是你的模型id,根據(jù)你導出的表填寫。填寫完之后打開后臺欄目列表就出現(xiàn)導出按鈕




上一篇:Dedecms數(shù)據(jù)庫結構表名:dede_addonarticle

下一篇:織夢dedecms后臺登錄提示Call to undefined function get

收縮
  • 微信客服
  • 微信二維碼
  • 電話咨詢

  • 400-1100-266
左贡县| 广河县| 海门市| 融水| 桦南县| 宝丰县| 胶州市| 永春县| 缙云县| 章丘市| 忻州市| 陆良县| 塔河县| 长岛县| 吴川市| 宾川县| 乐昌市| 类乌齐县| 如东县| 白银市| 隆子县| 乌审旗| 阿克苏市| 灵丘县| 梁河县| 长葛市| 许昌市| 宁德市| 浙江省| 八宿县| 当雄县| 达日县| 长沙市| 光泽县| 乡宁县| 凤庆县| 历史| 晋城| 自贡市| 开原市| 福贡县|