Create and Set Custom Named Style

Spread.Services Documentation
Create and Set Custom Named Style
Collapse All Expand All

Spread.Services Documentation > Developer's Guide > Customize User Interaction > Apply Style > Create and Set Custom Named Style

Named style is a custom cell style that you apply to your worksheet with a unique name, which is different from the already existing built-in style names defined for a spreadsheet.

You can create and set named styles as and when required. In Spread.Services, Styles refers to the named style collection that stores both the built-in and custom named styles.

### Create custom named style

Spread.Services enables you to define custom named styles for your worksheet, configure it as per your preferences and store them in the collection so that they can be accessed later.

You can add a custom named style to your worksheet using the Add method of IStyleCollection interface. This method can also be used to return an IStyle instance. If you want to configure the named style settings in your spreadsheet, you can use the properties of the IStyle interface.

Refer to the following example code to create a custom name style and configure its settings.

C# | Copy Code
--- | ---

    //Add custom name style.
    IStyle style = workbook.Styles.Add("SampleStyle");

    //Config custom name style settings begin.
    //Border
    style.Borders[BordersIndex.EdgeLeft].LineStyle = BorderLineStyle.Thin;
    style.Borders[BordersIndex.EdgeTop].LineStyle = BorderLineStyle.Thick;
    style.Borders[BordersIndex.EdgeRight].LineStyle = BorderLineStyle.Double;
    style.Borders[BordersIndex.EdgeBottom].LineStyle = BorderLineStyle.Double;
    style.Borders.Color = Color.FromRGB(0, 255, 0);

    //Protection
    style.FormulaHidden = true;
    style.Locked = false;

    //Number
    style.NumberFormat = "#,##0_);[Red](#,##0)";

    //Alignment
    style.HorizontalAlignment = HorizontalAlignment.Right;
    style.VerticalAlignment = VerticalAlignment.Bottom;
    style.WrapText = true;
    style.IndentLevel = 5;
    style.Orientation = 45;

    //Fill
    style.Interior.ColorIndex = 5;
    style.Interior.Pattern = GrapeCity.Documents.Spread.Pattern.Down;
    style.Interior.PatternColor = Color.FromRGB(0, 0, 255);
    style.IncludeAlignment = false;
    style.IncludeBorder = true;
    style.IncludeFont = false;
    style.IncludeNumber = true;
    style.IncludePatterns = false;
    style.IncludeProtection = true;
    //Config custom name style settings end.

### Set custom named style

You can get or set named style in a worksheet using the Style property of the IRange interface. The Styles collection stores both built-in and custom named styles in Spread.Services.

Refer to the following example code to get or set named style in your worksheet.

C# | Copy Code
--- | ---

    //Set range's style to custom name style.
    worksheet.Range["A1"].Style = worksheet.Workbook.Styles["SampleStyle"];

Copyright © GrapeCity, inc. All rights reserved.