如何从 vb.net openxml IEnumerable(Of Row) 对象中删除一系列行?

How to remove a range of rows from a vb.net openxml IEnumerable(Of Row) object?

我在 vb.net 项目中使用 openxml 将具有大量行的 Excel 文件中的数据解析为 DataTables 对象。 excel 文件中的每一行都被解析并作为 DataRow 对象添加到 DataTable 中。

如果作业失败,我想跳过所有已解析的行。但是,我仍然需要解析第一行,因为它用于设置 DataTable 中的列名。

我正在寻找一种方法来删除 openxml IEnumerable(Of Row) 对象中的第 2 行到第 X 行。

    'open the excel doc
    Dim doc As SpreadsheetDocument = SpreadsheetDocument.Open(FilePath, False)

    'Read the first Sheet from Excel file.
    Dim sheet As Sheet = doc.WorkbookPart.Workbook.Sheets.GetFirstChild(Of Sheet)()

    'Get the Worksheet instance.
    Dim worksheet As Worksheet = TryCast(doc.WorkbookPart.GetPartById(sheet.Id.Value), WorksheetPart).Worksheet

    'Fetch all the rows present in the Worksheet. 
    Dim rows As IEnumerable(Of Row) = worksheet.GetFirstChild(Of SheetData)().Descendants(Of Row)()

    ' set a starting row value
    Dim startingRow as Integer = 125000

    'Remove rows 2 through startingRow from the "rows" IEnumerable(Of Row) variable..
    '** this is where I am stuck!!!!!!!!!!!!!!!!

仅供参考,C# 被添加为标签,因为那里不再有那么多活跃的 VB 程序员。我很乐意将任何 C# solutions/tips 转换为 VB.

而不是从只读 IEnumerable 中“删除”行,一种更直接的方法是迭代您 do 想要的行,即第 1 行,然后是第 X+1 行到末尾:

  rows = rows.Take(1).Concat(rows.Skip(startingRow))