How to Apply Multiple Fonts in a Single Cell in C#, VB.NET

To make the text within a cell diverse, we can apply different font to different range of characters. Spire.XLS also provides the ability to apply multiple fonts in a single cell by using RichText.SetFont() method. This article presents how to create different fonts in a workbook and apply them to a certain cell in C# and VB.NET.

Code Snippet:

Step 1: Initialize an instance of Workbook class and get the first worksheet.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step 2: Create a font object in workbook, setting the font color, size and type.

ExcelFont font1 = wb.CreateFont();
font1.KnownColor = ExcelColors.LightBlue;
font1.IsBold = true;
font1.Size = 10;

Step 3: Create another font object specifying its properties.

ExcelFont font2 = wb.CreateFont();
font2.KnownColor = ExcelColors.Red;
font2.IsBold = true;
font2.IsItalic = true;
font2.FontName = "Times New Roman";
font2.Size = 11;

Step 4: Write a RichText string to the cell 'A1', and set the font for the specific range of characters using RichText.SetFont() method.

RichText richText = sheet.Range["A1"].RichText;
richText.Text = "This document was created with Spire.XLS for .NET.";
richText.SetFont(0, 29, font1);
richText.SetFont(31, 48, font2);

Step 5: Save the file.

wb.SaveToFile("MultiFonts.xlsx", ExcelVersion.Version2010);

Output:

How to Apply Multiple Fonts in a Single Cell in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
namespace ApplyMutipleFont
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];

            ExcelFont font1 = wb.CreateFont();
            font1.KnownColor = ExcelColors.LightBlue;
            font1.IsBold = true;
            font1.Size = 10;

            ExcelFont font2 = wb.CreateFont();
            font2.KnownColor = ExcelColors.Red;
            font2.IsBold = true;
            font2.IsItalic = true;
            font2.FontName = "Times New Roman";
            font2.Size = 11;

            RichText richText = sheet.Range["A1"].RichText;
            richText.Text = "This document was created with Spire.XLS for .NET.";
            richText.SetFont(0, 29, font1);
            richText.SetFont(31, 48, font2);

            wb.SaveToFile("MultiFonts.xlsx", ExcelVersion.Version2010);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace ApplyMutipleFont
	Class Program

		Private Shared Sub Main(args As String())
			Dim wb As New Workbook()
			Dim sheet As Worksheet = wb.Worksheets(0)

			Dim font1 As ExcelFont = wb.CreateFont()
			font1.KnownColor = ExcelColors.LightBlue
			font1.IsBold = True
			font1.Size = 10

			Dim font2 As ExcelFont = wb.CreateFont()
			font2.KnownColor = ExcelColors.Red
			font2.IsBold = True
			font2.IsItalic = True
			font2.FontName = "Times New Roman"
			font2.Size = 11

			Dim richText As RichText = sheet.Range("A1").RichText
			richText.Text = "This document was created with Spire.XLS for .NET."
			richText.SetFont(0, 29, font1)
			richText.SetFont(31, 48, font2)

			wb.SaveToFile("MultiFonts.xlsx", ExcelVersion.Version2010)
		End Sub
	End Class
End Namespace