获取行中最大列索引和列中最大行索引
Contents
[
Hide
]
可能的使用场景
当您只需操纵某些行或列上的数据时,需要知道行和列的数据范围。Aspose.Cells 提供此功能。要获取行中的最大列索引,您可以获取 Row.LastCell 和 Row.LastDataCell 属性,然后使用 Cell.Column 属性来获取最大列索引和最大数据列索引。但是,为了获取列中的最大行索引和最大行数据索引,您需要在列上创建一个范围,然后遍历范围找到最后一个单元格,最后获取单元格上的 Cell.Row 属性。
Aspose.Cells 提供以下属性和方法,帮助您实现目标。
使用 Aspose.Cells 获取行中的最大列索引和列中的最大行索引
此示例演示如何:
- 加载示例文件。
- 获取需要获取最大列索引和最大数据列索引的行。
- 获取单元格上的 Cell.Column 属性。
- 根据列创建一个范围。
- 获取迭代器并遍历范围。
- 获取单元格上的 Cell.Row 属性。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |