dede织梦系统怎样导出后台的文章或自定义模型中的数据到excel,并且不出现乱码
在后台目录创建一个php文件toexcel.php,在最上面加入代码;
1 |
require_once(dirname(__FILE__).'/config.php'); |
2 |
require_once(DEDEINC.'/typelink.class.php'); |
3 |
require_once(DEDEINC.'/datalistcp.class.php'); |
4 |
require_once(DEDEADMIN.'/inc/inc_list_functions.php'); |
|
加入导出到excel类;
05 |
public function addHeader($arr){ |
06 |
foreach($arr as $headVal){ |
07 |
$headVal = $this->charset($headVal); |
08 |
$this->head .= "{$headVal}t "; |
12 |
public function addBody($arr){ |
13 |
foreach($arr as $arrBody){ |
14 |
foreach($arrBody as $bodyVal){ |
15 |
$bodyVal = $this->charset($bodyVal); |
16 |
$this->body .= "{$bodyVal}t "; |
21 |
public function downLoad($filename=''){ |
23 |
$filename = date('YmdHis',time()).'.xls'; |
24 |
header("Content-type:application/vnd.ms-excel"); |
25 |
header("Content-Disposition:attachment;filename=$filename"); |
26 |
header("Content-Type:charset=gb2312"); |
31 |
public function charset($string){ |
32 |
return mb_convert_encoding($string,'GBK','auto'); |
|
代码解释:
1.输出列名数组,并转码
1 |
public function addHeader($arr){ |
2 |
foreach($arr as $headVal){ |
3 |
$headVal = $this->charset($headVal); |
4 |
$this->head .= "{$headVal}t "; |
|
2.输出导出内容数组,并转码
1 |
public function addBody($arr){ |
2 |
foreach($arr as $arrBody){ |
3 |
foreach($arrBody as $bodyVal){ |
4 |
$bodyVal = $this->charset($bodyVal); |
5 |
$this->body .= "{$bodyVal}t "; |
|
3.设置header头部信息和导出到excel内容,并输出到浏览器
01 |
public function downLoad($filename=''){ |
03 |
$filename = date('YmdHis',time()).'.xls'; |
04 |
header("Content-type:application/vnd.ms-excel"); |
05 |
header("Content-Disposition:attachment;filename=$filename"); |
06 |
header("Content-Type:charset=gb2312"); |
|
4.转码,这里不用iconv函数,有可能会与gd冲突导致输出空白。用
1 |
public function charset($string){ |
2 |
return mb_convert_encoding($string,'GBK','auto'); |
|
5.调用方法;
02 |
$excel->addHeader(array('列一','列二','列三','列四')); |
04 |
$sql="select 列一字段,列二字段,列三字段,列四字段 from 表名"; |
05 |
$dsql->SetQuery($sql); |
07 |
while($row = $dsql->GetArray()){ |
11 |
$excel->addBody($list); |
|
后天添加导出到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