How to apply superscript to certain text in a cell?
24 Jun 20264 minutes to read
The following code example illustrates how to apply superscript to certain text in a cell without affecting the existing style in C# (cross-platform and Windows-specific) and VB.NET.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Create a workbook
IWorkbook workbook = application.Workbooks.Open("../../../Data/Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Add Text
IRange range = worksheet.Range["A1"];
IRichTextString richText = range.RichText;
IFont superScript = workbook.CreateFont();
superScript.Size = richText.GetFont(6).Size;
superScript.FontName = richText.GetFont(6).FontName;
superScript.Color = richText.GetFont(6).Color;
superScript.Superscript = true;
richText.SetFont(6, 6, superScript);
//Save the workbook to disk in xlsx format
workbook.SaveAs("../../../Output/Output.xlsx");
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Create a workbook
IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Add Text
IRange range = worksheet.Range["A1"];
IRichTextString richText = range.RichText;
IFont superScript = workbook.CreateFont();
superScript.Size = richText.GetFont(6).Size;
superScript.FontName = richText.GetFont(6).FontName;
superScript.Color = richText.GetFont(6).Color;
superScript.Superscript = true;
richText.SetFont(6, 6, superScript);
//Save the workbook to disk in xlsx format
workbook.SaveAs("../../Output/Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
'Create a workbook
Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/Sample.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Add Text
Dim range As IRange = worksheet.Range("A1")
Dim richText As IRichTextString = range.RichText
Dim superScript As IFont = workbook.CreateFont()
superScript.Size = richText.GetFont(6).Size
superScript.FontName = richText.GetFont(6).FontName
superScript.Color = richText.GetFont(6).Color
superScript.Superscript = True
richText.SetFont(6, 6, superScript)
'Save the workbook to disk in xlsx format
workbook.SaveAs("../../Output/Output.xlsx")
End UsingA complete working example to apply superscript to certain text in a cell using C# is present on this GitHub page.