How to covert tab separated file to CSV file

前端 未结 5 1442
逝去的感伤
逝去的感伤 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:27

    Import the tab separated file into Excel (or another spreadsheet program) then export it as CSV.

    0 讨论(0)
  • 2021-01-27 12:30

    If its SQl Server 2005 and above, right click the database you want to import to and select Tasks and then Import Data. Point to your tab delimited file and just go through the remaining screens. On the final screen, just before you actually import, you have an option to save it as a SSIS package(check box). This is by far the easiest, fast and efficient way (without the conversion from tab delimited to csv) to import it without writing any code and the code is also available at the end of the wizard, which can be used to automate the task, if required.

    0 讨论(0)
  • 2021-01-27 12:36

    The BULK INSERT command in SQL Server 2005 will use a tab as a field delimiter by default so there is no need to convert a tab delimited text file to csv format.

    In fact, converting it to a real csv will make things more difficult. BULK INSERT will not respect quotes as a string delimiter so a true csv file that has strings encapsulated by quotes with commas somewhere in the string will break the import.

    See https://msdn.microsoft.com/en-us/library/ms188365(v=sql.90).aspx for more information.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-27 12:43

    If you are using sqlserver, you should have a look at SSIS (SqlServer integration Services). This tool will help you to import your csv file in a efficent manner. You can configure your seperator in this tool, so there is no need to replace the tabs with comma's.

    0 讨论(0)
提交回复
热议问题