VB.net txt to csv

  • 本文关键字:csv to txt net VB vb.net
  • 更新时间 :
  • 英文 :


我是 vb.net 新手,我想将 txt 文件转换为 csv。我有这样的 txt 文件:

Name
A
Class
10
Roll No
123
Name
B
Class
9
Roll No
23
Name
C
Class
7
Roll No
3

如何像这样导出到csv文件中

A,10,123
B,9,23
c,7,3

您的数据结构不佳。如果是XML就好了。试试这个代码

static void Main(string[] args)
{
  StreamReader reader = new StreamReader(filename);
  StringBuilder csv = new StringBuilder();
  using (reader)
  {
    string line = "";
    while (!reader.EndOfStream)
    {
        reader.ReadLine();  //Skip the name line
        string name = reader.ReadLine();
        reader.ReadLine();  //Skip the class line
        string cls = reader.ReadLine();
        reader.ReadLine();  //skip the rollup line
        string rollno = reader.ReadLine();
        csv.AppendLine(String.Join(",", new string[] { name, cls, rollno }));
    }
  }
  Console.WriteLine(csv.ToString());
  Console.ReadLine();
}

输出

A,10,123
B,9,23
C,7,3

试试这个

SearchString = Replace(SearchString, Chr(13), ",")

最新更新