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

后端 未结 6 1231
暖寄归人
暖寄归人 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:40

    I had the same issue and I solved it using the ExcelTable to get the table boundary and the ExcelWorksheet to retrieve the data. So your code will look something like this:

    var fileInfo = new FileInfo(filename);
    using(var excelPackage = new OfficeOpenXml.ExcelPackage(fileInfo))
    {
        foreach (var sheet in excelPackage.Workbook.Worksheets)
        {
            foreach (ExcelTable table in sheet.Tables)
            {
                ExcelCellAddress start = table.Address.Start;
                ExcelCellAddress end = table.Address.End;
    
                for (int row = start.Row; row <= end.Row; ++row)
                {
                    ExcelRange range = sheet.Cells[row, start.Column, row, end.Column];
                    ...
                }
            }
        }
    }
    

    You need to check for table header or other things, but that did the trick for me.

提交回复
热议问题