sql = "Select case when date ='' then '0'else CONVERT(varchar(100), date, 101) end as date,case when ad ='' then '0'else CONVERT(varchar(100), ad, 101) end as ad,event1,type1,code from EventData where ip='" + UserIP + "' order by code,event1"

            Dim table As DataTable = gData.GetDataTable(sql, SqlConnect) '將所有的查詢結果放入臨時表中,在查詢臨時表內容
            Dim count = table.Rows.Count

            If count = 0 Then
                MessageBox.Show("no data.")
                Exit Sub
            End If

            Dim sw As StreamWriter = File.CreateText(TextBox1.Text & "\" & "Events.csv") '在桌面創建文件Events.csv

            Dim c As String = ""
            For i As Integer = 0 To count
                If i = 0 Then 'header
                    sw.WriteLine("E.CODE,E.TYPE,E.EVENT,E.AD,E.DONE") '標題
                Else 'data
                    For j As Integer = 0 To 4 '一共5列
                        If j = 0 Then
                            c = table.Rows(i - 1).Item(j)
                        Else
                            c = table.Rows(i - 1).Item(j) & "," & c '對字段進行拼接
                        End If
                    Next
                    sw.WriteLine(c)
                    c = "" '將C清空
                End If
            Next

            sw.Flush()
            sw.Close() '新建文件將原有的內容覆蓋

posted on 2013-10-14 14:29  vinsonLu  阅读(4523)  评论(0编辑  收藏  举报