StyleCollection.Add(String) Method
Creates a new style with the specified name and appends it to the style collection.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v19.1.Core.dll
Declaration
Parameters
Name | Type | Description |
---|---|---|
name | String | A string that specifies the style name. |
Returns
Type | Description |
---|---|
Style | A Style object that specifies the new style. |
Remarks
A workbook’s style collection (IWorkbook.Styles) contains Microsoft® Excel® built-in styles by default. The BuiltInStyleId enumerator lists these predefined styles. You can use the Add method to extend the style collection with custom styles.
When you name a style, take into account the following restrictions:
- A style name must be unique. To ensure that StyleCollection does not contain a style with the specified name, use the StyleCollection.Contains method.
- A style name must be non-empty.
All settings of the created Style object except name (Style.Name) are the same as in the Normal default style. To set required formatting characteristics for the style, modify the corresponding properties of the Style object within the Formatting.BeginUpdate - Formatting.EndUpdate paired method calls.
To create a new style based on the existing style, use the Style.CopyFrom method.
For details on how to create and modify styles, see the How to: Create or Modify a Style document.
Example
Add a new style to the Workbook.Styles collection by calling the
StyleCollection.Add
method with the style name passed as a parameter.Use properties of the newly created Style object to set the required format characteristics.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/spreadsheet-document-server-api-e4339
// Add a new style under the "My Style" name to the Styles collection of the workbook.
Style myStyle = workbook.Styles.Add("My Style");
// Specify formatting characteristics for the style.
myStyle.BeginUpdate();
try {
// Set the font color to Blue.
myStyle.Font.Color = Color.Blue;
// Set the font size to 12.
myStyle.Font.Size = 12;
// Set the horizontal alignment to Center.
myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
// Set the background.
myStyle.Fill.BackgroundColor = Color.LightBlue;
myStyle.Fill.PatternType = PatternType.LightGray;
myStyle.Fill.PatternColor = Color.Yellow;
}
finally {
myStyle.EndUpdate();
}