Free Support Forum -

Get total rows and columns in Excel worksheet in .NET


I am trying to load an excel using Aspose.

Problem, I am not getting the column count which gives 0 but i can loop through the cells which has 10 rows for my excel.

Why am I not getting the column count?


Yes, I can notice the problem with the latest version:
for .NET v6.0.1.6

However, you can get the right column count using the last cell as shown below.

We will fix the issue asap.

This bug has been logged as CELLSNET-29822.

I have also attached the source file and you can see the output below


string path = @“F:\Shak-Data-RW\Downloads\source.xlsx”;

Workbook workbook = new Workbook(path);

Worksheet worksheet = workbook.Worksheets[0];


Cell lastCell = worksheet.Cells.LastCell;

int columnCount = lastCell.Column + 1;



Moreover, Cells.Columns.Count property would give you columns count but, the columns with standard (default) settings / formatting are not included to be counted. For your information, the Rows.Count and Columns.Count are mainly useful if you want to retrieve the number of rows / columns which are initialized / used in a worksheet and also those rows / columns other than with default formatting/setting in a worksheet.

Please always use Cells.MaxDataColumn / Cells.MaxDataRow and Cells.MaxColumn/Cells.MaxRow attributes to get the farthest column and row indexes. So, you should change your sample code accordingly.

For complete reference for using these attributes and others in that series, please check the thread:

If you still have any confusion, let us know.