vb 使用StreamWriter书写流写出数据并生成文件

时间:2023-03-09 05:43:35
vb 使用StreamWriter书写流写出数据并生成文件
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 = 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 = To count
If i = Then 'header
sw.WriteLine("E.CODE,E.TYPE,E.EVENT,E.AD,E.DONE") '標題
Else 'data
For j As Integer = To '一共5列
If j = Then
c = table.Rows(i - ).Item(j)
Else
c = table.Rows(i - ).Item(j) & "," & c '對字段進行拼接
End If
Next
sw.WriteLine(c)
c = "" '將C清空
End If
Next sw.Flush()
sw.Close() '新建文件將原有的內容覆蓋

vb 使用StreamWriter书写流写出数据并生成文件