2015-09-09 96 views
1

更新的代碼。Visual Basic文本文件操作

Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click 
    Dim xString = File.ReadAllLines("C:\test\bbooktest.csv") 
    Dim qString As String = Nothing 
    For Each myItem In lstboxSerialout.Items 

     For Each x As String In xString 
      If x.Contains(myItem) And x.Contains(", ***,") Then 
       qString = qString & x.Replace(", ***,", ", " & txtboxdateshpd.Text & ",") & vbCrLf 
      Else 
       qString = qString & x & vbCrLf 
      End If 
     Next 
    Next 
    File.WriteAllText("C:\test\bbooktest.csv", qString) 
End Sub 

這就是我在運行時得到的結果。

MANUFACTURER,MODEL,SERIAL ,TYPE,Size,DATE REC,MFG NAME ,DATE SHIP,COMMENTS 
Test Company, Test Model,123456,Test Type,Test Size,20150917,*****,2015,****** 
Test Company, Test Model,434343,Test Type,Test Size,20150917,*****,***,******* 
Test Company, Test Model,789456,Test Type,Test Size,20150917,*****,***,******* 
MANUFACTURER,MODEL,SERIAL ,TYPE,Size,DATE REC,MFG NAME ,DATE SHIP,COMMENTS 
Test Company, Test Model, 123456, Test Type, Test Size, 20150917, ******, ***, ******** 
Test Company, Test Model, 434343, Test Type, Test Size, 20150917, ******, 2015, ******** 
Test Company, Test Model, 789456, Test Type, Test Size, 20150917, ******, ***, ******** 
MANUFACTURER,MODEL,SERIAL ,TYPE,Size,DATE REC,MFG NAME ,DATE SHIP,COMMENTS 
Test Company, Test Model, 123456, Test Type, Test Size, 20150917, ******, ***, ******** 
Test Company, Test Model, 434343, Test Type, Test Size, 20150917, ******, ***, ******** 
Test Company, Test Model, 789456, Test Type, Test Size, 20150917, ******, 2015, ******** 

當我希望它看起來這本

MANUFACTURER,MODEL,SERIAL ,TYPE,Size,DATE REC,MFG NAME ,DATE SHIP,COMMENTS 
Test Company, Test Model,123456, Test Type, Test Size,20150917, ******,2015, ******** 
Test Company, Test Model,434343, Test Type, Test Size,20150917, ******,2015, ******** 
Test Company, Test Model,789456, Test Type, Test Size,20150917, ******,2015, ******** 

對不起了令人難以置信的窮人格式化我就使它看起來不那麼瘋狂的工作。

+0

由於數據是CSV格式,因此您可以非常輕鬆地將其讀取到DataTable或List(T),並在不解析文本的情況下將它們引用爲列或屬性 – Plutonix

+0

究竟哪些方法無效? –

+0

這假設序列號在所有數據集中都是唯一的。如果某個型號名稱包含其他序列號的確切數字,該怎麼辦?你的替換應該讓昏迷回來:「,測試」,而不是「測試」 –

回答

0

For Each myItem循環之外移動Dim xString = File.ReadAllLines("C:\test\bbooktest.csv")

+0

感謝您的快速回復。我試過了,我仍然得到相同的結果。 –