Search code examples
javaexcelaspose

Aspose ColumnCollection getCount() exact meaning


Aspose

    //Instantiating a Workbook object
    Workbook workbook = new Workbook();        
    //Obtaining the reference of the first worksheet
    Worksheet worksheet = workbook.getWorksheets().get(0);
    ColumnCollection columns = worksheet.getCells().getColumns();
    int count = columns.getCount();
  1. what is the value of columns.getCount() here meant? the maximum column count in a worksheet?
  2. if I want to know the column count of each row in a worksheet, how to do in aspose? (e.g. row1 has 10 columns, row 2 has 5 columns...)

Solution

  • 1.) Well, ColumnCollection/RowCollection.getCount() would give you rows/cols count which are initialized in the worksheet, so it may include cells even if they are empty or with null values. I think you should use Cells.getMaxDataRow and Cells.getMaxDataColumn methods to get the farthest (last) row/column indices (zero-based), see the sample code segment for your reference: e.g

    Sample code:

    Workbook workbook = new Workbook("Book1.xlsx", new LoadOptions());
    //Get the farthest (last) row's index (zero-based) which contains data.
    int lastRowIndex = workbook.getWorksheets().get(0).getCells().getMaxDataRow();
    //Get the farthest (last) column's index (zero-based) which contains data.
    int lastColIndex = workbook.getWorksheets().get(0).getCells().getMaxDataColumn();
    

    2.) You may use Cells.endCellInRow attribute to get the last cell in a row (you may evaluate the column index of the resultant cell). See the sample code. e.g

    Sample code:

    Worksheet worksheet = workbook.getWorksheets().get(0);
    Cells cells = worksheet.getCells();     
    //Get last cell in the 4th row
    Cell lastCellOutput = cells.endCellInRow(3);
    //Get the column index of the cell.
    int col = lastCellOutput.getColumn();
    

    Hope, this helps a bit.

    For detailed questions about Aspose APIs, you may post here.

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