When iterating over all cells of a column, some cells are skipped (e.g. row 6..11).
I tried to reproduce the error in a test method but was not successful.
The sourcecode of the Enumerator is very complex and the Enumerator is instantiated in the ExcelRangeBase class. This means only one iterator for a range is possible at the same time.
If you pass the range to a method and do a nested foreach loop there, then you will get errors.
I could not find this situation in my code but still could fix the Situation by implementing a simple iterator class for my purpose (see below):
private class RowColEnumerator : IEnumerable, IEnumerator
{
bool m_rowEnumerator;
int m_offset;
int m_row;
int m_column;
ExcelWorksheet m_worksheet;
public RowColEnumerator(bool rowEnumerator, int row, int col, ExcelWorksheet worksheet)
{
m_rowEnumerator = rowEnumerator;
m_offset = -1;
m_row = row;
m_column = col;
m_worksheet = worksheet;
}
public IEnumerator GetEnumerator()
{
return this;
}
public object Current
{
get
{
if (m_rowEnumerator)
{
return m_worksheet.Cells[m_row + m_offset, m_column];
}
else
{
return m_worksheet.Cells[m_row, m_column + m_offset];
}
}
}
public bool MoveNext()
{
m_offset++;
return m_offset < 32000;
}
public void Reset()
{
m_offset = -1;
}
}
I tried to reproduce the error in a test method but was not successful.
The sourcecode of the Enumerator is very complex and the Enumerator is instantiated in the ExcelRangeBase class. This means only one iterator for a range is possible at the same time.
If you pass the range to a method and do a nested foreach loop there, then you will get errors.
I could not find this situation in my code but still could fix the Situation by implementing a simple iterator class for my purpose (see below):
private class RowColEnumerator : IEnumerable, IEnumerator
{
bool m_rowEnumerator;
int m_offset;
int m_row;
int m_column;
ExcelWorksheet m_worksheet;
public RowColEnumerator(bool rowEnumerator, int row, int col, ExcelWorksheet worksheet)
{
m_rowEnumerator = rowEnumerator;
m_offset = -1;
m_row = row;
m_column = col;
m_worksheet = worksheet;
}
public IEnumerator GetEnumerator()
{
return this;
}
public object Current
{
get
{
if (m_rowEnumerator)
{
return m_worksheet.Cells[m_row + m_offset, m_column];
}
else
{
return m_worksheet.Cells[m_row, m_column + m_offset];
}
}
}
public bool MoveNext()
{
m_offset++;
return m_offset < 32000;
}
public void Reset()
{
m_offset = -1;
}
}