Search code examples
c#.netexcelaspose-cells

ASPOSE: How to correctly get Total Rows in Worksheet


Currently using Aspose for .NET in a C# WebService. I am currently having issues getting the correct Total Rows using worksheet.Cells.Rows.Count It is showing that I have around 230 rows but inside the file, I only have 14 rows. I am guessing because I reuse my Excel file and deleted some rows by highlighting the rows then pressing the delete key.

When I recreate a brand new Excel file, then copy-paste the 14 values in the new file. The worksheet.Cells.Rows.Count works correctly.

Would this be a bug in the library or am I deleting the values in Excel file wrongly or I am using a wrong count property?

Am I missing something?

TIA


Solution

  • Well, RowCollection/ColumnCollection.Count attribute would give you total number of rows/columns which are initialized, so it won't be always reliable to get total number of data rows/cols. I think you should use Worksheet.Cells.MaxDataRow and Worksheet.Cells.MaxDataColumn properties to get the farthest (last) row/column indices (0 - based), see the sample code segment for your reference: e.g Sample code:

        Workbook workbook = new Workbook("Book1.xlsx");
         //Get the farthest (last) row's index (zero-based) which contains data.
         int lastRowIndex = workbook.Worksheets[0].Cells.MaxDataRow;
    
         //Get the farthest (last) column's index (zero-based) which contains data.
         int lastColIndex = workbook.Worksheets[0].Cells.MaxDataColumn;
    ......
    

    Hope, this helps a bit.

    PS. I am working as Support developer/ Evangelist at Aspose.