Blogger Information
Blog 48
fans 2
comment 3
visits 37437
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
织梦自定义表单后台增加php导出到excel功能
黑猫警长的博客
Original
692 people have browsed it

在后台目录创建一个php文件toexcel.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=\"toexcel.php\" target=\"_blank\">导出到excel</a>\r\n"; ?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!