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

}

}

  • 共2页:
  • 上一页
  • 1
  • 2
  • 下一页

免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。