简单的php导出excel

PHPExcel类挺强大的,但是性能有些慢,简单的导出没有必要了,写一个简单导出excel。

                   ob_end_clean();
			header("Content-type:application/octet-stream"); 
			header("Accept-Ranges:bytes"); 
			header("Content-type:application/vnd.ms-excel");   
			header("Content-Disposition:attachment;filename=fee_search_".date("Y-m-d").".xls");
			header("Pragma: no-cache");
			header("Expires: 0");
			
			$date_strat = strtotime($_REQUEST['listen_time']);
			$date_end = $date_strat+86400;
			if($date_strat!=''){
              $str = "AND lws_createtime Between ".$date_strat." AND ".$date_end."";
			}else{
			  $str = "";
			}
            $state   = $_REQUEST['state'];
			$stutype = $_REQUEST['stutype'];
			if($state==''){
			   $state = 0;
			}
			if($stutype!=''&&$stutype!='undefined'){
			  $stu = " and lws_stutype='$stutype'"; 
			}
			//导出xls 开始
			$tag1=iconv("UTF-8", "GB2312", '姓名');
			$tag2=iconv("UTF-8", "GB2312", '英文名');
			$tag3=iconv("UTF-8", "GB2312", '邮箱');
			$tag4=iconv("UTF-8", "GB2312", '手机');
			$tag8=iconv("UTF-8", "GB2312", '手机归属地');
			$tag5=iconv("UTF-8", "GB2312", '注册时间');
			$tag6=iconv("UTF-8", "GB2312", '来源');
			$tag7=iconv("UTF-8", "GB2312", '状态');
			
			echo "<table border='1' cellspacing='0' cellpadding='0'>";
			echo "<tr bgcolor='#ccc'>";
			echo "<td width='100' align='center'>$tag1</td>";
			echo "<td width='100' align='center'>$tag2</td>";
			echo "<td width='200' align='center'>$tag3</td>";
			echo "<td width='150' align='center'>$tag4</td>";
			echo "<td width='100' align='center'>$tag8</td>";
			echo "<td width='200' align='center'>$tag5</td>";
			echo "<td width='150' align='center'>$tag6</td>";
			echo "<td width='100' align='center'>$tag7</td>";
			echo "</tr>";
			$fix    = C ('DB_PREFIX');
			
			$student = M("student");
			$find = $student->field("stu.lws_id,lws_nameen,SiteName,lws_area,lws_contactdate,  lws_contacttime,lws_namecn,lws_email,lws_phone,lws_status,lws_createtime  AS needtime,sc.lsc_id")->join("AS stu LEFT JOIN sitesource AS ss ON ss.ID = stu.lws_origin LEFT JOIN  ".$fix."studentcontact AS sc ON sc.lws_id=stu.lws_id")->where("lws_status = $state AND lws_belong = 0 AND lws_advisorid = '".$_SESSION['arr']['uid']."' $str $stu GROUP BY stu.lws_id")->findall();
			for($i=0;$i<count($find);$i++){
				$namecn      =   iconv("UTF-8", "GB2312",  $find[$i]['lws_namecn']);
				$nameen      =   iconv("UTF-8", "GB2312", $find[$i]['lws_nameen']);
				$email       =   iconv("UTF-8", "GB2312",  $find[$i]['lws_email']);
				$phone       =   iconv("UTF-8", "GB2312",  $find[$i]['lws_phone']);
				$area       =   iconv("UTF-8", "GB2312",  $find[$i]['lws_area']);
				$netime      =   iconv("UTF-8", "GB2312",  date("y-m-d H:i",$find[$i]['needtime']));
				$site        =   iconv("UTF-8", "GB2312",  $find[$i]['SiteName']);
				if($find[$i]['lsc_id']!=''){
					if($stutype!=''){
				      $tact        =   iconv("UTF-8", "GB2312",  '有效待联系');
				    }else{
				      $tact        =   iconv("UTF-8", "GB2312",  '联络沟通问题');
				    }
				}else{
				      $tact        =   iconv("UTF-8", "GB2312",  '新量');
				}
				
				echo "<tr>";
				echo "<td width='100' align='center'>$namecn</td>";
				echo "<td width='100' align='center'>$nameen</td>";
				echo "<td width='200' align='center'>$email</td>";
				echo "<td width='150' align='center'>$phone</td>";
				echo "<td width='100' align='center'>$area</td>";
				echo "<td width='200' align='center'>$netime</td>";			
				echo "<td width='150' align='center'>$tact</td>";			
				echo "<td width='100' align='center'>$site</td>";	
				echo "</tr>";
			}

还可以采用phpmyadmin 方法

<html xmlns:o="urn:schemas-microsoft-com:office:office" 
 xmlns:x="urn:schemas-microsoft-com:office:excel" 
 xmlns="http://www.w3.org/TR/REC-html40"> 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 <html> 
     <head> 
        <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
         <style id="Classeur1_16681_Styles"></style> 
     </head> 
     <body> 
         <div id="Classeur1_16681" align=center x:publishsource="Excel"> 
             <table x:str border=0 cellpadding=0 cellspacing=0 width=100% style="border-collapse: collapse"> 
                 <tr><td class=xl2216681 nowrap>1234</td><td class=xl2216681 nowrap>Robbin会吐口水</td></tr> 
                <tr><td class=xl2216681 nowrap>5678</td><td class=xl2216681 nowrap>javaeye网站</td></tr> 
             </table> 
         </div> 
     </body> 
 </html> 
这下可以直接echo了,又不需要iconv转码,只要设置好HTML里的Content-type(这里用的是UTF-8),是不是有舒服的感觉呢? 当然header还是要加上

header("Content-type:application/vnd.ms-excel"); 
 header("Content-Disposition:attachment;filename=export_data.xls"); 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值