加入收藏 | 设为首页 | 会员中心 | 我要投稿 鹤壁站长网 (https://www.0392zz.cn/)- 分布式云、存储数据、视频终端、媒体处理、内容创作!
当前位置: 首页 > 教程 > 正文

dede织梦后台增加导出内容到excel功能方法 不会乱码

发布时间:2022-09-02 10:07:10 所属栏目:教程 来源:互联网
导读:dede织梦系统怎样导出后台的文章或自定义模型中的数据到excel,并且不出现乱码 在后台目录创建一个php文件toexcel.php,在最上面加入代码; 1 require_once(dirname(__FILE__)./config.php); 2 3 require_once(DEDEINC./typelink.class.php); 4 5 require_once(
  dede织梦系统怎样导出后台的文章或自定义模型中的数据到excel,并且不出现乱码
 
  在后台目录创建一个php文件toexcel.php,在最上面加入代码;
 
  1
 
  require_once(dirname(__FILE__).'/config.php');
 
  2
 
   
 
  3
 
  require_once(DEDEINC.'/typelink.class.php');
 
  4
 
   
 
  5
 
  require_once(DEDEINC.'/datalistcp.class.php');
 
  6
 
   
 
  7
 
  require_once(DEDEADMIN.'/inc/inc_list_functions.php');加入导出到excel类;
 
  01
 
  class Excel
 
  02
 
   
 
  03
 
  {   
 
  04
 
   
 
  05
 
    private $head;    
 
  06
 
   
 
  07
 
  private $body;
 
  08
 
   
 
  09
 
    public function addHeader($arr){       
 
  10
 
   
 
  11
 
    foreach($arr as $headVal){           
 
  12
 
   
 
  13
 
  $headVal = $this->charset($headVal);            
 
  14
 
   
 
  15
 
  $this->head .= "{$headVal}t ";       
 
  16
 
   
 
  17
 
  }        
 
  18
 
   
 
  19
 
  $this->head .= "n";    
 
  20
 
   
 
  21
 
  }
 
  22
 
   
 
  23
 
    public function addBody($arr){        
 
  24
 
   
 
  25
 
    foreach($arr as $arrBody){            
 
  26
 
   
 
  27
 
  foreach($arrBody as $bodyVal){                
 
  28
 
   
 
  29
 
  $bodyVal = $this->charset($bodyVal);                
 
  30
 
   
 
  31
 
  $this->body .= "{$bodyVal}t ";            
 
  32
 
   
 
  33
 
  }            
 
  34
 
   
 
  35
 
  $this->body .= "n";       
 
  36
 
   
 
  37
 
  }    
 
  38
 
   
 
  39
 
  }
 
  40
 
   
 
  41
 
    public function downLoad($filename=''){        
 
  42
 
   
 
  43
 
    if(!$filename)            
 
  44
 
   
 
  45
 
    $filename = date('YmdHis',time()).'.xls';        
 
  46
 
   
 
  47
 
    header("Content-type:application/vnd.ms-excel");        
 
  48
 
   
 
  49
 
    header("Content-Disposition:attachment;filename=$filename");         
 
  50
 
   
 
  51
 
    header("Content-Type:charset=gb2312");        
 
  52
 
   
 
  53
 
    if($this->head)           
 
  54
 
   
 
  55
 
    echo $this->head;        
 
  56
 
   
 
  57
 
      echo $this->body;    
 
  58
 
   
 
  59
 
  }
 
  60
 
   
 
  61
 
     public function charset($string){        
 
  62
 
   
 
  63
 
  return mb_convert_encoding($string,'GBK','auto');   
 
  64
 
   
 
  65
 
     }
 
  66
 
   
 
  67
 
  }代码解释:
 
  1.输出列名数组,并转码
 
  01
 
  public function addHeader($arr){       
 
  02
 
   
 
  03
 
    foreach($arr as $headVal){           
 
  04
 
   
 
  05
 
  $headVal = $this->charset($headVal);            
 
  06
 
   
 
  07
 
  $this->head .= "{$headVal}t ";       
 
  08
 
   
 
  09
 
  }        
 
  10
 
   
 
  11
 
  $this->head .= "n";    
 
  12
 
   
 
  13
 
  }2.输出导出内容数组,并转码
 
  01
 
  public function addBody($arr){        
 
  02
 
   
 
  03
 
    foreach($arr as $arrBody){            
 
  04
 
   
 
  05
 
  foreach($arrBody as $bodyVal){                
 
  06
 
   
 
  07
 
  $bodyVal = $this->charset($bodyVal);                
 
  08
 
   
 
  09
 
  $this->body .= "{$bodyVal}t ";            
 
  10
 
   
 
  11
 
  }            
 
  12
 
   
 
  13
 
  $this->body .= "n";       
 
  14
 
   
 
  15
 
  }    
 
  16
 
   
 
  17
 
  }3.设置header头部信息和导出到excel内容,并输出到浏览器
 
  01
 
  public function downLoad($filename=''){        
 
  02
 
   
 
  03
 
    if(!$filename)            
 
  04
 
   
 
  05
 
    $filename = date('YmdHis',time()).'.xls';        
 
  06
 
   
 
  07
 
    header("Content-type:application/vnd.ms-excel");        
 
  08
 
   
 
  09
 
    header("Content-Disposition:attachment;filename=$filename");         
 
  10
 
   
 
  11
 
    header("Content-Type:charset=gb2312");        
 
  12
 
   
 
  13
 
    if($this->head)           
 
  14
 
   
 
  15
 
    echo $this->head;        
 
  16
 
   
 
  17
 
      echo $this->body;    
 
  18
 
   
 
  19
 
  }4.转码,这里不用iconv函数,有可能会与gd冲突导致输出空白。用
 
  1
 
  public function charset($string){        
 
  2
 
   
 
  3
 
  return mb_convert_encoding($string,'GBK','auto');   
 
  4
 
   
 
  5
 
     }7.调用方法;
 
  01
 
  $excel = new Excel();
 
  02
 
   
 
  03
 
  $excel->addHeader(array('列一','列二','列三','列四'));
 
  04
 
   
 
  05
 
  global $dsql;
 
  06
 
   
 
  07
 
  $sql="select 列一字段,列二字段,列三字段,列四字段 from 表名";
 
  08
 
   
 
  09
 
  $dsql->SetQuery($sql);
 
  10
 
   
 
  11
 
  $dsql->Execute();
 
  12
 
   
 
  13
 
  while($row = $dsql->GetArray()){
 
  14
 
   
 
  15
 
  $list[]=$row;
 
  16
 
   
 
  17
 
  }
 
  18
 
   
 
  19
 
  unset($row);
 
  20
 
   
 
  21
 
  $excel->addBody($list);
 
  22
 
   
 
  23
 
  $excel->downLoad();后台添加导出到excel代码:
 
  找到后台目录下的templets目录,下面有个content_list.htm文件,
 
  找到<a href="javascript:;" onClick="cAtts('attsDel',event,this)" class="coolbg"> 删除属性 </a>
 
  在后面加一段代码
 
  <?php if($channelid==1) echo " <a href="toexcel.php" class="coolbg" target="_blank">导出到excel</a>rn"; ?>
 
  $channelid就是你的模型id,根据你导出的表填写。填写完之后打开后台栏目列表就出现导出按钮
 
  注意事项
 
  转码问题,根据自己的实际情况
 
  导出字段,多表或自定义模型的表可以通过left join
 

(编辑:鹤壁站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读