範囲の管理

序章

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

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

ここでは、範囲を作成し、値を入力し、スタイルを設定し、「Range」オブジェクトに対してその他の操作を行う方法を示します。

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

Aspose.Cells はクラスを提供し、ワークブック Microsoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。ワークシートは、ワークシートクラス。のワークシートクラスは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");

Range の Cells に値を入れます

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

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

// 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");

レンジのCellsのセットスタイル

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

// 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. マウスボックスで領域(range1)を選択します。
  2. [ホーム] - [編集] - [検索と選択] - [特別な場所に移動] - [現在の地域] をクリックするか、[Ctrl+Shift+*] を使用すると、Excel が自動的に領域 (範囲 2) を選択するのに役立ちます。 range1 の CurrentRegion。

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

次のテスト ファイルをダウンロードして Excel で開き、マウス ボックスを使用して領域 “A1:D7” を選択し、“Ctrl+Shift+*” をクリックすると、領域 “A1:C3” が選択されていることがわかります。

現在の地域.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;

先行トピック