範囲の管理

紹介

Excelでは、マウスボックス選択で複数のセルを選択することができ、選択されたセルのセットを「範囲」と呼びます。

たとえば、Excelのセル"A1"で左ボタンをクリックしてからセル"C4"までドラッグすることができます。選択した長方形の領域は、Aspose.Cellsを使用してオブジェクトとして簡単に作成することもできます。

範囲を作成し、値を設定し、スタイルを設定し、“範囲"オブジェクトにさらなる操作を行う方法

Aspose.Cellsを使用した範囲の管理

Aspose.Cellsは、Microsoft Excelファイルを表すクラス、Workbookを提供しています。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。WorksheetクラスにはCellsコレクションが提供されています。

範囲の作成

A1:C4にまたがる長方形領域を作成する場合は、次のコードを使用できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Create a Workbook
Aspose.Cells.Workbook workbook = new Workbook();
//Get Cells
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
//Create Range
Aspose.Cells.Range range = cells.CreateRange("A1:C4");

範囲のセルに値を入力する

A1:C4にまたがるセルの範囲があるとします。行列は4*3=12セルを作ります。それぞれの範囲セルは順に配置されます: Range[0,0]、Range[0,1]、Range[0,2]、Range[1,0]、Range[1,1]、Range[1,2]、Range[2,0]、Range[2,1]、Range[2,2]、Range[3,0]、Range[3,1]、Range[3,2]。

次の例は、範囲のセルに値を入力する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Create a Workbook
Aspose.Cells.Workbook workbook = new Workbook();
//Get Cells
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
//Create Range
Aspose.Cells.Range range = cells.CreateRange("A1:C4");
//Put value
range[0, 0].PutValue("A1");
range[0, 1].PutValue("B1");
range[0, 2].PutValue("C1");
range[3, 0].PutValue("A4");
range[3, 1].PutValue("B4");
range[3, 2].PutValue("C4");
//Save the Workbook
workbook.Save("RangeValueTest.xlsx");

範囲のセルのスタイルを設定する

次の例は、セルの範囲のスタイルを設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Create a Workbook
Aspose.Cells.Workbook workbook = new Workbook();
//Get Cells
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
//Create Range
Aspose.Cells.Range range = cells.CreateRange("A1:C4");
//Put value
range[0, 0].PutValue("A1");
range[3, 2].PutValue("C4");
//Set Style
Style style00 = workbook.CreateStyle();
style00.Pattern = BackgroundType.Solid;
style00.ForegroundColor = System.Drawing.Color.Red;
range[0, 0].SetStyle(style00);
Style style32 = workbook.CreateStyle();
style32.Pattern = BackgroundType.HorizontalStripe;
style32.ForegroundColor = System.Drawing.Color.Green;
range[3, 2].SetStyle(style32);
//Save the Workbook
workbook.Save("RangeStyleTest.xlsx");

範囲のCurrentRegionを取得する

CurrentRegionは、現在の範囲を表すRangeオブジェクトを返すプロパティです。

現在の領域は、空白の行と列の組み合わせによって囲まれた範囲です。読み取り専用です。

Excelでは、以下の方法でCurrentRegionエリアを取得できます:

  1. マウスのボックスでエリア(範囲1)を選択します。
  2. “ホーム - 編集 - 検索と選択 - 特殊に移動 - CurrentRegion"をクリックするか、“Ctrl+Shift+*“を使用します。するとExcelが自動的にエリア(範囲2)を選択してくれます。これで、範囲2は範囲1のCurrentRegionになります。

Aspose.Cellsを使用すると、「Range.CurrentRegion」プロパティを使用して同じ機能を実行できます。

以下のテストファイルをダウンロードし、Excelで開き、マウスのボックスを使用して"A1:D7"のエリアを選択し、次に"Ctrl+Shift+*“をクリックすると、“A1:C3"のエリアが選択されます。

current_region.xlsx

次に、以下の例を実行して、Aspose.Cellsでの動作を確認してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Create a Workbook
Aspose.Cells.Workbook workbook = new Workbook("current_region.xlsx");
//Get Cells
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
//Create Range
Aspose.Cells.Range src = cells.CreateRange("A1:D7");
//Get CurrentRegion
Aspose.Cells.Range A1C3 = src.CurrentRegion;

高度なトピック