ASP.NET下载CSV文件

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        
Dim aSql As String
        
Dim i As Integer, j As Integer
        
Dim aRow As Long
        
Dim aDs As New DataSet
        
Dim aRecord As String
        
Const FLD_CNT& = 22

        
Dim Encodingd As System.Text.Encoding

        Encodingd 
= System.Text.Encoding.GetEncoding("Shift-JIS")

        Response.ContentType 
= "application/x-download"
        Response.AddHeader(
"Content-Disposition""filename=Skmfile.csv")
        Response.AddHeader(
"Content-Type""text/csv; charset=SHIFT_JIS")

        aSql 
= ""
        aSql 
= aSql & "Select * From SKMSOMT"


        
If GetData(aSql, "SKMSOMT", aDs) = True Then
            
For i = 0 To aDs.Tables("SKMSOMT").Rows.Count - 1
                
For j = 0 To FLD_CNT - 1
                    
Select Case j
                        
Case 1
                            
'1項目目
                            aRecord = ""
                            aRecord 
= aDs.Tables("SKMSOMT").Rows(i).Item(j)
                        
Case 468101215161718
                            
'数字
                            aRecord = aRecord & "," & CDblX(aDs.Tables("SKMSOMT").Rows(i).Item(j))
                        
Case 12356911
                            
'文字
                            aRecord = aRecord & ",""" & aDs.Tables("SKMSOMT").Rows(i).Item(j) & """"
                        
Case 141921
                            
'日付
                            If IsDate(Format(aDs.Tables("SKMSOMT").Rows(i).Item(j), "0000/00/00")) = True Then
                                aRecord 
= aRecord & ",""" & Format(aDs.Tables("SKMSOMT").Rows(i).Item(j), "0000/00/00"& """"
                            
Else
                                aRecord 
= aRecord & ","" """
                            
End If
                        
Case 2022
                            
'時間
                            If IsDate(Format(aDs.Tables("SKMSOMT").Rows(i).Item(j), "00:00:00")) = True Then
                                aRecord 
= aRecord & ",""" & Format(aDs.Tables("SKMSOMT").Rows(i).Item(j), "00:00:00"& """"
                            
Else
                                aRecord 
= aRecord & ","" """
                            
End If
                        
Case Else
                            
'読み飛ばし
                    End Select
                
Next
                aRecord 
= aRecord & vbCrLf
                Response.BinaryWrite(Encodingd.GetBytes(aRecord))
            
Next
            Response.End()
        
End If
    
End Sub
Response.ContentType属性指定服务器响应的 HTTP 内容类型,默认为 text类型的HTML格式文件。除了text他还支持image、audio、video、application等类型。
Response.End 是Response常用的一种方法,使 Web 服务器停止处理脚本并返回当前结果,文件中剩余的内容将不被处理。
Response.BinaryWrite(Buffer);. 运行,IE出现打开、下载文件的对话框。