When I open a Worksheet I calculate the number of rows in the sheet as follows:

int rows = ws.Cells.MaxRow - ws.Cells.MinRow;

This works almost all the time. I have one spreadsheet that is returning a MaxRow value of 1574 and a MinRow value of 1. This results in a rows value of 1573, which is incorrect. The total number of rows is 1574.

What am I doung wrong? Should I not be using the formula above for retrieving the number of rows and just use MaxRows?




MaxRow and MinRow properties return the max and min row index. So the row number should be:

int rows = ws.Cells.MaxRow - ws.Cells.MinRow + 1;

I think in your file, generally the MinRow returns 0. So your code works in most of time but it fails when MinRow doesn't return 0.


Should I use the same formula to calculate columns?

int cols = ws.Cells.MaxColumn + ws.Cells.MinColumn + 1; <== ???

I haven’t had the issue with Column yet, but should I change the formula?



Can MinRow ever return > 1? Would a formulat like this:

int rows = ws.Cells.MaxRow + ws.Cells.MinRow;

work better in most cases?

Same question for MinColumn…



Please use:

int rows = ws.Cells.MaxRow - ws.Cells.MinRow + 1;
int cols = ws.Cells.MaxColumn - ws.Cells.MinColumn + 1;


Ok, I implemented your suggestion. But…

In most cases, all but one, when I load the worksheet into my Grid I am getting an extra blank column and row shown. Should I only add one if the Min values are greater than 0?



Could you please post your file and sample code here? Is the extra blank column and row show at the start of your grid?


No, it shoes at the end of the Grid (right and bottom).


You may set style in the end of row or column in your excel file. Please delete the blank end row/column in your excel file.


I have a spreadsheet that has 1655 rows. The MaxRow is returning 1654. The MinRow is returning 3.

The formula MaxRow - MinRow + 1 doesn't work. Why is MinRow 3??

I can't attach the spreadsheet as it contains sensitive data.

Any help ASAP would be appreciated.


-Steven Perry


Cells.MinRow actually returns the row index of the first cell object: cells[0].Row.