2015-04-07 67 views
-1
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 
+0

請詳細一點上它是什麼,你正在尋找。添加一些僞代碼,如果有的話,它顯示你想要這個代碼做什麼。 –

+0

此刻此代碼只是將一個文件添加到我的網絡服務器,我希望它輸出一個文件給誰點擊按鈕在他們的計算機上導出。我已經讀過,說你只能用Response. *做到這一點,但沒有找到適合這個代碼的東西。當我現在單擊按鈕時,該文件嘗試將自己寫入無法訪問的服務器上。 – Solarplex

回答

1

下面應該工作,

Dim FilePath As String = serverPath & "\App_Data\" & FileName 
    Response.Clear() 
    Response.ContentType = "text/csv" 
    Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName) 
    Response.TransmitFile(FilePath) 
    Response.End() 

編輯:基於問題的更多細節, [更改]:如何將DataTable作爲CSV/Excel文件傳遞給響應流,而無需將其寫入服務器上的磁盤。上述完成的

的一種方法是首先要綁定的DataTable到GridView,然後GridView控件的內容,可以使用下面的傳遞到響應流,

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) 
grid.RenderControl(htmlWrite) Response.Write(stringWrite.ToString()) 
Response.End() 
+0

這在網絡服務器上查找文件,不是嗎?我正在嘗試從數據表下載文件 – Solarplex

+1

如果我是正確的,您正在使用DataTable2CSV將數據表的內容寫入CSV文件。這將創建一個新的CSV文件。在上面的代碼片段中傳遞CSV文件的相同文件路徑,您的新文件應該被下載。如果這不是您要查找的內容,請詳細說明。 –

+0

如果我有寫入權限的網絡服務器,這將是解決方案。我真的需要它從DataTable直接寫入用戶的下載。也許我只是要求管理員進行寫入訪問,我很感激幫助。 – Solarplex