利用FileStream 和StreamWriter生成csv文件:
'利用生成csv文件实现Web自定义报表 public shared Function WriteCsv(ByVal dt As DataTable, ByVal strTitle As String, ByVal filename As String) As String Dim path As String = HttpContext.Current.Server.MapPath("~/csv/" & filename & ".csv") Dim fw As New FileStream(path, FileMode.CreateNew, FileAccess.Write) Dim sw As New StreamWriter(fw, Text.Encoding.Default) sw.WriteLine(strTitle) Dim strRow As String = "" For i As Integer = 0 To dt.Rows.Count For j As Integer = 0 To dt.Columns.Count strRow += dt.Rows(i)(j) + "," Next j sw.WriteLine(strRow.Substring(0, strRow.Length - 1)) strRow = "" Next i sw.Close() fw.Close() sw.Dispose() fw.Dispose() Return path end Function