-3

我是 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
4

2 回答 2

1

尝试这个

SearchString = Replace(SearchString, Chr(13), ",")
于 2012-09-23T06:08:05.337 回答
1

您的数据结构不完善。如果是 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
于 2012-09-23T06:28:45.483 回答