Articles in this section
Category / Section

How to apply multiple fonts to a single cell in Excel using XlsIO

5 mins read

Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, it is possible to set rich text in a single cell in an Excel document.

Steps to set rich text to a single cell in a worksheet, programmatically:

  1. Create a new C# console application project.

Create a new C# console application project

Create a new C# console application project

  1. Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

Install NuGet package to the project

Install NuGet package to the project

  1. You can set rich text using the SetFont method of the IRichTextString interface.

C#

richText.SetFont(0, 7, blueFont);
richText.SetFont(9, 14, redFont);

 

VB.NET

richText.SetFont(0, 7, blueFont)
richText.SetFont(9, 14, redFont)

 

  1. Include the following namespaces in Program.cs file.

C#

using Syncfusion.XlsIO;
using System.Drawing;

 

VB.NET

Imports Syncfusion.XlsIO
Imports System.Drawing

 

  1. Include the following code snippet in main method of the Program.cs file to set rich text to a single cell in an Excel worksheet.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    // Instantiate the Excel application object
    IApplication application = excelEngine.Excel;
 
    // Create a new Excel workbook
    IWorkbook workbook = application.Workbooks.Create(1);
 
    // Get the first worksheet in the workbook into IWorksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    // Get a cell in worksheet into IRange and assign text to it
    IRange range = worksheet.Range["A1"];
    range.Text = "Employee Report";
    range.AutofitColumns();
 
    // Initialize RichText for the cell
    IRichTextString richText = range.RichText;
 
    // Create and set a new font
    IFont blueFont = workbook.CreateFont();
    blueFont.Italic = true;
    blueFont.RGBColor = Color.Blue;
    richText.SetFont(0, 7, blueFont);
 
    // Create and set another new font
    IFont redFont = workbook.CreateFont();
    redFont.Bold = true;
    redFont.RGBColor = Color.Red;                
    richText.SetFont(9, 14, redFont);
 
    // Save the Excel workbook
    workbook.SaveAs("Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the Excel application object
    Dim application As IApplication = excelEngine.Excel
 
    'Create a new Excel workbook
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
 
    'Get the first worksheet in the workbook into IWorksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Get a cell in worksheet into IRange and assign text to it
    Dim range As IRange = worksheet.Range("A1")
    range.Text = "Employee Report"
    range.AutofitColumns()
 
    'Initialize RichText for the cell
    Dim richText As IRichTextString = range.RichText
 
    'Create and set a new font
    Dim blueFont As IFont = workbook.CreateFont()
    blueFont.Italic = True
    blueFont.RGBColor = Color.Blue
    richText.SetFont(0, 7, blueFont)
 
    'Create and set another new font
    Dim redFont As IFont = workbook.CreateFont()
    redFont.Bold = True
    redFont.RGBColor = Color.Red
    richText.SetFont(9, 14, redFont)
 
    'Save the Excel workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete working sample to set rich text to a single cell in a worksheet can be downloaded from Set-RichText.zip.

By executing the program, you will get the output Excel document as follows.

Rich text to single cell in Excel file

Output Excel document

Take a moment to peruse the documentation, where you will find other options like row and column style, applying number formats to cells, cell text alignment, merging and unmerging of cells, font, color and border setting, HTML string formatting and Rich-Text formatting with code examples.

Click here to explore the rich set of Syncfusion® Excel (XlsIO) library features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about how to apply multiple fonts to a single cell in Excel using XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied