Back to Devexpress

StyleCollection.Add(String) Method

officefileapi-devexpress-dot-spreadsheet-dot-stylecollection-dot-add-x28-system-dot-string-x29.md

latest6.5 KB
Original Source

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.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
Style Add(
    string name
)
vb
Function Add(
    name As String
) As Style

Parameters

NameTypeDescription
nameString

A string that specifies the style name.

|

Returns

TypeDescription
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:

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

Use the StyleCollection.Add method of the Workbook.Styles collection to create a new style with a given name. The style’s format settings are identical to the Normal built-in style. Use the Style object’s properties to specify format characteristics for the new style.

View Example

csharp
// Add a new style under the "My Style" name to the style collection.
Style myStyle = workbook.Styles.Add("My Style");

// Specify the style's format characteristics.
myStyle.BeginUpdate();
try {
    // Set the font color to blue.
    myStyle.Font.Color = Color.Blue;

    // Set the font size to 12.
    myStyle.Font.Size = 12;

    // Center text in cells.
    myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

    // Set the background fill.
    myStyle.Fill.BackgroundColor = Color.LightBlue;
    myStyle.Fill.PatternType = PatternType.LightGray;
    myStyle.Fill.PatternColor = Color.Yellow;
} 
finally {
    myStyle.EndUpdate();
}
vb
' Add a new style under the "My Style" name to the style collection.
Dim myStyle As Style = workbook.Styles.Add("My Style")

' Specify the style's format characteristics.
myStyle.BeginUpdate()
Try
    ' Set the font color to blue.
    myStyle.Font.Color = Color.Blue

    ' Set the font size to 12.
    myStyle.Font.Size = 12

    ' Center text in cells.
    myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center

    ' Set the background fill.
    myStyle.Fill.BackgroundColor = Color.LightBlue
    myStyle.Fill.PatternType = PatternType.LightGray
    myStyle.Fill.PatternColor = Color.Yellow
Finally
    myStyle.EndUpdate()
End Try

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Add(String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/FormattingActions.vb#L50

vb
' Add a new style under the "My Style" name to the workbook's style collection.
Dim myStyle As Style = workbook.Styles.Add("My Style")
' Specify formatting characteristics for the style.

See Also

Format Spreadsheet Cells

How to: Create or Modify a Cell Style

How to: Apply a Style to a Cell or Range of Cells

StyleCollection Interface

StyleCollection Members

DevExpress.Spreadsheet Namespace