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 for Python via .NET, 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 for Python via .NET 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 for Python via .NET provides the get_style method in the Cell class. The set_style 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
BOTTOM_BORDER A bottom border line
DIAGONAL_DOWN A diagonal line from top left to right bottom
DIAGONAL_UP A diagonal line from bottom left to right top
LEFT_BORDER A left border line
RIGHT_BORDER A right border line
TOP_BORDER 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 line_style 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
DASH_DOT Thin dash-dotted line
DASH_DOT_DOT Thin dash-dot-dotted line
DASHED Dashed line
DOTTED Dotted line
DOUBLE Double line
HAIR Hairline
MEDIUM_DASH_DOT Medium dash-dotted line
MEDIUM_DASH_DOT_DOT Medium dash-dot-dotted line
MEDIUM_DASHED Medium dashed line
NONE No line
MEDIUM Medium line
SLANTED_DASH_DOT 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 line_style property.

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 create_range 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 create_range method returns a Range object, which contains the specified range of cells. The Range object provides a set_outline_border 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.