VB.Net 寫入CSV檔
前言:在蜜月期間,還是花了時間先測試了可能會做的案子要用到的功能。
參考網站:http://www.blueshop.com.tw/board/show.asp?subcde=BRD20100113143435FF1
匯入參考
參考網站:http://www.blueshop.com.tw/board/show.asp?subcde=BRD20100113143435FF1
將五個TtextBox中的資料插入csv中
Imports System.IO Imports System.Text Public Class Form1 Dim FilePath As String = "C:\csv.CSV" Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Try Using fs As IO.FileStream = New IO.FileStream(FilePath, FileMode.Append) Using sw As StreamWriter = New StreamWriter(fs, Encoding.Default) Dim str As String = TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," _ & TextBox4.Text & "," & TextBox5.Text ' sw.Write(str) '第一行 sw.WriteLine(str) '插入一整行 sw.Flush() End Using End Using Catch ex As Exception End Try End Sub End Class
匯入參考
1 Imports System.IO 2 Imports System.Text
儲存的檔案位置 5 Dim FilePath As String = "C:\csv.CSV"
11 Using fs As IO.FileStream = New IO.FileStream(FilePath, FileMode.Append) 12 13 Using sw As StreamWriter = New StreamWriter(fs, Encoding.Default) 14 15 Dim str As String = TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text 16 ' sw.Write(str) '第一行 17 sw.WriteLine(str) '插入一整行 18 sw.Flush() 19 End Using 20 End Using
注意:
第16行是寫入到第一行,每次都會從最後補上資料
第17行是插入到最後一行,但是無法寫入第一行。
範例參考:下載
想請問標題要如何加進去
回覆刪除比如說
ID NAME
1 小名
ID 和NAME如何加進去呢
感謝
17 sw.Write(str) '第一行
刪除這一行就是插入標題的指令,但他只能拿來插入標題