範囲のボーダーを設定
Contents
[
Hide
]
可能な使用シナリオ
特定の範囲にボーダーを設定する場合、各セルを個別に設定する必要はありません。範囲にボーダーを設定することができます。Aspose.Cellsはこの機能を提供しています。 この記事では、Aspose.Cellsを使用して範囲のボーダーを設定するサンプルコードを提供します。
Excelで範囲のボーダーを設定する
Excelで範囲のボーダーを設定するには、次の手順に従います:
- ボーダーを適用する範囲のセルを選択します。
- リボンの「ホーム」タブに移動し、「フォント」グループを検索します。
- 「フォント」グループ内で、「ボーダー」ドロップダウンボタンをクリックします。
- ドロップダウンメニュー内のオプションから適用するボーダーの種類を選択します。プリセットのボーダースタイルを選択するか、独自のボーダーをカスタマイズすることができます。
- 希望のボーダースタイルを選択したら、そのボーダーが選択したセル範囲に適用されます。
Aspose.Cellsを使用して範囲のボーダーを設定する
この例では、次のことができます:
- ワークブックを作成する。
- 最初のワークシートのセルにデータを追加する。
- Rangeを作成します。
- 範囲の内側のボーダーを設定します。
- 範囲の外側のボーダーを設定します。
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.getWorksheets().get(0); | |
Cells cells = ws.getCells(); | |
//Setting the value to the cells | |
Cell cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("C1"); | |
cell.putValue("Price"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("C2"); | |
cell.putValue(5); | |
cell = cells.get("C3"); | |
cell.putValue(20); | |
cell = cells.get("C4"); | |
cell.putValue(30); | |
cell = cells.get("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.setColor(Color.getRed()); | |
range.setInsideBorders(BorderType.VERTICAL, CellBorderType.THIN, innerColor); | |
innerColor.setColor(Color.getGreen()); | |
range.setInsideBorders(BorderType.HORIZONTAL, CellBorderType.THIN, innerColor); | |
//set outer borer of range | |
CellsColor outerColor = workbook.createCellsColor(); | |
outerColor.setColor(Color.getBlue()); | |
range.setOutlineBorders(CellBorderType.THIN, outerColor); | |
workbook.save("out.xlsx"); |