行内の最大列インデックスと列内の最大行インデックスの取得

可能な使用シナリオ

行または列上の一部のデータを操作する必要がある場合、行と列のデータ範囲を知る必要があります。Aspose.Cellsはこの機能を提供しています。行の最大列インデックスを取得するには、Row.LastCell および Row.LastDataCell プロパティを取得し、その後、 Cell.Column プロパティを使用して最大列インデックスおよび最大データ列インデックスを取得します。ただし、列の最大行インデックスと最大行データインデックスを取得するには、列に範囲を作成して、範囲を走査して最後のセルを検索し、最終的に Cell.Row 属性を取得する必要があります。

Aspose.Cellsは、目標を達成するための以下のプロパティとメソッドを提供しています。

Aspose.Cellsを使用して行内の最大列インデックスと列内の最大行インデックスの取得

この例では、次のことができます:

  1. サンプルファイルをロードする。
  2. 最大列インデックスと最大データ列インデックスを取得する行を取得します。
  3. セル上のCell.Column属性を取得します。
  4. 列に基づいて範囲を作成します。
  5. イテレータを取得して範囲をトラバースします。
  6. セル上のCell.Row属性を取得します。
Workbook workbook = new Workbook(filePath + "sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;
Row row = cells.CheckRow(1);
if (row != null)
{
//get Maximum column index of Row which contains data or style.
Console.WriteLine("Max column index in row: " + row.LastCell.Column);
//get Maximum column index of Row which contains data.
Console.WriteLine("Max data column index in row: " + row.LastDataCell.Column);
}
// create the range of column B
Range columnRange = cells.CreateRange(1, 1, true);
IEnumerator colIter = columnRange.GetEnumerator();
int maxRow = 0;
int maxDataRow = 0;
while (colIter.MoveNext())
{
Cell currCell = (Cell)colIter.Current;
if (!string.IsNullOrEmpty(currCell.StringValue))
{
maxDataRow = currCell.Row;
}
if (!string.IsNullOrEmpty(currCell.StringValue) || currCell.HasCustomStyle)
{
maxRow = currCell.Row;
}
}
//Maximum row index of Column which contains data or style.
Console.WriteLine("Max row index in Column: " + maxRow);
//Maximum row index of Column which contains data.
Console.WriteLine("Max data row index in Column: " + maxDataRow);