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

}     


 

免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。


来源:网友投稿 关注: 时间:2020-06-28 09:04
首先声明,只要是我们的vip会员所有源码均可以免费下载,不做任何限制(了解更多)
☉本站的源码不会像其它下载站一样植入大量的广告。为了更好的用户体验以后坚持不打水印
☉本站只提供精品织梦源码,源码在于可用,不在多!!希望在这里找到你合适的。
☉本站提供的整站织梦程序,均带数据及演示地址。可以在任一源码详情页查看演示地址
☉本站所有资源(包括源码、模板、素材、特效等)仅供学习与参考,请勿用于商业用途。
☉如有其他问题,请加网站客服QQ进行交流。
相关织梦教程