asp導(dǎo)出內(nèi)容到excel表并自定義命名后下載(非打開)
網(wǎng)絡(luò) 2019/4/16 14:29:04 深山工作室 字體:
大 中 小 瀏覽 10532
本ASP程序代碼實現(xiàn)兩個功能:
1、將指定的數(shù)據(jù)庫內(nèi)容導(dǎo)出到EXCEL表格(可改為其它格式)。
2、任何類型的文件都是直接下載,不是在瀏覽器打開,并且自定義另存為對話框里的默認文件名。
完整代碼如下:
<%
dim action
action=lcase(trim(request.querystring("action")))
if action="down" then '下載文件
'任何類型的文件都是直接下載,不是在瀏覽器打開,且自定義另存為對話框里的默認文件名。
call gw_downfile(trim(request.querystring("f")),"文章列表.xls") '參數(shù):服務(wù)器端文件路徑及文件名,客戶端下載時的默認文件名
else '導(dǎo)出文件
'將指定的數(shù)據(jù)庫內(nèi)容導(dǎo)出到EXCEL表格(可改為其它格式)
const filename="news.xls" '導(dǎo)出后的文件名(全名,帶.擴展名)
dim fs,filepath,myfile
set fs=server.createobject("scripting.filesystemobject")
filepath=server.mappath(filename)
if fs.FileExists(filepath) then fs.DeleteFile(filepath)
set myfile=fs.CreateTextFile(filepath,true)
'chr(9)等于tab鍵
dim strline
strline="發(fā)布時間" & chr(9) & "分類" & chr(9) & "標題" & chr(9) & "內(nèi)容"
myfile.writeline strLine
dim rs
'conn為打開數(shù)據(jù)庫變量
set rs=conn.execute("select * from [表名] order by [id] desc")
do while not rs.eof
strline=rs("發(fā)布時間") & chr(9) & rs("分類") & chr(9) & rs("標題") & chr(9) & rs("內(nèi)容")
myfile.writeline strLine
rs.movenext() : loop
set rs=nothing
set myfile=nothing
set fs=nothing
response.write("導(dǎo)出成功!點擊下載:<a href='?action=down&f=" & filename & "'>" & filename & "</a>")
end if
'功能:為文件下載"另存為"對話框指定默認文件名
'參數(shù):服務(wù)器端文件路徑及文件名,客戶端下載時的默認文件名
function gw_downfile(file_server,file_client)
gw_downfile=false
dim filename : filename=server.mappath(file_server)
dim fso,fso_file,file_length
set fso=server.createobject("scripting.filesystemobject")
if not fso.fileexists(filename) then exit function '檢驗文件是否存在
set fso_file=fso.getfile(filename) '生成文件對象
file_length=fso_file.size '獲取文件大小
'開啟緩存,直到出現(xiàn)response.flush或response.end才將響應(yīng)發(fā)送給客戶端瀏覽器
response.buffer=true
'清除緩沖區(qū)中的所有HTML輸出
response.clear()
'指定返回的是一個不能被客戶端讀取的流,必須被下載
response.contenttype="application/octet-stream"
'添加頭信息,為"文件下載/另存為"對話框指定默認文件名
response.addheader "content-disposition","attachment; filename=" & file_client
'添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
response.addheader "content-length",file_length
dim stream
set stream=server.createobject("adodb.stream") '創(chuàng)建讀二進制文件對象
stream.type=1 '指定或返回的數(shù)據(jù)類型為二進制,2為文本
stream.open()
stream.loadfromfile(filename) '將指定的文件裝入對像中
'eos返回對像內(nèi)數(shù)據(jù)是否為空
'read讀取指定長度的二進制內(nèi)容
'readtext讀取指定長度的文本內(nèi)容
while not stream.eos
response.binarywrite stream.read(1024*64) '以塊方式讀取內(nèi)容
wend
stream.close() : set stream=nothing
'立即發(fā)送緩沖區(qū)中的輸出。如果未將response.buffer設(shè)置為true,則該方法將導(dǎo)致運行時錯誤。
response.flush()
gw_downfile=true
end function
%>
- 相關(guān)閱讀
- CSS語法在DW中無效的解決方法
- 用ASP對網(wǎng)頁進行限制性的訪問
- 深山行者asp購物網(wǎng)終于上線了
- DateDiff(計算類型,日期1,日期2)計算兩個日期之間的差
- ASP操作access或sqlserver數(shù)據(jù)庫的函數(shù)庫
- 松原市海航商務(wù)旅行社
- Request.ServerVariables在網(wǎng)頁中的一些應(yīng)用集合
- 旅行社網(wǎng)站管理系統(tǒng)商業(yè)版4.0發(fā)布
- 共有0條關(guān)于《asp導(dǎo)出內(nèi)容到excel表并自定義命名后下載(非打開)》的評論
- 發(fā)表評論