1

I'm using ExcelDataReader to read .xlsx files.

using (var reader = ExcelReaderFactory.CreateReader(stream))
{
    for (bool canRead = true; canRead; canRead = reader.NextResult())
    {
        //reader.Read(); many times
    }
}

How to get the index of the row I'm currently reading? In other words, how many times reader.Read() has been called.

havon
  • 21
  • 3
  • Declare a `int` variable before the `for` and increment it inside the `for`. – mjwills Sep 03 '20 at 04:00
  • @mjwills, `reader.Read()` may be called many times every iteration. I know I can use some counters and constants, but it's inconvenient to modify. – havon Sep 03 '20 at 04:06

1 Answers1

0

You can try this

using (var stream = File.Open("Book1.xls", FileMode.Open, FileAccess.Read))
{
    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        do
        {
            while (reader.Read())
            {
                Console.WriteLine($"Row: {reader.Depth} Values: {reader[0]}, {reader[1]}");
            }
        } while (reader.NextResult());
    }
}

Output
enter image description here

tontonsevilla
  • 2,649
  • 1
  • 11
  • 18