'DataTable存成CSV檔的程式碼----------------------------------
Dim SavePath As String = "C:\Temp\"
Dim FileName As String = "Test.csv"
Dim FilePath As String = SavePath + FileName
Dim sw As New System.IO.StreamWriter(FilePath , False, System.Text.Encoding.Default)

'寫入欄位名稱
If MyDataTable.Columns.Count > 0 Then
    sw.Write(MyDataTable.Columns.Item(0).ColumnName.ToString)
End If
For i As Integer = 1 To MyDataTable.Columns.Count - 1
    sw.Write("," + MyDataTable.Columns.Item(i).ColumnName.ToString)
Next
sw.Write(sw.NewLine)

'寫入各欄位資料
For i As Integer = 0 To MyDataTable.Rows.Count - 1
    For j As Integer = 0 To MyDataTable.Columns.Count - 1
        If j = 0 Then
            sw.Write(MyDataTable.Rows(i)(j))
        Else
            sw.Write("," + MyDataTable.Rows(i)(j))
        End If
    Next
    sw.Write(sw.NewLine)
Next

sw.Close()

'使用SaveAs.aspx隱藏檔案路徑讓使用者下載
Response.Redirect("SaveAs.aspx?filename=" + FileName)
'SaveAs.aspx.vb Page_Load 內的程式碼--------------------------
Dim SavePath As String = "C:\Temp\"
Dim FileName As String = Request.QueryString("filename").ToString
Dim FilePath As String = SavePath + FileName

'設定表頭並存檔
If System.IO.File.Exists(filep) Then
    With Response
        .ContentType = "application/save-as"
        .AddHeader("content-disposition", "attachment; filename=" & FileName)
        .ContentEncoding = Encoding.UTF8
        .WriteFile(FilePath)
    End With
Else
    Response.Write("no file")
End If
'做成Class方便使用
Public Class ExportCSV
    Function DataTableToCSV(ByVal dt As Data.DataTable, ByVal FileName As String, ByVal SavePath As String) As Boolean
        Dim FilePath As String = SavePath + FileName
        Try
            Dim sw As New System.IO.StreamWriter(FilePath, False, System.Text.Encoding.Default)

            If dt.Columns.Count > 0 Then
                sw.Write(dt.Columns.Item(0).ColumnName.ToString)
            End If
            For i As Integer = 1 To dt.Columns.Count - 1
                sw.Write("," + dt.Columns.Item(i).ColumnName.ToString)
            Next
            sw.Write(sw.NewLine)

            For i As Integer = 0 To dt.Rows.Count - 1
                For j As Integer = 0 To dt.Columns.Count - 1
                    If j = 0 Then
                        sw.Write(dt.Rows(i)(j))
                    Else
                        sw.Write("," + dt.Rows(i)(j))
                    End If
                Next
                sw.Write(sw.NewLine)
            Next
            sw.Close()
        Catch ex As Exception
            'MsgBox(ex.Message)
            Throw ex
        End Try

        If System.IO.File.Exists(FilePath) Then
            Return True
        Else
            Return False
        End If

    End Function
End Class
'存成CSV檔
Dim SavePath As String = "C:\Temp\"
Dim FileName As String = "Test.csv"
Dim epCsv As New ExportCSV
If epCsv.DataTableToCSV(MyDataTable, FileName, SavePath) = True Then
    Response.Redirect("SaveAs.aspx?filename=" + FileName)
End If

arrow
arrow
    全站熱搜

    伊 發表在 痞客邦 留言(0) 人氣()