栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > dedecms

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

dedecms 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

}     


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/4159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号