範囲のボーダーを設定

可能な使用シナリオ

特定の範囲にボーダーを設定する場合、各セルを個別に設定する必要はありません。範囲にボーダーを設定することができます。Aspose.Cellsはこの機能を提供しています。 この記事では、Aspose.Cellsを使用して範囲のボーダーを設定するサンプルコードを提供します。

Excelで範囲のボーダーを設定する

Excelで範囲のボーダーを設定するには、次の手順に従います:

  1. ボーダーを適用する範囲のセルを選択します。
  2. リボンの「ホーム」タブに移動し、「フォント」グループを検索します。
  3. 「フォント」グループ内で、「ボーダー」ドロップダウンボタンをクリックします。
  4. ドロップダウンメニュー内のオプションから適用するボーダーの種類を選択します。プリセットのボーダースタイルを選択するか、独自のボーダーをカスタマイズすることができます。
  5. 希望のボーダースタイルを選択したら、そのボーダーが選択したセル範囲に適用されます。

Aspose.Cellsを使用して範囲のボーダーを設定する

この例では、次のことができます:

  1. ワークブックを作成する。
  2. 最初のワークシートのセルにデータを追加する。
  3. Rangeを作成します。
  4. 範囲の内側のボーダーを設定します。
  5. 範囲の外側のボーダーを設定します。
//Instantiating an Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet
Worksheet ws = workbook.Worksheets[0];
Aspose.Cells.Cells cells = ws.Cells;
//Setting the value to the cells
Aspose.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);
// Create a range (A1:C5).
Range range = cells.CreateRange("A1", "C5");
//set inner borer of range
CellsColor innerColor = workbook.CreateCellsColor();
innerColor.Color = Color.Red;
range.SetInsideBorders(BorderType.Vertical, CellBorderType.Thin, innerColor);
innerColor.Color = Color.Green;
range.SetInsideBorders(BorderType.Horizontal, CellBorderType.Thin, innerColor);
//set outer borer of range
CellsColor outerColor = workbook.CreateCellsColor();
outerColor.Color = Color.Blue;
range.SetOutlineBorders(CellBorderType.Thin, outerColor);
workbook.Save("out.xlsx");