我正在尝试从经典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。这适用于较少的行数。但是当查询提取的记录数量更多时,它只会给站点无法访问错误。
有人可以帮我解决这个问题。
答案 0 :(得分:0)
听起来你的页面已超时,因为它适用于少量数据(我认为这就是我理解你所说的)。您可以尝试通过将以下代码放在页面顶部来扩展页面的超时设置(默认为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
如果这不起作用,那么查看您获得的确切错误消息会很有帮助。
答案 1 :(得分:0)
听起来您可能没有收到详细的错误消息。
在IIS中,在ASP,Debugging Properties下,将Send Errors to Browser设置为true。
在错误页面,编辑功能设置中,选择明细错误。
然后确保您的浏览器未设置为友好的错误消息。
如果碰巧其中一个设置不正确,那么您将无法获得所需的行号和错误。
如果所有这些都设置正确,那么增加会话和脚本超时的其他建议是很好的建议。
答案 2 :(得分:0)
除了server.scripttimeout,默认为90秒;
检查IIS是否,对于您在ASP设置下的网站,限制属性是否过于严格。有两个选项,最大请求实体主体限制用于允许大上传,响应缓冲限制用于允许大量下载。如果您的CSV超出此限制,则无法下载。
我发现您已经正确设置了内容类型。如果要将CSV渲染到浏览器,它应该有response.ContentType =&#34; text / csv&#34;在你做任何输出之前。
有点无关,也可以使用GetString()函数在ASP中立即将您的记录集渲染为CSV(在一行代码中):
csv = RS.GetString(2,,vbCrLf,",","")