richText.SetFont(0, 7, blueFont);
richText.SetFont(9, 14, redFont);
VB.NET
richText.SetFont(0, 7, blueFont)
richText.SetFont(9, 14, redFont)
Include the following namespaces in Program.cs file.
using Syncfusion.XlsIO;
using System.Drawing;
VB.NET
Imports Syncfusion.XlsIO
Imports System.Drawing
Include the following code snippet in main method of Program.cs file to set rich text to single cell in an Excel worksheet.
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.
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.