Search code examples
c#asp.netopenxml

Get second last row of a table


If getting the first/last row is as following:

TableRow lastRow = theTable.Elements<TableRow>().Last();

TableRow firstRow = theTable.Elements<TableRow>().First();

How to select the second last row?


Solution

  • Try this:

    TableRow firstRow = theTable.Elements<TableRow>().Reverse().Skip(1).First();
    

    Note, that this expression can throw an exception if no suitable row is available (also Last and First).