ASP往EXCEL导数据问题?

见帖 http://expert.csdn.net/Expert/topic/1969/1969510.xml?temp=.4360163
---------------------------------------------------------------

'确保文件名唯一
strFileName = Session.SessionID & ".xls"
strAppPath = Request.ServerVariables("PATH_TRANSLATED")
strAppPath = Left(strAppPath, InstrRev(strAppPath, ""))
strFullPath = strAppPath & strFileName
'保存文件
myWorkbook.SaveAs(strFullPath)
'关闭Excel
myWorkbook.Close
xlApp.Quit
set myWorksheet = Nothing
set myWorkbook = Nothing
set myxlApp = Nothing
'写出到浏览器中
Response.Redirect strFileName

---------------------------------------------------------------

1@ LANGUAGE="VBSCRIPT" 
1option explicit
1<html>
2<head>
3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
4<title>生成EXCEL文件</title>
5</head>
6<body>
7<a href="dbtoexcel.asp?act=make">生成在线人口的EXCEL</a>
8<hr align="left" size="1" 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 sort desc"
rs.Open sql,conn
if rs.EOF and rs.BOF then
Response.Write "库里暂时没有数据!"
else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine= strLine &amp; x.name &amp; chr(9)
Next

'--将表的列名先写入EXCEL
myfile.writeline strLine

Do while Not rs.EOF
strLine=""

for each x in rs.Fields
strLine= strLine &amp; x.value &amp; 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
end if

1</body>
2</html>
Published At
Categories with Web编程
comments powered by Disqus