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

织梦教程 强烈建议 2024-02-26 11:29 73 0

  在后台目录创建一个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 .= " ";

  }

  }

本站资源均来自互联网或会员发布,如果不小心侵犯了您的权益请与我们联系。我们将立即删除!谢谢!