2017-06-29 95 views
0

我想從經典asp導出csv。數據由Oracle DB提取。該查詢返回超過2500行。這裏是我試圖使用的代碼:使用經典asp導出csv

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

聽起來你的網頁已超時,因爲它適用於較少量的數據(我認爲這就是我所理解的你所說的)。你可以嘗試延長超時設置的頁面(默認情況下爲90秒),通過將下面的代碼在你的頁面的頂部:

Server.ScriptTimeout[=NumSeconds] 

我也移動你將Response.Buffer = TRUE行到頁面頂部,並在do while 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 

,如果這也不行,那將是有益的,看看你得到確切的錯誤信息。

+0

您好我會嘗試,讓你知道結果。謝謝。 –

+0

嗨我得到了以下錯誤 無法聯繫到此站點 http://dev.web.com/ExcelExport.asp的網頁可能暫時關閉,或者它可能已永久移動到新的Web地址。 ERR_INVALID_RESPONSE 服務器是Windows 2003計算機 –

+0

我已將Server.ScriptTimeout = 6000添加到頁面的頂部。 –

0

這聽起來像你可能沒有得到詳細的錯誤消息。

在IIS中,在ASP,Debugging Properties下,將Send Errors to Browser設置爲true。

在錯誤頁面中,編輯功能設置,選擇詳細錯誤。

然後確保您的瀏覽器未設置爲友好的錯誤消息。

如果碰巧其中一個設置不正確,那麼您將不會得到所需的行號和錯誤。

如果所有這些都設置正確,那麼增加會話和腳本超時的其他建議是很好的建議。

0

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

檢查IIS是否對於您的網站在ASP設置下的限制屬性不太嚴格。有兩種選擇,最大請求實體正文限制允許大量上傳,響應緩衝限制允許大量下載。如果您的CSV超過此限制,則不會下載。

IIS limits settings for ASP

我看你已經正確設置你的內容類型。如果您要將CSV呈現給瀏覽器,則在執行任何輸出之前,它應該有response.ContentType =「text/csv」。

有點不相關的,也可以立即使你的記錄到CSV(在一個單一的代碼行)在ASP中,使用GetString()功能:

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