0
我有一個csv文件。我需要打開它,根據列值刪除整行,更新少數列值並將文件另存爲.dat文件。 我使用2010 VB.net如何打開CSV文件更新某些行,列,刪除一些行並將其另存爲VB.net中的另一個CSV文件?
我有一個csv文件。我需要打開它,根據列值刪除整行,更新少數列值並將文件另存爲.dat文件。 我使用2010 VB.net如何打開CSV文件更新某些行,列,刪除一些行並將其另存爲VB.net中的另一個CSV文件?
您可以使用LINQ來加載,刪除和更新您的CSV,例如:
Const separator = ","c
Dim csvPath = "C:\Temp\USPresident.csv"
Dim datPath = "C:\Temp\USPresident.dat"
Dim rows = (From line In IO.File.ReadAllLines(csvPath)
Select line.Split(separator)).ToList
' get all lines with specific value '
Dim presidentRows = (From cols In rows
Where cols.Contains("William Howard Taft")).ToList
' remove these lines with Except'
Dim rowsWithoutPresident = rows.Except(presidentRows).ToList
' update some values '
For Each row In rowsWithoutPresident
row(3) = "test-value"
Next
Dim newLines = (From cols In rowsWithoutPresident
Select String.Join(separator, cols)).ToArray
IO.File.WriteAllLines(datPath, newLines)
測試與this csv-file with US-presidents。
Option Strict On | Option Infer在|上Option Explicit對
http://filehelpers.sourceforge.net/ –