How to covert tab separated file to CSV file

前端 未结 5 1480
逝去的感伤
逝去的感伤 2021-01-27 12:17

I have a tab separated text file which is about 1.2 GB , i need to convert it into CSV file(Comma Separated) using c#. I have to insert a bulk data into sqlserver, the data is i

5条回答
  •  没有蜡笔的小新
    2021-01-27 12:40

    I came up with this which splits on the tab character:

       using(System.IO.StreamReader rdr = new System.IO.StreamReader(@"C:\text.txt"))
       {
                int counter = 0;
                string lne;
                while((lne = rdr.ReadLine()) != null)
                {
                    string[] temp = lne.Split('\t');
                    Console.WriteLine(temp[0]);
                    Console.WriteLine(temp[1]);
                    counter++;
                }
       }
       Console.ReadLine();
    

    After that you can just use a StringBuilder to concatenate the the items in the array with a comma.

提交回复
热议问题