【问题标题】:Export a csv using classic asp使用经典 asp 导出 csv
【发布时间】:2017-07-03 20:02:52
【问题描述】:

我正在尝试从经典 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。这适用于更少的行数。但是当查询获取的记录数较多时,只会报站点不可达错误。

谁能帮我解决这个问题。

【问题讨论】:

    标签: excel csv asp-classic


    【解决方案1】:

    听起来您的页面正在超时,因为它适用于较小的数据量(我想这就是我理解您所说的)。您可以尝试通过将以下代码放在页面顶部来延长页面的超时设置(默认为 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
    

    如果这不起作用,查看您收到的确切错误消息会很有帮助。

    【讨论】:

    • 您好,我会尝试并告诉您结果。谢谢。
    • 您好,我收到以下错误无法访问此站点dev.web.com/ExcelExport.asp 的网页可能暂时关闭,或者它可能已永久移至新网址。 ERR_INVALID_RESPONSE 服务器是windows 2003机器
    • 我已将 Server.ScriptTimeout = 6000 添加到页面顶部。
    • 看起来像是某种网络问题,因为它适用于少量数据。服务器是您本地的吗?还是在防火墙后面?尝试检查您的 IIS 日志/事件查看器,看看那里是否有任何其他详细信息
    【解决方案2】:

    听起来您可能没有收到详细的错误消息。

    在 IIS 中,在 ASP 下的调试属性下,将 Send Errors to Browser 设置为 true。

    在错误页面中,编辑功能设置,选择详细错误。

    然后确保您的浏览器未设置为友好的错误消息。

    如果其中一个设置不正确,那么您将无法获得所需的行号和错误。

    如果所有这些都设置正确,那么增加会话和脚本超时的其他建议是不错的建议。

    【讨论】:

      【解决方案3】:

      除了server.scripttimeout,默认是90秒;

      如果您的网站在 ASP 设置下的限制属性不是太严格,请检查 IIS。有两个选项,Maximum requesting Entity Body Limit 用于允许大上传,Response Buffering Limit 用于允许大下载。如果您的 CSV 超出此限制,则不会下载。

      我看到您已经正确设置了您的内容类型。如果您要向浏览器呈现 CSV,则在进行任何输出之前,它应该具有 response.ContentType = "text/csv"。

      有点不相关,也可以使用GetString() 函数在 ASP 中立即(在一行代码中)将您的记录集呈现为 CSV:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多