Back to Devexpress

Use the Excel Export API to Configure Cell Font Settings

officefileapi-114208-excel-export-library-formatting-how-to-configure-cell-font-settings.md

latest11.1 KB
Original Source

Use the Excel Export API to Configure Cell Font Settings

  • Sep 19, 2023
  • 8 minutes to read

To set different font attributes for a cell, use the properties of the XlFont object. This object provides the following properties inherited form the XlFontBase class to change cell font characteristics.

To change font settings of cell content, perform the steps below:

  1. Initialize an instance of the XlFont class. Do one of the following:

  2. Set the required properties of the XlFont object. For example, to make cell text bold, set the XlFontBase.Bold property to true.

  3. To apply font characteristics to a cell, use one of the following approaches:

View Example

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

The image below shows the result (the workbook is opened in Microsoft® Excel®).

See Also

Use the Excel Export API to Format a Cell