[英]Export a csv using classic asp
我正在嘗試從經典的ASP導出csv。 oracle DB正在獲取數據。 查詢返回超過2500行。 這是我嘗試使用的代碼:
<%
sub Write_CSV_From_Recordset(RS)
if RS.EOF then
'
' There is no data to be written
'
exit sub
end if
dim RX
set RX = new RegExp
RX.Pattern = "\r|\n|,|"""
dim i
dim Field
dim Separator
'
' Writing the header row (header row contains field names)
'
Separator = ""
for i = 0 to RS.Fields.Count - 1
Field = RS.Fields(i).Name
if RX.Test(Field) then
'
' According to recommendations:
' - Fields that contain CR/LF, Comma or Double-quote should be enclosed in double-quotes
' - Double-quote itself must be escaped by preceeding with another double-quote
'
Field = """" & Replace(Field, """", """""") & """"
end if
Response.Write Separator & Field
Separator = ","
next
Response.Write vbNewLine
'
' Writing the data rows
'
do until RS.EOF
Separator = ""
for i = 0 to RS.Fields.Count - 1
'
' Note the concatenation with empty string below
' This assures that NULL values are converted to empty string
'
Field = RS.Fields(i).Value & ""
if RX.Test(Field) then
Field = """" & Replace(Field, """", """""") & """"
end if
Response.Write Separator & Field
Separator = ","
next
Response.Write vbNewLine
RS.MoveNext
loop
end sub
Response.Buffer = True
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "attachment; filename=Export.csv"
theSQL = Session("Query")
Set RS = Connection.Execute(theSQL)
Write_CSV_From_Recordset RS
%>
<html>
<head>
<title>Excel/CSV Export</title>
</head>
<body>
</body>
</html>
但是我得到的只是站點無法到達的錯誤。 我甚至嘗試在頁面上顯示數據,然后通過更改內容類型和文件擴展名將其導出為excel。 這適用於更少的行數。 但是,當查詢獲取的記錄數量更多時,它將僅給出站點無法訪問的錯誤。
任何人都可以幫助我解決這個問題。
聽起來您的頁面正在超時,因為它只能處理少量數據(我想這就是您的意思)。 您可以通過在頁面頂部放置以下代碼來嘗試擴展頁面的超時設置(默認為90秒):
Server.ScriptTimeout[=NumSeconds]
我還將您的Response.Buffer = true行移動到頁面頂部,並在do while循環內,逐行刷新響應。 由於要轉到excel文件,因此與最終用戶看起來沒有什么不同:
do until RS.EOF
Separator = ""
for i = 0 to RS.Fields.Count - 1
'
' Note the concatenation with empty string below
' This assures that NULL values are converted to empty string
'
Field = RS.Fields(i).Value & ""
if RX.Test(Field) then
Field = """" & Replace(Field, """", """""") & """"
end if
Response.Write Separator & Field
Separator = ","
next
Response.Write vbNewLine
Response.Flush '<-----add this line here
RS.MoveNext
如果這不起作用,那么查看您收到的確切錯誤消息將很有幫助。
聽起來您可能還沒有收到詳細的錯誤消息。
在IIS中的“ ASP”,“調試屬性”下,將“將錯誤發送到瀏覽器”設置為true。
在“錯誤頁面”的“編輯功能設置”中,選擇“詳細錯誤”。
然后,確保您的瀏覽器未設置為友好錯誤消息。
如果碰巧其中之一設置不正確,那么您將不會得到所需的行號和錯誤。
如果所有這些設置都正確,那么增加會話和腳本超時的其他建議就是好的建議。
除了server.scripttimeout,默認為90秒;
對於在ASP設置下的網站,請檢查IIS是否對限制屬性不太嚴格。 有兩個選項,“最大請求實體正文限制”用於允許大量上傳,“響應緩沖限制”用於允許大量下載。 如果您的CSV超出此限制,則不會下載。
我看到您已經正確設置了內容類型。 如果要向瀏覽器呈現CSV,則在執行任何輸出之前,它應該具有response.ContentType =“ text / csv”。
有點無關,還可以使用GetString()函數在ASP中立即(在一行代碼中)將記錄集呈現為CSV:
csv = RS.GetString(2,,vbCrLf,",","")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.