将指定的记录生成excel文件
将指定的记录生成excel文件
<%
DB="db1.mdb"
path=Server.MapPath(DB)
set conn=server.createobject("adodb.Connection")
connstr="provider=Microsoft.Jet.OLEDB.4.0;Data Source="&path
conn.Open connstr
Set rs=server.CreateObject("adodb.recordset")
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style>
td{font-size:12px;}
</style>
</HEAD>
<script>
function CheckAll(a)
{
o=document.getElementsByName(a)
for(i=0;i<o.length;i++)
o[i].checked=event.srcElement.checked
}
function fsubmit()
{
if(!f2("要生成excel","确定要将这些记录生成excel文件吗?","记录")) return false;
}
function f2(text,t1,name)
{
var dnum=document.getElementsByName("id").length;
j=0;
if (dnum==1)
{
if(document.form1.id.checked==true)
{
j++;
}
}
else
{
for (i=0;i<dnum;i++)
{
if(document.form1.id[i].checked==true)
{
j++;
}
}
}
if (j==0)
{
alert('请选择你要'+text+'的'+name);
return false;
}
else
{
if(!confirm(t1)) return false;
}
return true;
}
</script>
<BODY>
<TABLE border="1" width="600" cellpadding=0 cellspacing=0>
<form name="form1" method="post" action="1.asp教程" onsubmit="return fsubmit();">
<TR>
<TD width="5%" height="25" align="center"><input type="checkbox" name="checkbox" value="checkbox" onclick="CheckAll('id')"></TD>
<TD height="25" align="center">用户名</TD>
<TD height="25" align="center">密码</TD>
<TD width="40%" height="25" align="center">内容</TD>
<TD align="center">性别</TD>
<TD align="center">发布时间</TD>
</TR>
<%
sql="select top 10 id,username,password,content,易做图,ytime from users"
rs.open sql,conn,1,1
If rs.eof Then
Else
do while not rs.eof
id = rs("id")
username = rs("username")
password = rs("password")
content = rs("content")
易做图 = rs("易做图")
ytime = rs("ytime")
'******************下面是你要显示的******************'
%>
<TR>
<TD height="20" align="center"><input type="checkbox" name="id" value="<%=id%>"></TD>
<TD height="20"><%=username%></TD>
<TD height="20"><%=password%></TD>
<TD height="20"><%=content%></TD>
<TD height="20" align="center"><%=易做图%></TD>
<TD height="20" align="left"> <%=ytime%></TD>
</TR>
<%
'******************上面是你要显示的******************'
rs.movenext
loop
End If
rs.close
%>
<TR>
<TD height="25" colspan="6">
<input type="submit" name="Submit" value="生成excel文件"></TD>
</TR>
</form>
</TABLE>
</BODY>
</HTML>
1.asp文件
<!--#include file="conn.asp"-->
<%
'功能:将指定的记录生成excel文件
'作者:wangsdong
id=request("id")
set Conn =server.CreateObject("adodb.connection")
ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
conn.open ConnStr
dim rs,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
filename = Server.MapPath("zzzyk.com教程.xls")
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
set myfile = fs.CreateTextFile(filename,true)
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select id,username,password,content,易做图,ytime from users where id in("&id&")"
rs.Open sql,conn,1,1
if rs.EOF and rs.BOF then
else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine = strLine & x.name & chr(9)
Next
myfile.writeline strLine
Do while Not rs.EOF
strLine=""
for each x in rs.Fields
strLine = strLine & x.value & chr(9)
next
myfile.writeline strLine
rs.MoveNext
loop
end if
rs.Close
set rs = nothing
Response.Write "生成EXCEL文件成功!下载:<input type=""button"" value=""点击下载"" onclick=""javascript教程:window.open('aspprogra.xls')"">"
%>
补充:asp教程,高级应用