如何用asp把sql server数据转化为execl文件?
1、ASP文件:
<%@ LANGUAGE="VBSCRIPT" %>
<%option explicit%>
<%
'EXAMPLE AS:把数据库中一个每天24小时在线人数放到一个Excel文件中去
'AUTHOR :钢铁工人
'EMAIL :hello_hhb@21cn.com
'DATE :2001-3-25
'TEST :在NT4,SP6,SQL SERVER 7.0,Excel2000中测试通过
%>
<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
else
dim conn
set conn=server.CreateObject("adodb.connection")
conn.Open "test","sa",""
'conn.Open Application("connstr")
dim rs,sql,filename,fs,myfile,x,link
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的Excel文件做如下的存放
filename = "c:\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 population,hourpos,datepos from populationperhour order by datepos,hourpos asc"
rs.Open sql,conn
if rs.EOF and rs.BOF then
else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine= strLine