asp導(dǎo)出內(nèi)容到excel表并自定義命名后下載(非打開)
網(wǎng)絡(luò) 2019/4/16 14:29:04 深山工作室 字體:
大 中 小 瀏覽 10562
本ASP程序代碼實(shí)現(xiàn)兩個(gè)功能:
1、將指定的數(shù)據(jù)庫(kù)內(nèi)容導(dǎo)出到EXCEL表格(可改為其它格式)。
2、任何類型的文件都是直接下載,不是在瀏覽器打開,并且自定義另存為對(duì)話框里的默認(rèn)文件名。
完整代碼如下:
<%
dim action
action=lcase(trim(request.querystring("action")))
if action="down" then '下載文件
'任何類型的文件都是直接下載,不是在瀏覽器打開,且自定義另存為對(duì)話框里的默認(rèn)文件名。
call gw_downfile(trim(request.querystring("f")),"文章列表.xls") '參數(shù):服務(wù)器端文件路徑及文件名,客戶端下載時(shí)的默認(rèn)文件名
else '導(dǎo)出文件
'將指定的數(shù)據(jù)庫(kù)內(nèi)容導(dǎo)出到EXCEL表格(可改為其它格式)
const filename="news.xls" '導(dǎo)出后的文件名(全名,帶.擴(kuò)展名)
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ā)布時(shí)間" & chr(9) & "分類" & chr(9) & "標(biāo)題" & chr(9) & "內(nèi)容"
myfile.writeline strLine
dim rs
'conn為打開數(shù)據(jù)庫(kù)變量
set rs=conn.execute("select * from [表名] order by [id] desc")
do while not rs.eof
strline=rs("發(fā)布時(shí)間") & chr(9) & rs("分類") & chr(9) & rs("標(biāo)題") & chr(9) & rs("內(nèi)容")
myfile.writeline strLine
rs.movenext() : loop
set rs=nothing
set myfile=nothing
set fs=nothing
response.write("導(dǎo)出成功!點(diǎn)擊下載:<a href='?action=down&f=" & filename & "'>" & filename & "</a>")
end if
'功能:為文件下載"另存為"對(duì)話框指定默認(rèn)文件名
'參數(shù):服務(wù)器端文件路徑及文件名,客戶端下載時(shí)的默認(rèn)文件名
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 '檢驗(yàn)文件是否存在
set fso_file=fso.getfile(filename) '生成文件對(duì)象
file_length=fso_file.size '獲取文件大小
'開啟緩存,直到出現(xiàn)response.flush或response.end才將響應(yīng)發(fā)送給客戶端瀏覽器
response.buffer=true
'清除緩沖區(qū)中的所有HTML輸出
response.clear()
'指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
response.contenttype="application/octet-stream"
'添加頭信息,為"文件下載/另存為"對(duì)話框指定默認(rèn)文件名
response.addheader "content-disposition","attachment; filename=" & file_client
'添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進(jìn)度
response.addheader "content-length",file_length
dim stream
set stream=server.createobject("adodb.stream") '創(chuàng)建讀二進(jìn)制文件對(duì)象
stream.type=1 '指定或返回的數(shù)據(jù)類型為二進(jìn)制,2為文本
stream.open()
stream.loadfromfile(filename) '將指定的文件裝入對(duì)像中
'eos返回對(duì)像內(nèi)數(shù)據(jù)是否為空
'read讀取指定長(zhǎng)度的二進(jìn)制內(nèi)容
'readtext讀取指定長(zhǎng)度的文本內(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)致運(yùn)行時(shí)錯(cuò)誤。
response.flush()
gw_downfile=true
end function
%>
- 相關(guān)閱讀
- 復(fù)制信息給好友代碼
- 東莞理工班徽網(wǎng)絡(luò)在線投票
- 腳本控制三行三列自適應(yīng)高度DIV布局
- 中國(guó)大宗資產(chǎn)交易網(wǎng)
- 利用css中的scale()實(shí)現(xiàn)放大縮小效果
- uni-app基礎(chǔ)知識(shí)數(shù)據(jù)綁定
- 四川悠旅地旅游有限公司
- 陽(yáng)光麗江客棧
- 共有0條關(guān)于《asp導(dǎo)出內(nèi)容到excel表并自定義命名后下載(非打開)》的評(píng)論
- 發(fā)表評(píng)論