织梦dede自定义表单后台增加php导出到excel功能
首先在网站plus目录创建excel.php文件,并加入如下代码
<?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}\t ";
}
$this->head .= "\n";
}//输出导出内容数组,并转码
public function addBody($arr){
foreach($arr as $arrBody){
foreach($arrBody as $bodyVal){
$bodyVal = $this->charset($bodyVal);
$this->body .= "{$bodyVal}\t ";
}
$this->body .= "\n";
}
}
//设置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;
}
//转码,这里不用iconv函数,有可能会与gd冲突导致输出空白。
public function charset($string){
return mb_convert_encoding($string,'GBK','auto');
}
}
//调用方法
$excel = new Excel();
$excel->addHeader(array('姓名','电话','QQ','项目类型','留言内容','访问域名','来源页面','留言时间'));
global $dsql;
$sql="select name,tel,qq,leixing,lynr,domain,laiyuan,time from yudu_diyform1";
$dsql->SetQuery($sql);
$dsql->Execute();
while($row = $dsql->GetArray()){
$list[]=$row;
}
unset($row);
$excel->addBody($list);
$excel->downLoad();?>
然后找到自定义表单模板diy_list.htm,在里面添加
<?php if($diyid==1) echo " <a href=\"excel.php\" target=\"_blank\">导出到excel</a>\r\n"; ?>