开发者

How to By pass first Row on Excel in foreach loop?

I have this Code for Reading Excell Records:

public IEnumerable<FillinEntity> Map(IEnumerable<ExcelRow> excelRows)
            {

                List<FillinEntity> fillinEntities = new List<FillinEntity>();

                foreach (ExcelRow row in excelRows)
                {

                    FillinEntity excell = new FillinEntity();
                    excell.SerialNumber = Convert.ToString(row.Cells[0]);
                    excell.PalletNumber = Convert.ToString(row.Cells[1]);
                    excell.Location = Convert.ToString(row.Cells[2]);
                    excell.CreatedBy = Convert.ToString(row.Cells[3]);

                    fillinEntities.Add(excell);

                }

                retu开发者_开发技巧rn fillinEntities;
            }

I have this records: And it succesfully inserted

R03091294   2    2  FGROOM  RYAN

My Problem: I Add column header on the excell sheet.

Serial Number    Pallet      Location   CreatedBy -----> i need to by pass column header.
R03091294   2    2         FGROOM     RYAN

Thanks in regards


You could always just skip it:

foreach (ExcelRow row in excelRows.Cast<ExcelRow>().Skip(1))

See Skip().

Note: I used Cast<ExcelRow>() in case your enumerable excelRows can't be resolved to ExcelRow.


bool is_first_row = True;
foreach (ExcelRow row in excelRows)
{
   if(is_first_row)
   {
      is_first_row = false;
      continue;
   }
    ...
}
....

.Skip(1)

solution provided by Codesleuth is much better option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜