Ottenere l indice delle celle

Possibili Scenari di Utilizzo

Quando hai bisogno solo di manipolare un dato specifico nel foglio di lavoro per indice di riga e colonna, devi conoscere gli indici di riga e colonna di quella cella specifica. Aspose.Cells offre questa funzione per ottenere l’indice di riga e colonna per il nome della riga, colonna e cella. Aspose.Cells fornisce le seguenti proprietà e metodi per aiutarti a raggiungere i tuoi obiettivi.

Nota: L’indicizzazione è a base zero in Aspose.Cells per .Net, ma l’id della riga è a base uno in MS Excel.

Ottieni Gli Indici delle Celle utilizzando Aspose.Cells

Questo esempio mostra come:

  1. Creare un workbook e aggiungere alcuni dati.
  2. Trova la cella specifica nel primo foglio di lavoro.
  3. Ottieni l’indice di riga e l’indice di colonna per nome della cella.
  4. Ottieni l’indice di colonna per nome della colonna.
  5. Ottieni l’indice di riga per nome della riga.
//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);