dede织梦后台增加php导出到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 .= ”

  ”;

  }

  }

  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文件,

  找到 删除属性 

  在后面加一段代码

  导出到excel

  ”; ?>

☉免责声明:本站所有模板均来自用户分享和网络收集,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服,我们核实后会立即删除。
☉本站提供的源码、模板、软件工具等其他资源,都不包含技术服务,请大家谅解!
pbootcms模板网 » dede织梦后台增加php导出到excel功能不出现乱码

Pbootcms模板网 提供优质的模板集合

立即查看 了解详情