开发者

How to open a CSV file update some rows, columns, delete some rows and save it as another CSV file in VB.net?

I have a csv file. I need to open it, delete whole row on 开发者_运维技巧basis of a column value, Update few of the column values and save the file as .dat file. I am using VB.net 2010


You can use LINQ to load, remove and update your CSV, for example:

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)

Tested with this csv-file with US-presidents.

Option Strict On | Option Infer On | Option Explicit On

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜