写入CSV文件时跳过第一行

本文关键字:一行 CSV 文件 写入 | 更新日期: 2023-09-27 18:10:00

我编写了这段代码来读取和写入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);
              }

写入CSV文件时跳过第一行

如果您不想写第一行,请在开始写之前使用一次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);
         // ...

在实际开始读取输入之前调用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