获取单元格索引
Contents
[
Hide
]
Excel 的默认视图是 A1 样式引用,每列定义为 A、B、C……,单元格命名为 A1、B2、C3……等等。
您可能想知道该单元格位于哪一行和列。
可能的使用场景
当您只需通过行和列索引操作工作表上的特定数据时,您需要了解该特定单元格的行索引和列索引。 Aspose.Cells for Python via .NET 提供了通过行、列和单元格的名称获取行和列索引的功能。 Aspose.Cells for Python via .NET提供以下属性和方法,以帮助您实现您的目标。
- CellsHelper.cell_name_to_index
- CellsHelper.column_index_to_name
- CellsHelper.column_name_to_index
- CellsHelper.row_index_to_name
- CellsHelper.row_name_to_index
注意:在Aspose.Cells for Python via .NET中,索引是以零为基础的,但是MS Excel中的行标识是以一为基础的。
使用Aspose.Cells for Python Excel库获取单元格索引
此示例演示如何:
- 创建一个工作簿并添加一些数据。
- 在第一个工作表中查找特定单元格。
- 通过单元格名称获取行索引和列索引。
- 通过列名获取列索引。
- 通过行名获取行索引。
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
from aspose.cells import Workbook, CellsHelper | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the newly added worksheet | |
ws = workbook.worksheets[0] | |
cells = ws.cells | |
# Setting the value to the cells | |
cell = cells.get("A1") | |
cell.put_value("Fruit") | |
cell = cells.get("B1") | |
cell.put_value("Count") | |
cell = cells.get("C1") | |
cell.put_value("Price") | |
cell = cells.get("A2") | |
cell.put_value("Apple") | |
cell = cells.get("A3") | |
cell.put_value("Mango") | |
cell = cells.get("A4") | |
cell.put_value("Blackberry") | |
cell = cells.get("A5") | |
cell.put_value("Cherry") | |
cell = cells.get("B2") | |
cell.put_value(5) | |
cell = cells.get("B3") | |
cell.put_value(3) | |
cell = cells.get("B4") | |
cell.put_value(6) | |
cell = cells.get("B5") | |
cell.put_value(4) | |
cell = cells.get("C2") | |
cell.put_value(5) | |
cell = cells.get("C3") | |
cell.put_value(20) | |
cell = cells.get("C4") | |
cell.put_value(30) | |
cell = cells.get("C5") | |
cell.put_value(60) | |
curr = cells.find("Blackberry", None) | |
# get row and column index of current cell | |
currRow = [] | |
currCol = [] | |
CellsHelper.cell_name_to_index(curr.name, currRow, currCol) | |
print("Row Index: " + str(currRow[0]) + " Column Index: " + str(currCol[0])) | |
# get column name by column index | |
columnName = CellsHelper.column_index_to_name(currCol[0]) | |
# get row name by row index | |
rowName = CellsHelper.row_index_to_name(currRow[0]) | |
print("Column Name: " + columnName + " Row Name: " + rowName); | |
# get column index by column name | |
columnIndex = CellsHelper.column_name_to_index(columnName); | |
# get row index by row name | |
rowIndex = CellsHelper.row_name_to_index(rowName) | |
print("Column Index: " + str(columnIndex) + " Row Index: " + str(rowIndex)) |