Asp教程

通过asp实例结合结合ACCESS,MSSQL来更好的深入Asp学习 - 爬坡者

« 查询指定记录数 asp调试 参数不足,期待是 1 的原因及办法 »

Asp生成Excel文件示例

Asp生成Excel文件示例
代码如下:

<HTML>
<HEAD>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type"> 
<TITLE>生成EXCEL文件</TITLE> 
</HEAD> 
<body> 
<a href="dbtoexcel.asp?act=make">在线生成EXCEL</a> 
<hr size=1 align=left width=300px> 
<%
if Request("act") = "" then 
Response.Write "生成EXCEL文件" 
else
dim conn,strconn 
strconn="driver={SQL Server};server=wen;uid=sa;pwd=;database=DB_Test" 
set conn=server.CreateObject("adodb.connection") 
conn.Open strconn
dim rs,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject") 
'--假设你想让生成的EXCEL文件做如下的存放 
filename = Server.MapPath("online.xls") 
'--如果原来的EXCEL文件存在的话删除它 
if fs.FileExists(filename) then
fs.DeleteFile(filename) 
end if
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据查出来 
sql = "select * from Tb_Execl order by id desc" 
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
'--将表的列名先写入EXCEL 
myfile.writeline strLine
Do while Not rs.EOF 
strLine=""
for each x in rs.Fields 
strLine = strLine & x.value & chr(9) 
next
'--将表的数据写入EXCEL 
myfile.writeline strLine
rs.MoveNext
loop
end if
rs.Close 
set rs = nothing
End If
%>

Asp生成Excel文件示例

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80710

Copyright 2007-2008 papozhe.com [asp教程] All Rights Reserved.
浙ICP备07030537号
免责申明:所有文章除特别声明,均来自网上,主要为学习用!内容仅供参考,版权归原作者。如侵犯您利益,请来信告知.
Email:papozhe$Gmail.com QQ:76336503