简体   繁体   English

使用经典ASP导出CSV

[英]Export a csv using classic asp

I am trying to export a csv from classic asp. 我正在尝试从经典的ASP导出csv。 The data is being fetched by oracle DB. oracle DB正在获取数据。 The query returns more than 2500 rows. 查询返回超过2500行。 Here is the code I am trying to use : 这是我尝试使用的代码:

<%
    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>

But all I am getting is site unreachable error. 但是我得到的只是站点无法到达的错误。 I tried to even display the data on the page and export to excel by changing content-type and file extension. 我甚至尝试在页面上显示数据,然后通过更改内容类型和文件扩展名将其导出为ex​​cel。 That works for less number of rows. 这适用于更少的行数。 But when the number of records fetched by the query is more, it will just give site unreachable error. 但是,当查询获取的记录数量更多时,它将仅给出站点无法访问的错误。

Could anybody help me out in resolving this issue. 任何人都可以帮助我解决这个问题。

sounds like your page is timing out, since it works for smaller amounts of data (I think that's what I understood you to say). 听起来您的页面正在超时,因为它只能处理少量数据(我想这就是您的意思)。 you could try extending the timeout setting for the page (which is 90 seconds by default) by putting the following code at the top of your page: 您可以通过在页面顶部放置以下代码来尝试扩展页面的超时设置(默认为90秒):

Server.ScriptTimeout[=NumSeconds]

I would also move your Response.Buffer = true line to the top of the page, and within your do while loop, flush out the response line by line. 我还将您的Response.Buffer = true行移动到页面顶部,并在do while循环内,逐行刷新响应。 Since it's going to an excel file, it won't look any different to the end user: 由于要转到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

if this doesn't work, it would be helpful to see the exact error message you're getting. 如果这不起作用,那么查看您收到的确切错误消息将很有帮助。

It also sounds like you may not be getting detailed error messages. 听起来您可能还没有收到详细的错误消息。

In IIS, under ASP, Debugging Properties, set Send Errors to Browser to true. 在IIS中的“ ASP”,“调试属性”下,将“将错误发送到浏览器”设置为true。

In Error Pages, Edit Feature Settings, select Detail Errors. 在“错误页面”的“编辑功能设置”中,选择“详细错误”。

Then make sure your browser is NOT set to friendly error messages. 然后,确保您的浏览器未设置为友好错误消息。

If it happens that one of those is not set correctly, then you won't be getting the line number and error that you need. 如果碰巧其中之一设置不正确,那么您将不会得到所需的行号和错误。

If all of those are set correctly, then the other advice to increase your session and script timeouts is good advice. 如果所有这些设置都正确,那么增加会话和脚本超时的其他建议就是好的建议。

Besides the server.scripttimeout, which is 90 seconds by default; 除了server.scripttimeout,默认为90秒;

Check in IIS if, for your website under the ASP settings, the limits properties aren't too strict. 对于在ASP设置下的网站,请检查IIS是否对限制属性不太严格。 There are two options, Maximum requesting Entity Body Limit is for allowing big uploads, Response Buffering Limit is for allowing big downloads. 有两个选项,“最大请求实体正文限制”用于允许大量上传,“响应缓冲限制”用于允许大量下载。 If your CSV exceeds this limit, it won't download. 如果您的CSV超出此限制,则不会下载。

IIS限制ASP的设置

I see that you have already set your content-type correctly. 我看到您已经正确设置了内容类型。 If you are rendering a CSV to the browser, it should have response.ContentType = "text/csv" before you do any output. 如果要向浏览器呈现CSV,则在执行任何输出之前,它应该具有response.ContentType =“ text / csv”。

Somewhat unrelated, it is also possible to render your recordset into a CSV immediately (in a single line of code) in ASP, using the GetString() function: 有点无关,还可以使用GetString()函数在ASP中立即(在一行代码中)将记录集呈现为CSV:

csv = RS.GetString(2,,vbCrLf,",","")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM