How do I iterate through rows in an excel table using epplus?

后端 未结 6 1296
暖寄归人
暖寄归人 2021-01-30 20:19

I am new to epplus, and i\'m trying to read some values from an excel table.

This is what I have so far:

var fileInfo = new FileInfo(filename);
using(var e         


        
6条回答
  •  独厮守ぢ
    2021-01-30 20:34

    You can access the .Worksheet property of a table and index its cells. I wrote an extension method for this purpose, which generates a series of dictionaries mapping column name to cell value:

    public static IEnumerable> GetRows(this ExcelTable table)
    {
        var addr = table.Address;
        var cells = table.WorkSheet.Cells;
    
        var firstCol = addr.Start.Column;
    
        var firstRow = addr.Start.Row;
        if (table.ShowHeader)
            firstRow++;
        var lastRow = addr.End.Row;
    
        for (int r = firstRow; r <= lastRow; r++)
        {
            yield return Enumerable.Range(0, table.Columns.Count)
                .ToDictionary(x => table.Columns[x].Name, x => cells[r, firstCol + x].Value);
        }
    }
    

提交回复
热议问题