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 4, 6, 8, 10, 12, 15, 16, 17, 18
'数字
aRecord = aRecord & "," & CDblX(aDs.Tables("SKMSOMT").Rows(i).Item(j))
Case 1, 2, 3, 5, 6, 9, 11
'文字
aRecord = aRecord & ",""" & aDs.Tables("SKMSOMT").Rows(i).Item(j) & """"
Case 14, 19, 21
'日付
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 20, 22
'時間
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等类型。
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 4, 6, 8, 10, 12, 15, 16, 17, 18
'数字
aRecord = aRecord & "," & CDblX(aDs.Tables("SKMSOMT").Rows(i).Item(j))
Case 1, 2, 3, 5, 6, 9, 11
'文字
aRecord = aRecord & ",""" & aDs.Tables("SKMSOMT").Rows(i).Item(j) & """"
Case 14, 19, 21
'日付
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 20, 22
'時間
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.End 是Response常用的一种方法,使 Web 服务器停止处理脚本并返回当前结果,文件中剩余的内容将不被处理。
Response.BinaryWrite(Buffer);. 运行,IE出现打开、下载文件的对话框。
本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 |