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.
|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.
|DashDot||Thin dash-dotted line|
|DashDotDot||Thin dash-dot-dotted line|
|MediumDashDot||Medium dash-dotted line|
|MediumDashDotDot||Medium dash-dot-dotted line|
|MediumDashed||Medium dashed line|
|SlantedDashDot||Slanted medium dash-dotted line|
|Select one of the line styles and then assign it to the Border object’s LineStyle 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 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.