简体   繁体   中英

Export a csv using classic asp

I am trying to export a csv from classic asp. The data is being fetched by oracle DB. The query returns more than 2500 rows. 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. 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:

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. Since it's going to an excel file, it won't look any different to the end user:

    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.

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;

Check in IIS if, for your website under the ASP settings, the limits properties aren't too strict. 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.

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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