Skip to main content
A newer version of this page is available. .

XlFont Class

Contains the cell font attributes.

Namespace: DevExpress.Export.Xl

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

Declaration

public class XlFont :
    XlFontBase,
    ISupportsCopyFrom<XlFont>

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/xl-export-api-examples-t253492

// 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);
        }
    }
}

Inheritance

See Also