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. Also, converts Excel documents to PDF files. Using this library, it is possible to set Rich Text in a single cell in Excel document.

Steps to set Rich Text to 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 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 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 Program.cs file to set rich text to 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 single cell in 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 trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.

 

 

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