Border Settings
Adding Borders to Cells
Microsoft Excel allows users to format cells by adding borders. The type of border depends on where it is added. For example, a top border is one added to the top position of a cell. Users can also modify the borders' line style and color.
With Aspose.Cells, developers can add borders and customize what they look like in the same flexible way as in Microsoft Excel.
Adding Borders to Cells
Aspose.Cells provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides the Cells collection. Each item in the Cells collection represents an object of the Cell class.
Aspose.Cells provides the GetStyle method in the Cell class. The SetStyle method is used to set a cell’s formatting style. The Style class provides properties for adding borders to cells.
Adding Borders to a Cell
Developers can add borders to a cell by using the Style object’s Borders collection. The border type is passed as an index to the Borders collection. All border types are pre-defined in the BorderType enumeration.
Border enumeration
Border Types | Description |
---|---|
BottomBorder | A bottom border line |
DiagonalDown | A diagonal line from top left to right bottom |
DiagonalUp | A diagonal line from bottom left to right top |
LeftBorder | A left border line |
RightBorder | A right border line |
TopBorder | A top border line |
The Borders collection stores all borders. Each border in the Borders collection is represented by a Border object which provides two properties, Color and LineStyle to set a border’s line color and style respectively.
To set a border’s line color, select a color using the Color enumeration (part of the .NET Framework) and assign it to the Border object’s Color property.
The border’s line style is set by selecting a line style from the CellBorderType enumeration.
CellBorderType enumeration
Line Styles | Description |
---|---|
DashDot | Thin dash-dotted line |
DashDotDot | Thin dash-dot-dotted line |
Dashed | Dashed line |
Dotted | Dotted line |
Double | Double line |
Hair | Hairline |
MediumDashDot | Medium dash-dotted line |
MediumDashDotDot | Medium dash-dot-dotted line |
MediumDashed | Medium dashed line |
None | No line |
Medium | Medium line |
SlantedDashDot | Slanted medium dash-dotted line |
Thick | Thick line |
Thin | Thin line |
Select one of the line styles and then assign it to the Border object’s LineStyle property. |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first (default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the "A1" cell from the worksheet | |
Aspose.Cells.Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Visit Aspose!"); | |
// Create a style object | |
Style style = cell.GetStyle(); | |
// Setting the line style of the top border | |
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the top border | |
style.Borders[BorderType.TopBorder].Color = Color.Black; | |
// Setting the line style of the bottom border | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the bottom border | |
style.Borders[BorderType.BottomBorder].Color = Color.Black; | |
// Setting the line style of the left border | |
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the left border | |
style.Borders[BorderType.LeftBorder].Color = Color.Black; | |
// Setting the line style of the right border | |
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the right border | |
style.Borders[BorderType.RightBorder].Color = Color.Black; | |
// Apply the border styles to the cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Adding Borders to a Range of Cells
It is also possible to add borders to a range of cells rather than just a single cell. To do so, first, create a range of cells by calling the Cells collection’s CreateRange method. It takes the following parameters:
- First Row, the first row of the range.
- First Column, represents the first column of the range.
- Number of Rows, the number of rows in the range.
- Number of Columns, the number of columns in the range.
The CreateRange method returns a Range object, which contains the specified range of cells. The Range object provides a SetOutlineBorder method that takes the following parameters to add a border to the range of cells:
- Border Type, the border type, selected from the BorderType enumeration.
- Line Style, the border line style, selected from the CellBorderType enumeration.
- Color, the line color, selected from the Color enumeration.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first (default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello World From Aspose"); | |
// Creating a range of cells starting from "A1" cell to 3rd column in a row | |
Range range = worksheet.Cells.CreateRange(0, 0, 1, 3); | |
// Adding a thick top border with blue line | |
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick bottom border with blue line | |
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick left border with blue line | |
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick right border with blue line | |
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |