[英]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.