Back to Devexpress

XlFont Class

corelibraries-devexpress-dot-export-dot-xl-cd8df675.md

latest13.3 KB
Original Source

XlFont Class

Contains the cell font attributes.

Namespace : DevExpress.Export.Xl

Assembly : DevExpress.Printing.v25.2.Core.dll

NuGet Package : DevExpress.Printing.Core

Declaration

csharp
public class XlFont :
    XlFontBase,
    ISupportsCopyFrom<XlFont>
vb
Public Class XlFont
    Inherits XlFontBase
    Implements ISupportsCopyFrom(Of XlFont)

The following members return XlFont objects:

Remarks

The XlFont class inherits the common font characteristics from the XlFontBase class (XlFontBase.Name, XlFontBase.Size, XlFontBase.Bold, XlFontBase.Underline, etc.). In addition to the inherited settings, the XlFont class exposes properties and methods which allow you to define a font color (XlFont.Color), font family (XlFont.FontFamily), set the custom font (XlFont.CustomFont), or use the default theme body font (XlFont.BodyFont) and heading font (XlFont.HeadingsFont).

To apply font settings to a cell, create an instance of the XlFont class, set its properties and then assign the created XlFont object to the IXlCell.Formatting property, or pass it to the IXlCell.ApplyFormatting method as a parameter.

To share font settings with multiple adjacent cells in a row at once, use the IXlRow.BulkCells method.

To specify font settings for the entire row or column, use the IXlRow.ApplyFormatting and IXlColumn.ApplyFormatting methods, or IXlRow.Formatting and IXlColumn.Formatting properties, respectively.

For an example, refer to the How to: Configure Cell Font Settings article.

Example

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

csharp
// Create a new worksheet.
using (IXlSheet sheet = document.CreateSheet())
{
    // Create five successive columns and set their widths.
    for (int i = 0; i < 5; i++)
    {
        using (IXlColumn column = sheet.CreateColumn())
        {
            column.WidthInPixels = 100;
        }
    }

    // Create the first row.
    using (IXlRow row = sheet.CreateRow())
    {
        // Create the cell A1.
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Body font";
            // Apply the theme body font to the cell content.
            cell.ApplyFormatting(XlFont.BodyFont());
        }

        // Create the cell B1.
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Headings font";
            // Apply the theme heading font to the cell content.
            cell.ApplyFormatting(XlFont.HeadingsFont());
        }

        // Create the cell C1.
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Custom font";
            // Specify the custom font attributes.
            XlFont font = new XlFont();
            font.Name = "Century Gothic";
            font.SchemeStyle = XlFontSchemeStyles.None;
            // Apply the custom font to the cell content.
            cell.ApplyFormatting(font);
        }
    }

    // Create an array that stores different values of font size.
    int[] fontSizes = new int[] { 11, 14, 18, 24, 36 };
    // Skip one row in the worksheet.
    sheet.SkipRows(1);

    // Create the third row.
    using (IXlRow row = sheet.CreateRow())
    {
        // Create five successive cells (A3:E3) with different font sizes.
        for (int i = 0; i < 5; i++)
        {
            using (IXlCell cell = row.CreateCell())
            {
                // Set the cell value that displays the applied font size.
                cell.Value = string.Format("{0}pt", fontSizes[i]);
                // Create a font instance of the specified size.
                XlFont font = new XlFont();
                font.Size = fontSizes[i];
                // Apply font settings to the cell content.
                cell.ApplyFormatting(font);
            }
        }
    }

    // Skip one row in the worksheet.
    sheet.SkipRows(1);

    // Create the fifth row.
    using (IXlRow row = sheet.CreateRow())
    {
        // Create the cell A5.
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Red";
            // Create a font instance and set its color.
            XlFont font = new XlFont() { Color = Color.Red };
            // Apply the font color to the cell content.
            cell.ApplyFormatting(font);
        }

        // Create the cell B5. 
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Bold";
            // Create a font instance and set its style to bold.
            XlFont font = new XlFont() { Bold = true };
            // Apply the font style to the cell content.
            cell.ApplyFormatting(font);
        }

        // Create the cell C5. 
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Italic";
            // Create a font instance and set its style to italic.
            XlFont font = new XlFont() { Italic = true };
            // Italicize the cell text.
            cell.ApplyFormatting(font);
        }

        // Create the cell D5. 
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "Underline";
            // Create a font instance and set the underline type to double.
            XlFont font = new XlFont() { Underline = XlUnderlineType.Double };
            // Underline the cell text.
            cell.ApplyFormatting(font);
        }

        // Create the cell E5.
        using (IXlCell cell = row.CreateCell())
        {
            // Set the cell value.
            cell.Value = "StrikeThrough";
            // Create a font instance and turn the strikethrough formatting on.
            XlFont font = new XlFont() { StrikeThrough = true };
            // Strike the cell text through. 
            cell.ApplyFormatting(font);
        }
    }
}
vb
' Create a new worksheet.
Using sheet As IXlSheet = document.CreateSheet()
    ' Create five successive columns and set their widths.
    For i As Integer = 0 To 4
        Using column As IXlColumn = sheet.CreateColumn()
            column.WidthInPixels = 100
        End Using
    Next i

    ' Create the first row.
    Using row As IXlRow = sheet.CreateRow()
        ' Create the cell A1.
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Body font"
            ' Apply the theme body font to the cell content.
            cell.ApplyFormatting(XlFont.BodyFont())
        End Using

        ' Create the cell B1.
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Headings font"
            ' Apply the theme heading font to the cell content.
            cell.ApplyFormatting(XlFont.HeadingsFont())
        End Using

        ' Create the cell C1.
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Custom font"
            ' Specify the custom font attributes.

            Dim font_Renamed As New XlFont()
            font_Renamed.Name = "Century Gothic"
            font_Renamed.SchemeStyle = XlFontSchemeStyles.None
            ' Apply the custom font to the cell content.
            cell.ApplyFormatting(font_Renamed)
        End Using
    End Using

    ' Create an array that stores different values of font size.
    Dim fontSizes() As Integer = { 11, 14, 18, 24, 36 }
    ' Skip one row in the worksheet.
    sheet.SkipRows(1)

    ' Create the third row.
    Using row As IXlRow = sheet.CreateRow()
        ' Create five successive cells (A3:E3) with different font sizes.
        For i As Integer = 0 To 4
            Using cell As IXlCell = row.CreateCell()
                ' Set the cell value that displays the applied font size.
                cell.Value = String.Format("{0}pt", fontSizes(i))
                ' Create a font instance of the specified size.

                Dim font_Renamed As New XlFont()
                font_Renamed.Size = fontSizes(i)
                ' Apply font settings to the cell content.
                cell.ApplyFormatting(font_Renamed)
            End Using
        Next i
    End Using

    ' Skip one row in the worksheet.
    sheet.SkipRows(1)

    ' Create the fifth row.
    Using row As IXlRow = sheet.CreateRow()
        ' Create the cell A5.
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Red"
            ' Create a font instance and set its color.

            Dim font_Renamed As New XlFont() With {.Color = Color.Red}
            ' Apply the font color to the cell content.
            cell.ApplyFormatting(font_Renamed)
        End Using

        ' Create the cell B5. 
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Bold"
            ' Create a font instance and set its style to bold.

            Dim font_Renamed As New XlFont() With {.Bold = True}
            ' Apply the font style to the cell content.
            cell.ApplyFormatting(font_Renamed)
        End Using

        ' Create the cell C5. 
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Italic"
            ' Create a font instance and set its style to italic.

            Dim font_Renamed As New XlFont() With {.Italic = True}
            ' Italicize the cell text.
            cell.ApplyFormatting(font_Renamed)
        End Using

        ' Create the cell D5. 
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "Underline"
            ' Create a font instance and set the underline type to double.

            Dim font_Renamed As New XlFont() With {.Underline = XlUnderlineType.Double}
            ' Underline the cell text.
            cell.ApplyFormatting(font_Renamed)
        End Using

        ' Create the cell E5.
        Using cell As IXlCell = row.CreateCell()
            ' Set the cell value.
            cell.Value = "StrikeThrough"
            ' Create a font instance and turn the strikethrough formatting on.

            Dim font_Renamed As New XlFont() With {.StrikeThrough = True}
            ' Strike the cell text through. 
            cell.ApplyFormatting(font_Renamed)
        End Using
    End Using
End Using

Inheritance

Object XlFontBase XlFont

See Also

XlFont Members

Use the Excel Export API to Configure Cell Font Settings

DevExpress.Export.Xl Namespace