• 全国400电话网上服务平台
    强大号码库资源任选,方便,快捷,快速开通。
    咨询热线:400-1100-266  

    dede织梦后台增加导出内容到excel功能方法 不会乱码
    POST TIME:2021-05-23 02: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}\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.输出列名数组,并转码

    public function addHeader($arr){        
    
      foreach($arr as $headVal){            
    
    $headVal = $this->charset($headVal);             
    
    $this->head .= "{$headVal}\t ";        
    
    }         
    
    $this->head .= "\n";     
    
    } 

     

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

    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内容,并输出到浏览器

    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函数,有可能会与gd冲突导致输出空白。用

     

    public function charset($string){         
    
    return mb_convert_encoding($string,'GBK','auto');    
    
       } 

     

    7.调用方法;

    $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,根据你导出的表填写。填写完之后打开后台栏目列表就出现导出按钮

    注意事项

    转码问题,根据自己的实际情况

    导出字段,多表或自定义模型的表可以通过left join

    
    关于我们 | 付款方式 | 建站知识 | 增值服务 | 网站模板
    Copyright © 2003-2016
    时间:9:00-21:00 (节假日不休)
    版权所有:巨人网络(扬州)科技有限公司
    总部地址:江苏省信息产业基地11号楼四层
    《增值电信业务经营许可证》 苏B2-20120278
    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信

    微信号已复制,请打开微信添加咨询详情!