Search code examples
gembox-spreadsheet

Gembox get count of all rows (including blank) of specfic column


I am trying to get count of last used row for a specific column, but was only able to get count of max rows occupied. Test data is shown in attached snap. Please help. Snap Attached Here

        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
        ExcelFile ef = ExcelFile.Load("test.xlsx");

        CellRange rcount = ef.Worksheets[0].Columns[1].Cells;
        Console.WriteLine(ef.Worksheets[0].Rows.Count);


        ef.Save("test.xlsx");
        Console.ReadKey();  

Solution

  • Cells are internally allocated in rows, not in columns. In other words, you could get the last used column for each row with ExcelRow.AllocatedCells.Count.

    For getting the last used row in a specific column you can use something like the following:

    ExcelFile ef = ExcelFile.Load("test.xlsx");
    ExcelWorksheet ws = ef.Worksheets[0];
    
    ExcelColumn column = ws.Columns[1];
    int rowIndex = ws.Rows.Count - 1;
    
    while (rowIndex >= 0 && column.Cells[rowIndex].ValueType == CellValueType.Null)
        --rowIndex;
    
    Console.WriteLine(rowIndex);
    

    I hope this helps.