用ASP将数据库中的数据直接导出到EXCEL表中
asp实例代码,直接将中的数据导出到excel电子表中。
<!--#include file="../conn."-->
<%
dim s,sql,filename,fs,myfile,x
set fs = server.createobject("scripting.filesystemobject")
--假设你想让生成的excel文件做如下的存放
filename = server.mappath("users.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
starttime = request("starttime")
endtime = request("endtime")
startendtime = "addtime between #"& starttime &" 00:00:00# and #"& endtime &" 23:59:59#"
strsql = "select * from myeky_myusr "
set rstdata =conn.execute(strsql)
if not rstdata.eof and not rstdata.bof then
dim trline,responsestr
strline=""
for each x in rstdata.fields
strline = strline & x.name & chr(9)
next
--将表的列名先写入excel
myfile.writeline strline
do while not rstdata.eof
strline=""
for each x in rstdata.fields
strline = strline & x.value & chr(9)
next
myfile.writeline strline
rstdata.movenext
loop
end if
response.write "生成excel文件成功,点击<a href=""users.xls"" target=""_blank"">下载</a>!"
rstdata.close
set rstdata = nothing
conn.close
set conn = nothing
%>
上一篇: 用户控件如何控制ASPX页面的控件
下一篇: SQL 21 天自学通学习笔记 5