Obtener el índice de celdas
Escenarios de uso posibles
Cuando solo necesita manipular un dato específico en la hoja de cálculo por índice de fila y columna, necesita saber los índices de fila y columna de esa celda específica. Aspose.Cells ofrece esta funcionalidad para obtener el índice de fila y columna por el nombre de la fila, columna y celda. Aspose.Cells proporciona las siguientes propiedades y métodos para ayudarlo a alcanzar sus metas.
- CellsHelper.CellNameToIndex
- CellsHelper.ColumnIndexToName
- CellsHelper.ColumnNameToIndex
- CellsHelper.RowIndexToName
- CellsHelper.RowNameToIndex
Nota: El indexado es basado en cero en Aspose.Cells para .Net, pero el id de la Fila es basado en uno en MS Excel.
Obtener Índices de Celdas usando Aspose.Cells
Este ejemplo muestra cómo:
- Crear un libro de trabajo y agregar algunos datos.
- Encontrar la celda específica en la primera hoja de trabajo.
- Obtener el índice de fila e índice de columna por el nombre de la celda.
- Obtener el índice de columna por el nombre de la columna.
- Obtener el índice de fila por el nombre de la fila.
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.Worksheets[0]; | |
Cells cells = ws.Cells; | |
//Setting the value to the cells | |
Cell cell = cells["A1"]; | |
cell.PutValue("Fruit"); | |
cell = cells["B1"]; | |
cell.PutValue("Count"); | |
cell = cells["C1"]; | |
cell.PutValue("Price"); | |
cell = cells["A2"]; | |
cell.PutValue("Apple"); | |
cell = cells["A3"]; | |
cell.PutValue("Mango"); | |
cell = cells["A4"]; | |
cell.PutValue("Blackberry"); | |
cell = cells["A5"]; | |
cell.PutValue("Cherry"); | |
cell = cells["B2"]; | |
cell.PutValue(5); | |
cell = cells["B3"]; | |
cell.PutValue(3); | |
cell = cells["B4"]; | |
cell.PutValue(6); | |
cell = cells["B5"]; | |
cell.PutValue(4); | |
cell = cells["C2"]; | |
cell.PutValue(5); | |
cell = cells["C3"]; | |
cell.PutValue(20); | |
cell = cells["C4"]; | |
cell.PutValue(30); | |
cell = cells["C5"]; | |
cell.PutValue(60); | |
Cell curr = cells.Find("Blackberry", null); | |
int currRow; | |
int currCol; | |
//get row and column index of current cell | |
CellsHelper.CellNameToIndex(curr.Name, out currRow, out currCol); | |
Console.WriteLine("Row Index: " + currRow + " Column Index: " + currCol); | |
//get column name by column index | |
string columnName = CellsHelper.ColumnIndexToName(currCol); | |
//get row name by row index | |
string rowName = CellsHelper.RowIndexToName(currRow); | |
Console.WriteLine("Column Name: " + columnName + " Row Name: " + rowName); | |
//get column index by column name | |
int columnIndex = CellsHelper.ColumnNameToIndex(columnName); | |
//get row index by row name | |
int rowIndex = CellsHelper.RowNameToIndex(rowName); | |
Console.WriteLine("Column Index: " + columnIndex + " Row Index: " + rowIndex); | |
Assert.AreEqual(columnIndex, currCol); | |
Assert.AreEqual(rowIndex, currRow); |