按日期在csv行中多次删除

时间:2017-06-17 02:01:32

标签: c# csv

追求我在csv文件中删除多行的目标。我的想法是获取两个日期之间的日期,这些日期不包括删除。但是,如何在两个日期之间迭代几天,并将值转换为字符串并用于我的代码以评估包含此字段的csv文件。

  private void button2_Click(object sender, EventArgs e)
  {
    DateTime start = new DateTime(01 / 25 / 2015);
    DateTime end = new DateTime(01 / 27 / 2015);

    var dates = new List<DateTime>();

    for (var dt = start; dt <= end; dt = dt.AddDays(1))
    {
      dates.Add(dt);

      var oldLines = System.IO.File.ReadAllLines(txtFileName.Text);
      var newLines = oldLines.Where(line => 
      line.Contains(dates.ToString()));          
      System.IO.File.WriteAllLines(txtFileName.Text, newLines);

      newLines = oldLines.Select(line => new

        {
            Line = line,
            Words = line.Split(' ')
        })
         .Where(lineInfo => lineInfo.Words.Contains(dates.ToString()))
         .Select(lineInfo => lineInfo.Line);
     }
  } 

INPUT:

&#34; EmployeeCode&#34;&#34;日期&#34;&#34;时间&#34;&#34;类型&#34;
&#34; 3434&#34;&#34 01 /2013分之22&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 3023&#34;&#34 01 /2014分之23&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 2897&#34;&#34 01 /2015分之24&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 3734&#34;&#34 01 /2015分之25&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 3168&#34;&#34 01 /2015分之26&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 4863&#34;&#34 01 /2015分之26&#34;&#34; 07:55&#34;&#34; 0&#34;
&#34; 2513&#34;&#34 01 /2015分之27&#34;&#34; 07:55&#34;&#34; 0&#34;
&#34; 2582&#34;&#34 01 /二千〇一十五分之二十七&#34;&#34; 07:55&#34;&#34; 0&#34;

输出:

&#34; EmployeeCode&#34;&#34;日期&#34;&#34;时间&#34;&#34;类型&#34;
&#34; 3734&#34;&#34 01 /2015分之25&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 3168&#34;&#34 01 /2015分之26&#34;&#34; 07:54&#34;&#34; 0&#34;
&#34; 4863&#34;&#34 01 /2015分之26&#34;&#34; 07:55&#34;&#34; 0&#34;
&#34; 2513&#34;&#34 01 /2015分之27&#34;&#34; 07:55&#34;&#34; 0&#34;
&#34; 2582&#34;&#34 01 /二千〇一十五分之二十七&#34;&#34; 07:55&#34;&#34; 0&#34;

建议,评论,分享代码和想法受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

更典型的方法是查看所有行,提取相关部分,将它们转换为实际日期,并将它们与常规不等式的开始和结束日期进行比较。

Public blResume As Boolean
Dim NextTick As Date, t As Date
Sub StartStopWatch()
t = Time
Call StartTimer
End Sub

Sub StartTimer()
NextTick = Time + TimeValue("00:00:01")
Range("M1").Value = Format(NextTick - t - TimeValue("00:00:01"), "hh:mm:ss")
If blResume = False Then Exit Sub
Application.OnTime NextTick, "StartTimer"
End Sub
Sub Pause()
    blResume = False
End Sub
Sub myResume()
    blResume = True
    StartTimer
End Sub

这不考虑标题行,但您可以单独处理。