<%@Language=VBScript%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title>导出SQLServer数据到Excel中</title>
</head>
<body>
<%
Response.Clear
Response.ContentType="text/xls"
Response.AddHeader"content-disposition","attachment;filename=Excel.xls"
'生成Excel
Response.Write"编号"&Chr(9)&"姓名"&Chr(9)&"性别"&Chr(9)&"年龄"&Chr(13)
SetConn=Server.CreateObject("Adodb.Connection")
ConnStr="Provider=SQLOLEDB.1;PersistSecurityInfo=True;UserID=sa;Password=;InitialCatalog=Info;DataSource=127.0.0.1"
Conn.OpenConnStr
Sql="SELECT*FROMINFO"
SetRs=Conn.Execute(Sql)
Num=Rs.Fields.Count
DoWhileNotRs.Eof
MM=0
WhileMM<CInt(Num)
Data=Data&Rs(MM)&Chr(9)
MM=MM+1
Wend
Response.WriteData&Chr(13)
Data=""
Rs.MoveNext:Loop
SetRs=Nothing
Conn.Close
SetConn=Nothing
Response.Flush
Response.End
%>
</body>
</html>
本文介绍了一种使用VBScript将SQL Server数据库中的数据导出到Excel文件的方法。通过设置HTTP响应头来指定文件类型及名称,并利用ADO组件执行SQL查询,遍历结果集并逐行写入到Excel中。
1509

被折叠的 条评论
为什么被折叠?



