首页 DedeCms正文

dede织梦后台增加php导出到excel功能不出现乱码

Admin DedeCms 2020-06-29 17:06:10 26
dede织梦系统怎样导出后台的文章或自定义模型中的数据到excel,并且不出现乱码

在后台目录创建一个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} ";

}

$this->head .= " ";

}

public function addBody($arr){

foreach($arr as $arrBody){

foreach($arrBody as $bodyVal){

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

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

}

$this->body .= " ";

}

}

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.输出列名数组,并转码

public function addHeader($arr){

foreach($arr as $headVal){

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

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

}

$this->head .= " ";

}

2.输出导出内容数组,并转码

public function addBody($arr){

foreach($arr as $arrBody){

foreach($arrBody as $bodyVal){

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

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

}

$this->body .= " ";

}

}

Copyright ©2019-2020.Powered by©编程脚本学习网 苏ICP备17028197号