Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Description

Excerpt

ExcelWriter creates three style types: GlobalStyle, NamedStyle, and CellStyle. All three style types derive from the Style class, and NamedStyle derives from GlobalStyle.

Signature
C#
C#
 public sealed class NamedStyle : GlobalStyle
{signature}{signature:
}
Signature
vb.net
vb.net
Public NotInheritable Class NamedStyle
		Inherits GlobalStyle
{signature}
{remarks}A style can be  +set+  or  +applied+  to cells, rows, columns, ranges, and areas. When a style is  +set+ , it is cloned and the object to which the style is assigned acquires all of that style's properties, including font proprties and number formatting. When a style is  +applied+ , only the differences between the new style and existing style properties \(assigned through the ExcelWriter API or in Microsoft Excel\) will take effect. For example, if the cell has a background color and the new style applied does not contain a background color, the cell's color will not be affected. However, if the new style includes a background color, it will replace the existing background color of the cell.

The [ExcelApplication|ExcelApplication] object contains a collection of {{NamedStyle}} s, which \- unlike {{GlobalStyle}} s are accessible after the workbook is saved. This allows you to dynamically create a template, save it to disk, re\-open it to populate it with data, and then apply your newly\-created style. This collection of {{NamedStyle}} s includes Excel's built\-in styles \(such as the NORMAL style\) and any user\-defined styles in a workbook opened with the ExcelApplication object.

To create a {{NamedStyle}}, call [Workbook.CreateNamedStyle|Workbook.CreateNamedStyle(String)].

To return a {{NamedStyle}}, call [Workbook.GetNamedStyle|Workbook.GetNamedStyle(String)].

{remarks}
{example}{code:csharp|title=C#}
Remarks

A style can be set or applied to cells, rows, columns, ranges, and areas. When a style is set , it is cloned and the object to which the style is assigned acquires all of that style's properties, including font proprties and number formatting. When a style is applied , only the differences between the new style and existing style properties (assigned through the ExcelWriter API or in Microsoft Excel) will take effect. For example, if the cell has a background color and the new style applied does not contain a background color, the cell's color will not be affected. However, if the new style includes a background color, it will replace the existing background color of the cell.

The ExcelApplication object contains a collection of NamedStyle s, which - unlike GlobalStyle s are accessible after the workbook is saved. This allows you to dynamically create a template, save it to disk, re-open it to populate it with data, and then apply your newly-created style. This collection of NamedStyle s includes Excel's built-in styles (such as the NORMAL style) and any user-defined styles in a workbook opened with the ExcelApplication object.

To create a NamedStyle, call Workbook.CreateNamedStyle.

To return a NamedStyle, call Workbook.GetNamedStyle.

Example
Code Block
csharp
csharp
titleC#


          ExcelApplication xla = new ExcelApplication();
          Workbook wb = xla.Create();

          //--- Create a named style.
          Style myItalicStyle = wb.CreateNamedStyle("ItalicStyle");

          //--- Turn on italics for the named style.
          myGlobalStyle.Font.Italics = true;

          //--- Get a "total" cell
          Cell totalCell = wb.Worksheets[0].Cells[4, 4];

          //--- Check its value and apply style if necessary
          if(totalCell.Value > TARGET_TOTAL_VALUE)
               totalCell.ApplyStyle(myItalicStyle);
        
{code} {code:vbnet|title=
Code Block
vbnet
vbnet
titlevb.net
}


          Dim xla As New ExcelApplication()
          Workbook wb = xla.Create()

          '--- Create a named style.
          Dim myItalicStyle As Style = wb.CreateNamedStyle("ItalicStyle")

          '--- Turn on italics for the named style
          myGlobalStyle.Font.Italics = True

          '--- Get a "total" cell
          Dim totalCall As Cell = wb.Worksheets(0).Cells(4, 4)

          '--- Check its value and apply style if necessary
          If totalCell.Value > TARGET_TOTAL_VALUE Then
               totalCell.ApplyStyle(myItalicStyle)
          End If
        
{code} {example} {properties} ||Name||Description|| |[Name|NamedStyle.Name]|{excerpt-include:NamedStyle.Name|nopanel=true}|
Properties

Name

Description

Name

Excerpt Include
NamedStyle.Name
NamedStyle.Name
nopaneltrue