在行C#之后停止读取CSV文件

时间:2017-11-03 19:54:30

标签: c# csv

我有一些代码,我开始在某一行之后读取一个CSV文件。我想知道是否有办法在某一行之后也停止读取CSV文件。或者,有没有办法在我读取特定字符串后停止读取CSV文件。在某一行之前停止会更好但是。让我知道你的想法。如果有办法的话。

Dim Cl As Range
Dim SrcWbk As Workbook
Dim SrcSht As Worksheet
Dim DestSht As Worksheet
Dim Rng As Range

Application.ScreenUpdating = False

Set SrcWbk = Workbooks.Open("Transactional Activity PD 10-2017 (Expense Accounts).xlsb")
Set SrcSht = SrcWbk.Sheets("Activity")
Set DestSht = ThisWorkbook.Sheets("Transactions")

With CreateObject("scripting.dictionary")

    For Each Cl In DestSht.Range("AE2", DestSht.Range("AE" & Rows.Count).End(xlUp))
        If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing
    Next Cl

    For Each Cl In SrcSht.Range("AE2", SrcSht.Range("AE" & Rows.Count).End(xlUp))

        If Not .exists(Cl.Value) And Cl.Offset(, -29).Value = "PV" And _
        InStr(Cl.Offset(, -19), "utilities") > 0 Then
            If Rng Is Nothing Then
                Set Rng = Cl
            Else
                Set Rng = Union(Rng, Cl)
            End If
        End If

    Next Cl

End With

Rng.EntireRow.Copy DestSht.Range("A" & Rows.Count).End(xlUp).Offset(1)

End Sub

2 个答案:

答案 0 :(得分:2)

public IEnumerable<ResultantRead> processCSVFile()
{
    return File.ReadLines("C:/Users")
                    .Skip(54);
                    .Select(v => ResultantRead.readCsv(v));
}

//calling code
var result = processCSVFile().Take(5).ToList();
// result will only enumerate 59 rows (or less) from the file

答案 1 :(得分:0)

alist = File.ReadLines("C:/Users").take(250);

我认为这会告诉它在250行之后停止,但是我不确定如何跳过,如同,它会跳过54行然后接下来250行或者取250行并跳过54行。

我会尝试一下