セルのインデックスの取得
Contents
[
Hide
]
ExcelのデフォルトビューはA1形式の参照です。各列はA、B、C….と定義され、セルはA1、B2、C3…などと名前が付けられます。
このセルがどの行や列にあるか知りたい場合があるかもしれません。
可能な使用シナリオ
ワークシート上の特定のデータを列や行インデックスで操作する必要がある場合、その特定のセルの列と行のインデックスを知る必要があります。 Aspose.Cellsでは、行、列、セルの名前で行や列のインデックスを取得する機能が提供されています。 Aspose.Cellsは、目標を達成するための以下のプロパティとメソッドを提供しています。
- CellsHelper.CellNameToIndex
- CellsHelper.ColumnIndexToName
- CellsHelper.ColumnNameToIndex
- CellsHelper.RowIndexToName
- CellsHelper.RowNameToIndex
注意: Aspose.Cells for .Net ではインデックスは 0 から始まりますが、MS Excel では行の id が 1 から始まります。
Aspose.Cells を使用してセルのインデックスを取得
この例では、次のことができます:
- ワークブックを作成し、いくつかのデータを追加します。
- 最初のワークシートで特定のセルを見つけます。
- セルの名前によって行インデックスと列インデックスを取得します。
- 列の名前によって列インデックスを取得します。
- 行の名前によって行インデックスを取得します。
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
//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); |