如何将搜索结果输出至excel文件

如何将搜索结果输出至excel文件

现在已经将sql的条件全部设置好了,如何使用php语句将mysql中指定表的搜索结果自动生成excel文件(最好文件保存位置能有用户指定,想下面的asp文件的功能)。越简单越好。
下面是我一个asp格式的输出设置,由于刚学php,不是很熟悉。看大家能否帮忙转换成php语句,谢谢

<!--#include file="conn.asp"-->
<!--#include file="check.asp"-->
<%
'打开数据库
sql=Trim(Request.Form("getsql"))
pagelist=Request.Cookies("listok")
if sql="" then
sql=Trim(Request("getsql"))
end if
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1  
if rs.eof and rs.bof then
Response.Write("No data!")
else
%>
<html>
<head>
<TITLE>数据库记录到表格</TITLE>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="../INC/skin2.css">
<!--导出到Excel代码-->
<%response.ContentType ="application/vnd.ms-excel"%>

<body bgcolor="#FFFFFF" onLoad="AutomateExcel();">

<table border="1" align="center" cellpadding="0"  cellspacing="0" bordercolor="#000000" id="data" style="border-collapse: collapse; border: 0px dotted #0000FF; ">
<tr>

<%for each whatever in rs.fields%>
       <td><b><%=whatever.name%></B></TD>
<% next %>
</tr>

<%DO  UNTIL rs.eof %>
   <tr>
   <% for each whatever in rs.fields
      thisfield=whatever.value
      if isnull(thisfield) then
         thisfield=shownull
' 读取全部记录'
      end if
      if trim(thisfield)="" then
         thisfield="N/a"
      end if%>
             <td valign=top class="alt"><%=thisfield%></td>
<% next %>
   </tr>
   <%rs.movenext
loop%>
   
</table>

</body>
</html>
<%
end if
rs.close
set rs=nothing  
rsadm.close
set rsadm=nothing   
conn.close
set conn=nothing %>
毕业了。。。

求自动列出表中数据的代码