Set Range Border
Contents
[
Hide
]
Possible Usage Scenarios
When you want to set the border for Range, you don’t need to set each cell individually. You can set the border on the range. Aspose.Cells offers this feature. This article provides a sample code that uses Aspose.Cells to set range border.
Set Range border in Excel
To set the border of a range in Excel, you can follow these steps:
- Select the range of cells that you want to apply the border to.
- In the “Home” tab of the ribbon, locate the “Font” group.
- Within the “Font” group, click on the “Borders” drop-down button.
- Choose the type of border that you want to apply from the options in the drop-down menu. You can choose from preset border styles or customize your own border.
- Once you have selected the desired border style, the border will be applied to the selected range of cells.
Set Range border using Aspose.Cells
This example shows how to:
- Create a workbook.
- Add data to cells in the first worksheet.
- Create a Range.
- Set inner border of Range.
- Set outer border of 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"); |