...
Dim sdata As String = exportcsv(dt)
Response.ClearHeaders()
Response.ContentEncoding = System.Text.Encoding.Default'此句避免轉出內容呈亂碼
Response.AppendHeader("Content-disposition", "attachment;filename=mycsv.csv")
Response.Write(sdata)
Response.End()
...
Private Function exportcsv(ByVal dt As System.Data.DataTable) As String
Dim sdata As String = ""
Dim irow As DataRow
Dim icol As DataColumn
For Each icol In dt.Columns
sdata += icol.ColumnName + ","'讀取各列名
Next
sdata += Microsoft.VisualBasic.vbCrLf'換行
For Each irow In dt.Rows
For Each icol In dt.Columns
sdata += irow(icol).ToString + ","'讀取各欄值
Next
sdata += Microsoft.VisualBasic.vbCrLf'換行
Next
sdata += Microsoft.VisualBasic.vbCrLf'換行
Return sdata
End Function
此種方法適用於資料量小的情況,比如500筆以內,若超出則速度會很非常的慢,我在XP下測試幾乎會當掉:CPU在100%徘徊
現在找到另外一種快速方法,改天再補