将asp查询出来的数据导出excel格式,怎么实现呀,在网上找了大堆的代码做了,还是不行?希望大家帮忙解决。谢了。
保存为excel格式:
<%
if request.cookies("flag")< 1 then
response.redirect "error.htm"
response.End
end if
%> '这是我设置的权限,你可以删掉
<!--#include file="conn.asp"--> '数据库连接文件
<%
Response.ContentType = "application/vnd.ms-excel"
filename="CHINACOAT_NAME" '要保存的excel表格名
Response.AddHeader "content-disposition", "attachment; filename=" & filename & ".xls"
%>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> '下面是设置的表格,里面是数据库对应内容
<table width="90%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Exh
name<br>
</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Co.
Name in Eng</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Co.
Name in Chinese</strong></font></div></td>
<td bgcolor="#FFFFFF"><div align="center"><strong>Chinese (Eng) Name</strong></div></td>
<%
set rs= server.CreateObject("adodb.recordset")
rs.open " select * from detail order by shortname asc",myconn,1,1 '查询语句
%>
<td> <div align="center"><font color="#FFFFFF"><strong><font color="#000000">Origin</font><br>
</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Origin
(Chinese)</strong></font><font color="#FFFFFF"></font></div></td>
<td bgcolor="#FFFFFF"><div align="center"><strong>Follow up office</strong></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Rep
by<br>
</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>NEW<br>
</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Hall</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Zone
</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Booth
No.</strong></font><font color="#FFFFFF"></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Profile(chi)<br>
</strong></font></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><font color="#000000"><strong>Profile</strong></font></div></td>
<td bgcolor="#FFFFFF"><div align="center"><strong>Add people</strong></div></td>
<td bgcolor="#FFFFFF"><div align="center"><strong>Add date</strong></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><strong><font color="#000000">update
people </font></strong></div></td>
<td bgcolor="#FFFFFF"> <div align="center"><strong><font color="#000000">update
date </font></strong></div></td>
</tr>
<%do while not rs.eof%>
<tr>
<td> <div align="center"><%=rs("shortname")%></div></td>
<td><div align="center"><%=rs("companyeng")%> </div></td>
<td><%=rs("companychi")%> </td>
<td><%=rs("companychieng")%></td>
<td><div align="center"><%=rs("country")%> </div></td>
<td><%=rs("countrychi")%></td>
<td><%=rs("followup")%></td>
<td><div align="center"><%=rs("representby")%> </div></td>
<td> <div align="center"> <%=rs("new1") %> </div></td>
<td><div align="center"><%=rs("hall")%> </div></td>
<td> <div align="center"><%=rs("zone1")%></div></td>
<td> <div align="center"><%=rs("boothnumber")%></div></td>
<td> <div align="center">
<%if rs("instrchi") <>"" then response.write "Yes" end if%>
</div></td>
<td>
<%if rs("instreng") <>"" then response.write "Yes" end if%>
</td>
<td><%=rs("addman")%></td>
<td><%=year(rs("timeaddman"))&"-"&month(rs("timeaddman"))&"-"&day(rs("timeaddman"))%></td>
<td>
<%if rs("flag")>2 then%>
<%=rs("admin")%>
<%end if%>
</td>
<td>
<%if rs("flag")>2 then%>
<%=year(rs("time2"))&"-"&month(rs("time2"))&"-"&day(rs("time2"))%>
<%end if%>
</td>
</tr>
<%rs.movenext
loop
rs.close
set rs=nothing
%>
</table>
再给你发个保存为word文档的:
<%
Response.ContentType = "application/vnd.ms-word"
filename="CHINACOAT_SELECT"
Response.AddHeader "content-disposition", "attachment; filename=" & filename & ".doc"
%>
<%
if request.cookies("flag")< 1 then
response.redirect "error.htm"
response.End
end if
%>
<!--#include file="conn.asp"-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<%
if request.cookies("flag")< 1 then
response.redirect "error.htm"
response.End
end if
%>
<p>
<!--#include file="conn.asp"-->
<%
dim a1,b1,c1,d,e,f,g,h,i,j,k,l,m,n,o,p
a1=request.Form("int1") '展台号
b1=request.Form("int2") '新展商
c1=request.Form("int3") 'reprenstby
d=request.Form("int4") '公司名
e=request.Form("int5") '公司名中文
f=request.Form("int6") '地址
o=request.Form("int14") '中文地址
p=request.Form("int15") '附加资料
g=request.Form("int7") '邮遍
h=request.Form("int8") '电话
i=request.Form("int9") '传真
j=request.Form("int10") '电邮
k=request.Form("int11") '网址
l=request.Form("int12") '公司简介英文
m=request.Form("int13") '公司简介中文
%>
<%
dim a,b,c
a=request.Form("user")
b=request.Form("zone")
if a="All" then
a=""
end if
c=request.Form("office")
if c="All" then
c=""
end if
dim sql
yy=""
yy1=""
if a<>"" then
yy="admin="&a
end if
if c<>"" then
yy1=" followup="&c
end if
sql="select * from detail where admin='"&a&"' and zone1='3' or zone1='4' and followup='"&c&"' order by shortname asc "
set rs=server.CreateObject("adodb.recordset")
rs.Open sql,myconn,1,1
do while not rs.eof
%>
</p>
<p><%if a1<>"" then%>展台号:<%=rs("boothnumber")%><br><%end if%>
<%if b1<>"" then%><%=rs("new1")%><br><%end if%>
<%if c1<>"" then%><%=rs("representby")%><br><%end if%>
<%if d<>"" then%><%=rs("companyeng")%><br><%end if%>
<%if e<>"" then%><%=rs("companychi")%><br><%end if%>
<%if f<>"" then%><%=rs("address")%><br><%end if%>
<%if o<>"" then%><%=rs("addresschi")%><br><%end if%>
<%if g<>"" then%>邮编:<%=rs("post")%><br><%end if%>
<%if h<>"" then%>电话:<%=rs("phone")%><br><%end if%>
<%if i<>"" then%>传真:<%=rs("fax")%><br><%end if%>
<%if j<>"" then%>电邮:<%=rs("email")%><br><%end if%>
<%if k<>"" then%>网址:<%=rs("website")%><br><br><%end if%>
<%if p<>"" then%><%=rs("attached")%><br><br><%end if%>
<%if l<>"" then%><%=rs("instreng")%><br><br><%end if%>
<%if m<>"" then%><%=rs("instrchi")%><%end if%></p>
<%
rs.movenext
loop
rs.close
set rs=nothing
myconn.close
set myconn=nothing
%>
</body>
</html>
上面的代码都是我用过的,绝对可行,你修改试下
[此贴子已经被阳光白雪于2007-6-27 17:24:06编辑过]