[]
        
(Showing Draft Content)

Create and Set Custom Named Style

Named style is a custom cell style that you apply to your workbook or 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 custom named styles as and when required. You can also modify an existing style and save it as another workbook style. In GcExcel .NET, Styles refers to the named style collection that stores both the built-in and custom named styles.

While working with styles in the spreadsheets, you can use any of the following ways

Create and Set a Custom Named Style

GcExcel .NET 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.

//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.FromArgb(0, 255, 0);
            
//Protection
style.FormulaHidden = true;
style.Locked = false;

//Number
style.NumberFormat = "#,##0_);[Red](gcdocsite__documentlink?toc-item-id=335d8d4b-d3fd-467e-bcb8-3be9ee623ad6#,)";

//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.Excel.Pattern.Down;
style.Interior.PatternColor = Color.FromArgb(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.

You can also 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 GcExcel .NET.

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

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

Modify an Existing Style and Save it as a New Workbook Style

With GcExcel.NET, you don't always need to create a custom named style right from the scratch. Instead, you can modify an existing style (via getting the existing style from the Styles collection) as per your specific preferences and save the new style as another workbook style that can be used as and when required.

Users can use the Add method in order to add the new style. The newly created custom style will be based on the existing workbook style and will be stored in the IStyleCollection interface so that it can be used as another workbook style in the future.

Refer to the following example code in order to modify an existing style and save it as a new workbook style in the Styles collection.

// Create workbook
Workbook workbook = new Workbook();

// Fetch the default worksheet
IWorksheet worksheet = workbook.Worksheets[0];

// Fetch existing Style "Good" and set to Range A1's Style
worksheet.Range["A1"].Style = workbook.Styles["Good"];

// Setting Cell Text
worksheet.Range["A1"].Value = "Good";

// Create and modify a style based on current existing style "Good" and name it as "MyGood"
IStyle myGood = workbook.Styles.Add("MyGood", workbook.Styles["Good"]);
myGood.Font.Bold = true;
myGood.Font.Italic = true;

// Set new style "MyGood" to Range B1's Style
worksheet.Range["B1"].Style = workbook.Styles["MyGood"];

// Setting Cell Text
worksheet.Range["B1"].Value = "MyGood";
       
// Saving the workbook
workbook.Save(@"6 - AddWorkbookStyles.xlsx");