Get the number of rows of data with SpreadSheetGea

2020-04-02 16:59发布

问题:

I've checked a few online resources, maybe I'm blind but I've as yet been unable to find an answer to this.

I'm uploading a file, converting it to a stream, feeding it into SpreadSheetGear. Now, I need to loop through every row and read the data (which is fine). Here is my code so far:

IWorkbook wb = Factory.GetWorkbookSet().Workbooks.OpenFromStream(file.InputStream);
IWorksheet ws = wb.ActiveWorksheet;
IRange cells = ws.Cells;

for (int i = 2; i <= cells.RowCount; i++)
{
    //Code for every row
    for (int x = 1; x <= cells.ColumnCount; x++)
    {
         //Code for every column
    }
}

Here is the problem: cells.RowCount is equal to 1048576 which is clearly the Excel upper limit on number of rows. Is there a call to SpreadSheetGear that returns the number of rows that have data? I cant use a fixed amount as the spreadsheet provided could have anything from 500 to 2,000 rows.

I understand SpreadSheetGear may not be that widely used but I thought I'd chance my arm here anyway.

Cheers!

Edit :

Before somebody says use a while loop, it's possible that a row can be empty so checking for null strings is a bit of a messy one.

回答1:

Answered my own question, always the way.

Anyway, eventually found IWorksheet.UsedRange which returns just the used cells.