Interface IStyles
A collection of all the Style objects in the specified or active workbook. Each Style object represents a style description for a range. The Style object contains all style attributes (font, number format, alignment, and so on) as properties. There are several built-in styles including Normal, Currency, and Percent which are listed in the Style name box in the Style dialog box. (Format menu).
Namespace: Syncfusion.XlsIO
Assembly: Syncfusion.XlsIO.Portable.dll
Syntax
public interface IStyles : IEnumerable
Properties
Application
Used without an object qualifier, this property returns an Application object that represents the Microsoft Excel application.
Declaration
IApplication Application { get; }
Property Value
Type |
---|
IApplication |
Examples
The following code illustrates how IApplication object in the IStyles collection can be accessed .
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Access application object
Console.WriteLine(style.Application.DefaultVersion);
//Wait for user input
Console.ReadKey();
}
//Output will be
//Excel2013
Count
Declaration
int Count { get; }
Property Value
Type |
---|
System.Int32 |
Examples
The following code illustrates how to access the number of IStyle objects in the IStyles collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Text = "Sample";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Check styles count
Console.WriteLine(workbook.Styles.Count);
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//7
Item[Int32]
Declaration
IStyle this[int Index] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | Index |
Property Value
Type |
---|
IStyle |
Examples
The following code illustrates how to access IStyle object in the IStyles collection using it's index.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Text = "Sample";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Check style name
Console.WriteLine(workbook.Styles[6].Name);
Console.WriteLine(workbook.Styles[0].Name);
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//CustomStyle
//Normal
Item[String]
Declaration
IStyle this[string name] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.String | name |
Property Value
Type |
---|
IStyle |
Examples
The following code illustrates how to access IStyle object in the IStyles collection using it's Name.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Text = "Sample";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Check style built in
Console.WriteLine(workbook.Styles["CustomStyle"].BuiltIn);
Console.WriteLine(workbook.Styles["Normal"].BuiltIn);
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//False
//True
Parent
Returns the parent object for the specified object.
Declaration
object Parent { get; }
Property Value
Type |
---|
System.Object |
Examples
The following code illustrates how to access the parent of a IStyle object.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Text = "Sample";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Set color
style.ColorIndex = ExcelKnownColors.Red;
//Set style
worksheet["C2"].CellStyle = style;
//Check styles count
Console.WriteLine(style.Parent);
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//Syncfusion.XlsIO.Implementation.WorkbookImpl
Methods
Add(String)
Creates a new IStyle and adds it to the list of IStyles that are available for the current workbook. Returns a IStyle object.
Declaration
IStyle Add(string Name)
Parameters
Type | Name | Description |
---|---|---|
System.String | Name | Name of the created IStyle. |
Returns
Type | Description |
---|---|
IStyle | Newly created IStyle. |
Remarks
To know more about IStyles refer Create a Style.
Examples
The following code illustrates how to create and add a IStyle to the IStyles collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Text = "Sample";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Set color
style.ColorIndex = ExcelKnownColors.Red;
//Set style
worksheet["C2"].CellStyle = style;
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
}
Add(String, Object)
Creates a new IStyle and adds it to the list of IStyles that are available for the current workbook. Returns a IStyle object.
Declaration
IStyle Add(string Name, object BasedOn)
Parameters
Type | Name | Description |
---|---|---|
System.String | Name | Name of the newly created IStyle. |
System.Object | BasedOn | Prototype for the IStyle. |
Returns
Type | Description |
---|---|
IStyle | Newly created IStyle. |
Examples
The following code illustrates how to add a IStyle created based on an existing IStyle object.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Value = "100";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle", workbook.Styles[1]);
//Set color
style.ColorIndex = ExcelKnownColors.Red;
//Set style
worksheet["C2"].CellStyle = style;
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
}
Contains(String)
Declaration
bool Contains(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name to check. |
Returns
Type | Description |
---|---|
System.Boolean | True - if IStyle exists, otherwise False. |
Examples
The following code illustrates how to check whether a IStyle is present in IStyles collection or not.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text
worksheet["C2"].Text = "Sample";
//Add style
IStyle style = workbook.Styles.Add("CustomStyle");
//Check styles count
Console.WriteLine(workbook.Styles.Contains("CustomStyle"));
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//True
Merge(Object)
Merges the styles from another workbook into the Styles collection. Keep only unique style in collection.
Declaration
IStyles Merge(object Workbook)
Parameters
Type | Name | Description |
---|---|---|
System.Object | Workbook | Workbook from which all styles will be added. |
Returns
Type | Description |
---|---|
IStyles | Merged collection. |
Examples
The following code illustrates how styles can be imported from a source workbook to destination workbook.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Create Styles
workbook.Styles.Add("CustomStyle1");
workbook.Styles.Add("CustomStyle2");
//Modify styles
workbook.Styles["CustomStyle1"].ColorIndex = ExcelKnownColors.Red;
workbook.Styles["CustomStyle2"].ColorIndex = ExcelKnownColors.Blue;
//Create new worksheet
IWorkbook book = application.Workbooks.Create(1);
IWorksheet sheet = book.Worksheets[0];
//Check count
Console.WriteLine("Before Merge\nDestination "+book.Styles.Count);
Console.WriteLine("Source "+workbook.Styles.Count);
//Merge styles
book.Styles.Merge(workbook);
//Check count
Console.WriteLine("After Merge\nDestination "+book.Styles.Count);
Console.WriteLine("Source "+workbook.Styles.Count);
//Close
workbook.Close();
book.Close();
Console.ReadKey();
}
//Output will be
//Before Merge
//Destination 6
//Source 8
//After Merge
//Destination 8
//Source 8
Merge(Object, Boolean)
Merges the styles from another workbook into the Styles collection.
Declaration
IStyles Merge(object Workbook, bool overwrite)
Parameters
Type | Name | Description |
---|---|---|
System.Object | Workbook | Workbook from which all styles will be added. |
System.Boolean | overwrite | True - overwrite styles with the same names, otherwise. |
Returns
Type | Description |
---|---|
IStyles | Merged collection. |
Examples
The following code illustrates how styles can be imported from a source workbook to destination workbook.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Create Styles
workbook.Styles.Add("CustomStyle1");
workbook.Styles.Add("CustomStyle2");
//Modify styles
workbook.Styles["CustomStyle1"].ColorIndex = ExcelKnownColors.Red;
workbook.Styles["CustomStyle2"].ColorIndex = ExcelKnownColors.Blue;
//Create new worksheet
IWorkbook book = application.Workbooks.Create(1);
IWorksheet sheet = book.Worksheets[0];
//Check count
Console.WriteLine("Before Merge\nDestination "+book.Styles.Count);
Console.WriteLine("Source "+workbook.Styles.Count);
//Merge styles
book.Styles.Merge(workbook, true);
//Check count
Console.WriteLine("After Merge\nDestination "+book.Styles.Count);
Console.WriteLine("Source "+workbook.Styles.Count);
//Close
workbook.Close();
book.Close();
Console.ReadKey();
}
//Output will be
//Before Merge
//Destination 6
//Source 8
//After Merge
//Destination 8
//Source 8
Remove(String)
Declaration
void Remove(string styleName)
Parameters
Type | Name | Description |
---|---|---|
System.String | styleName | IStyle name to remove. |
Examples
The following code illustrates how to remove a IStyle from the IStyles collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorkbook workbook2 = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Create style
IStyle style1 = workbook.Styles.Add("CustomStyle1");
IStyle style2 = workbook.Styles.Add("CustomStyle2");
//Check styles count
Console.WriteLine(workbook.Styles.Count);
//Removing style
workbook.Styles.Remove("CustomStyle1");
//Check styles count
Console.WriteLine(workbook.Styles.Count);
//Save and dispose
workbook.SaveAs("CellFormats.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//8
//7