Sub DataTable2CSV(ByVal table As DataTable, ByVal filename As String)
DataTable2CSV(table, filename, vbTab)
End Sub
Sub DataTable2CSV(ByVal table As DataTable, ByVal filename As String, _
ByVal sepChar As String)
Dim writer As System.IO.StreamWriter
Try
writer = New System.IO.StreamWriter(filename)
' first write a line with the columns name
Dim sep As String = ""
Dim builder As New System.Text.StringBuilder
For Each col As DataColumn In table.Columns
builder.Append(sep).Append(col.ColumnName)
sep = sepChar
Next
writer.WriteLine(builder.ToString())
' then write all the rows
For Each row As DataRow In table.Rows
sep = ""
builder = New System.Text.StringBuilder
For Each col As DataColumn In table.Columns
builder.Append(sep).Append(row(col.ColumnName))
sep = sepChar
Next
writer.WriteLine(builder.ToString())
Next
Finally
If Not writer Is Nothing Then writer.Close()
End Try
End Sub
在搜索「DataTable to File VB.net」後,我在網上找到了這段代碼。我想要做的是在文件流中輸出它,是否可以設置響應輸出流?由於DataTable to File
編輯:爲了澄清(更改了)這裏就是我與現在的工作:
Protected Sub DoExportFinancials()
Dim ef As New ExportFinancials()
ef.Behavior = ExportFinancials.ObjectBehavior.Export
If ef.Load() = True Then
Me.GridView4.DataSource = ef.DT
Me.GridView4.DataBind()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
' Response.Charset = ""
'Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim stringWrite As StringWriter = New StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
GridView4.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End If
請詳細一點上它是什麼,你正在尋找。添加一些僞代碼,如果有的話,它顯示你想要這個代碼做什麼。 –
此刻此代碼只是將一個文件添加到我的網絡服務器,我希望它輸出一個文件給誰點擊按鈕在他們的計算機上導出。我已經讀過,說你只能用Response. *做到這一點,但沒有找到適合這個代碼的東西。當我現在單擊按鈕時,該文件嘗試將自己寫入無法訪問的服務器上。 – Solarplex