欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

如何用asp把sqlserver数据转换为execl文件

程序员文章站 2022-06-05 11:16:07
...


\\\'EXAMPLE AS:把数据库中一个每天24小时在线人数放到一个EXCEL文件中去
\\\'AUTHOR :钢铁工人
\\\'EMAIL :hello_hhb@21cn.com
\\\'DATE :2001-3-25
\\\'TEST :在NT4,SP6,SQL SERVER 7.0,EXCEL2000中测试通过
%>



生成EXCEL文件


生成在线人口的EXCEL


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 & 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
conn.close
set conn = nothing
set myfile = nothing
Set fs=Nothing

link="Open The Excel File"
Response.write link
end if
%>




2、数据库相关:

CREATE TABLE [populationperhour] (
[population] [int] NOT NULL ,
[hourpos] [int] NOT NULL ,
[datepos] [datetime] NOT NULL
);
insert into populationperhour values(\\\'936\\\',\\\'1\\\',\\\'2001-1-11\\\');
insert into populationperhour values(\\\'636\\\',\\\'2\\\',\\\'2001-1-11\\\');
...