首页 > 解决方案 > Aspose ColumnCollection getCount() 确切含义

问题描述

假设

    //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. columns.getCount() 这里的值是什么意思?工作表中的最大列数?
  2. 如果我想知道工作表中每一行的列数,怎么办?(例如第 1 行有 10 列,第 2 行有 5 列...)

标签: javaexcelaspose

解决方案


1.) 好吧,ColumnCollection/RowCollection.getCount() 会为您提供在工作表中初始化的行/列计数,因此即使它们为空或具有空值,它也可能包括单元格。我认为您应该使用 Cells.getMaxDataRow 和 Cells.getMaxDataColumn 方法来获取最远(最后)行/列索引(从零开始),请参阅示例代码段以供参考:例如

示例代码:

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.) 您可以使用 Cells.endCellInRow 属性来获取一行中的最后一个单元格(您可以评估结果单元格的列索引)。请参阅示例代码。例如

示例代码:

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();

希望这个对你有帮助。

有关 Aspose API 的详细问题,您可以在此处发布

PS。我在 Aspose 担任支持开发人员/传播者。


推荐阅读