Free Support Forum - aspose.com

Get total rows and columns in Excel worksheet in .NET

Hi,

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?

Hi,

Yes, I can notice the problem with the latest version:
Aspose.Cells
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

C#

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


Workbook workbook = new Workbook(path);


Worksheet worksheet = workbook.Worksheets[0];


Debug.WriteLine(worksheet.Cells.Columns.Count);


Cell lastCell = worksheet.Cells.LastCell;


int columnCount = lastCell.Column + 1;


Debug.WriteLine(columnCount);


Output:
0
2
Hi,

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: http://www.aspose.com/community/forums/178764/column-and-row-count-invalid/showthread.aspx#178764

If you still have any confusion, let us know.