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.
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.
A bottom border line
A diagonal line from top left to right bottom
A diagonal line from bottom left to right top
A left border line
A right border line
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
The border's line style is set by selecting a line style from the CellBorderType enumeration.
Thin dash-dotted line
Thin dash-dot-dotted line
Medium dash-dotted line
Medium dash-dot-dotted line
Medium dashed line
Slanted medium dash-dotted line
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.
Colors and Palette
A palette is the number of colors available for use in creating an image. The use of a standardized palette in a presentation allows the user to create a consistent look. Each Microsoft Excel (97-2003) file has a palette of 56 colors that can be applied to cells, fonts, gridlines, graphic objects, fills and lines in a chart.
With Aspose.Cells it is possible not only to use the palette's existing colors but also custom colors. Before using a custom color, add it to the palette first.
This topic discusses how to add custom colors to the palette.
Adding Custom Colors to Palette
Aspose.Cells supports Microsoft Excel's 56 color palette. To use a custom color that is not defined in the palette, add the color to the palette.
Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class provides a ChangePalette method that takes the following parameters to add a custom color to modify the palette:
- Custom Color, the custom color to be added.
- Index, the index of the color in the palette that the custom color will replace. Should be between 0-55.
The example below adds a custom color (Orchid) to the palette before applying it on a font.
Colors and Background Patterns
Microsoft Excel can set the foreground (outline) and background (fill) colors of cells and background patterns.
Aspose.Cells also supports these features in a flexible manner. In this topic, we learn to use these features using Aspose.Cells.
Setting Colors and Background Patterns
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 a Cells collection. Each item in the Cells collection represents an object of the Cell class.
The Cell has the GetStyle and SetStyle methods that are used to get and set a cell's formatting. The Style class provides properties for setting the foreground and background colors of the cells. Aspose.Cells provides a BackgroundType enumeration that contains a set of pre-defined types of background patterns which are given below.
Represents diagonal crosshatch pattern
Represents diagonal stripe pattern
Represents 6.25% gray pattern
Represents 12.5% gray pattern
Represents 25% gray pattern
Represents 50% gray pattern
Represents 75% gray pattern
Represents horizontal stripe pattern
Represents no background
Represents reverse diagonal stripe pattern
Represents solid pattern
Represents thick diagonal crosshatch pattern
Represents thin diagonal crosshatch pattern
Represents thin diagonal stripe pattern
Represents thin horizontal crosshatch pattern
Represents thin horizontal stripe pattern
Represents thin reverse diagonal stripe pattern
Represents thin vertical stripe pattern
Represents vertical stripe pattern
In the example below, the foreground color of the A1 cell is set but A2 is configured to have both foreground and background colors with a vertical stripe background pattern.