2014-06-24 325 views
0

我已经编写了此代码以读取和写入CSV文件。有什么办法可以跳过将第一行作为它的头文件,并且我想将自己的头文件写入新文件。这是我迄今为止所做的写入csv文件时跳过第一行

try 
    { 
     using (StreamWriter sw = new StreamWriter("C:/Projects/data/PYAEGON1AEGONRESULT.csv")) 
      using (StreamReader sr = new StreamReader("C:/Projects/data/PYAEGON1AEGON.csv")) 
      { 
       heading = "Title, First Name, Surname, Date Of Birth, NI Number, Gender, Payroll Reference, Address Line 1, Address Line 2, Address Line 3, Address Line 4, AddressLine 5, PostCode, Country, Email, Date Joined Employer, Annual Pensionable Salary, Current Period All Earnings (Monthly), Current Period Pensionable Earnings (Monthly), Emnployee (Amount Deducted), Employer (Amount Deducted), Leaving Date."; 
       sw.WriteLine(heading); 

       while((txtline = sr.ReadLine()) != null) 
       { 
        oldcolumns = Regex.Split(txtline,","); 

        newcolumns[0] = oldcolumns[0]; 
        newcolumns[1] = oldcolumns[1]; 
        newcolumns[2] = oldcolumns[2]; 
        newcolumns[3] = oldcolumns[3]; 
        newcolumns[4] = oldcolumns[4]; 
        newcolumns[5] = oldcolumns[5]; 
        newcolumns[6] = oldcolumns[6]; 
        newcolumns[7] = oldcolumns[7]; 
        newcolumns[8] = oldcolumns[9]; 
        newcolumns[9] = oldcolumns[10]; 
        newcolumns[10] = oldcolumns[11]; 
        newcolumns[11] = ""; 
        newcolumns[12] = oldcolumns[12]; 
        newcolumns[13] = "United Kingdom"; 
        newcolumns[14] = oldcolumns[14]; 
        newcolumns[15] = oldcolumns[15]; 
        newcolumns[16] = oldcolumns[16]; 
        newcolumns[17] = oldcolumns[17]; 
        newcolumns[18] = oldcolumns[18]; 
        newcolumns[19] = oldcolumns[19]; 
        newcolumns[20] = oldcolumns[20]; 
        newcolumns[21] = ""; 

        csvline = ""; 
        for (int i = 0; i < 21; i++) 
        { 
         csvline = csvline + "\"" + newcolumns[i].Replace("\"","") + "\","; 

        } 

        sw.WriteLine(csvline); 
       } 
+2

只需在您的代码中添加一个布尔标志,指示它是否是第一行,如果它为真则不写?真的不清楚你被什么混淆了。 –

+2

如果你不想写第一行,在开始写之前使用'sr.ReadLine()'。 –

+1

读取和写入csv文件比这更复杂,E.G.那么在文本和其他内容中,'你'应该是谷歌csvreader,那么你所有关于csv的问题都会自动解决。 – Casperah

回答

0

在您实际开始读取输入之前,请致电sr.ReadLine()

using (StreamWriter sw = new StreamWriter("C:/Projects/data/PYAEGON1AEGONRESULT.csv")) 
    using (StreamReader sr = new StreamReader("C:/Projects/data/PYAEGON1AEGON.csv")) 
    { 
     sr.ReadLine(); 

     // rest of your code 
+0

试过这个。它打印第一行,然后跳到第三行的一行..跳到第五行的行等 – user3734454

+0

我想你把它放在'while'而不是上面。 –

1

如果你不想让你再动笔写第一行使用sr.ReadLine()一次。这使读者进入第二行。

using (StreamWriter sw = new StreamWriter("C:/Projects/data/PYAEGON1AEGONRESULT.csv")) 
using (StreamReader sr = new StreamReader("C:/Projects/data/PYAEGON1AEGON.csv")) 
{ 
    sr.ReadLine(); 
    // the next ReadLine will read the second line as desired 
    heading = "Title, First Name, Surname, Date Of Birth, NI Number, Gender, Payroll Reference, Address Line 1, Address Line 2, Address Line 3, Address Line 4, AddressLine 5, PostCode, Country, Email, Date Joined Employer, Annual Pensionable Salary, Current Period All Earnings (Monthly), Current Period Pensionable Earnings (Monthly), Emnployee (Amount Deducted), Employer (Amount Deducted), Leaving Date."; 
    sw.WriteLine(heading); 
    while((txtline = sr.ReadLine()) != null) 
    { 
     // ... 
     sw.WriteLine(csvline); 
     // ... 
+0

试过这个。它打印第一行,然后跳到第三行的一行......跳到第五行的行等等。 – user3734454

+0

@ user3734454:也许你已经将'sr.ReadLine()'添加到了while循环中。看看我的代码示例。在while循环之前,我只使用'ReadLine'来跳过标题。 –