急:求一 数据下载后形成EXCEL表格的源文件,哪位老大帮帮

急:求一 数据下载后形成EXCEL表格的源文件,哪位老大帮帮
---------------------------------------------------------------

 1   
 2'前面包含的文件是和来链接到数据库,你要修改这个文件以链接到不同的库   
 3  
 4sql1="select username,usermail from maillist" '这里是要输出EXCEL的查询语句,如 "SESECT * FORM CAI WHERE 性别='女'"   
 5filename="excel.xls" ' 要输出的EXCEL文件的文件名, 你只要改以上两句就行了,其它的都不要改.   
 6  
 7  
 8call toexcel(FILENAME,sql1)   
 9set conn=nothing   
10  
11function ReadText(FileName) '这是一个用于读出文件的函数   
12set adf=server.CreateObject("Adodb.Stream")   
13with adf   
14.Type=2   
15.LineSeparator=10   
16.Open   
17.LoadFromFile (server.MapPath(FileName))   
18.Charset="GB2312"   
19.Position=2   
20ReadText=.ReadText   
21.Cancel()   
22.Close()   
23end with   
24set ads=nothing   
25end function   
26  
27sub SaveText(FileName,Data) '这是一个用于写文件的函数   
28set fs= createobject("scripting.filesystemobject")   
29set ts=fs.createtextfile(server.MapPath(FileName),true)   
30ts.writeline(data)   
31ts.close   
32set ts=nothing   
33set fs=nothing   
34end sub   
35  
36sub toexcel(filename,sql) '这是一个根据SQL语句和FILENAME生成EXCEL文件   
37Set rs=Server.CreateObject("ADODB.RecordSet")   
38' Response.Write sql   
39rs.Open sql,conn,1,1   
40TOEXCELLR="

<table width="100%"><tr>"
set myfield=rs.fields
dim fieldname(50)
for i=0 to myfield.count-1
toexcellr=toexcellr&amp;"<td class="xl24">"&amp;MYFIELD(I).NAME&amp;"</td>"
fieldname(i)=myfield(i).name
if myfield(i).type=135 then datename=datename&amp;myfield(i).name&amp;","
next
toexcellr=toexcellr&amp;"</tr>"
do while not rs.eof
toexcellr=toexcellr&amp;"<tr>"
for i=0 to myfield.count-1
if instr(datename,fieldname(i)&amp;",")&lt;&gt;0 then
if not isnull(rs(fieldname(i))) then
TOEXCELLR=TOEXCELLR&amp;"<td class="xl25"><p align="left">"&amp;formatdatetime(rs(fieldname(i)),2)&amp;"</p></td>"
else
TOEXCELLR=TOEXCELLR&amp;"<td class="xl25"><p align="left"> </p></td>"
end if
else
TOEXCELLR=TOEXCELLR&amp;"<td class="xl24">"&amp;rs(fieldname(i))&amp;"</td>"
end if
next
toexcellr=toexcellr&amp;"</tr>"
rs.movenext
loop
rs.close
toexcellr=toexcellr&amp;"</table>

1"   
2tou=readtext("tou.txt")   
3di=readtext("di.txt")   
4toexcellr=tou&toexcellr&di   
5call savetext(filename,toexcellr)   
6end sub   
 1<html>
 2<head>
 3<meta content="3;URL=```
 4=filename
 5```" http-equiv="refresh"/>
 6<meta content="en-us" http-equiv="Content-Language"/>
 7<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 8<title>正在生成EXCEL文件</title>
 9</head>
10<body>   
11正在生成EXCEL文件....   
12</body>
13</html>

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

强烈支持ING

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

不想写了………………方法可以是用fso 的copyFile

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

下面方法我试过,可以的。
例:

1<a href="downattach.asp?n=```
2=fileurl
3```&amp;upname=test.xls">test.xls</a>

注:fileurl为test.xls相对于当前文件的路径,
downattach.asp文件

 1   
 2Response.Buffer = true   
 3Response.Clear   
 4  
 5dim url   
 6Dim fso,fl,flsize   
 7dim Dname   
 8Dim objStream,ContentType,flName,isre,url1   
 9'*********************************************调用时传入的下载文件名   
10Dname=trim(request("n"))   
11upname=trim(request("upname"))   
12'******************************************************************   
13If Dname<>"" Then   
14'******************************下载文件存放的服务端目录   
15url=server.MapPath("attech/")&"\"&Dname   
16  
17'***************************************************   
18End If   
19  
20Set fso=Server.CreateObject("Scripting.FileSystemObject")   
21Set fl=fso.getfile(url)   
22flsize=fl.size   
23flName=fl.name   
24Set fl=Nothing   
25Set fso=Nothing   
 1   
 2Set objStream = Server.CreateObject("ADODB.Stream")   
 3objStream.Open   
 4objStream.Type = 1   
 5objStream.LoadFromFile url   
 6  
 7  
 8Select Case lcase(Right(flName, 4))   
 9Case ".asf"   
10ContentType = "video/x-ms-asf"   
11Case ".avi"   
12ContentType = "video/avi"   
13Case ".doc"   
14ContentType = "application/msword"   
15Case ".zip"   
16ContentType = "application/zip"   
17Case ".xls"   
18ContentType = "application/vnd.ms-excel"   
19Case ".gif"   
20ContentType = "image/gif"   
21Case ".jpg", "jpeg"   
22ContentType = "image/jpeg"   
23Case ".wav"   
24ContentType = "audio/wav"   
25Case ".mp3"   
26ContentType = "audio/mpeg3"   
27Case ".mpg", "mpeg"   
28ContentType = "video/mpeg"   
29Case ".rtf"   
30ContentType = "application/rtf"   
31Case ".htm", "html"   
32ContentType = "text/html"   
33Case ".txt"   
34ContentType = "text/plain"   
35Case Else   
36ContentType = "application/octet-stream"   
37End Select   
38  
39  
40  
41Response.AddHeader "Content-Disposition", "attachment; filename=" & upname   
42Response.AddHeader "Content-Length", flsize   
43  
44Response.Charset = "UTF-8"   
45Response.ContentType = ContentType   
46  
47Response.BinaryWrite objStream.Read   
48Response.Flush   
49response.Clear()   
50objStream.Close   
51Set objStream = Nothing   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus