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

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 .= " ";        

}     

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/07085d44ab86f8cd48910af87a02d114.html