CellRange.SetRichText
|
Assigns rich formatted text to a cell.
Example I
The following code snippet uses the
RichTextString.AddTextRun
method to compose the cell’s text from individual text runs.
VB.NET
// Create a RichTextString instance.
RichTextString richText = new RichTextString();
// Add three text runs. Each run has its own font settings.
richText.AddTextRun("Rich ", new RichTextRunFont("Arial", 14, System.Drawing.Color.FromArgb(0xc5, 0x9f, 0xc9)));
richText.AddTextRun("text ", new RichTextRunFont("Tahoma", 14, System.Drawing.Color.FromArgb(0x2c, 0x60, 0x8e)));
richText.AddTextRun("formatting", new RichTextRunFont("Castellar", 14, System.Drawing.Color.FromArgb(0x2f, 0x24, 0x4f)));
// Assign the rich formatted text to the cell B2.
worksheet["B2"].SetRichText(richText);
' Create a RichTextString instance.
Dim richText As New RichTextString()
' Add three text runs. Each run has its own font settings.
richText.AddTextRun("Rich ", New RichTextRunFont("Arial", 14, System.Drawing.Color.FromArgb(&Hc5, &H9f, &Hc9)))
richText.AddTextRun("text ", New RichTextRunFont("Tahoma", 14, System.Drawing.Color.FromArgb(&H2c, &H60, &H8e)))
richText.AddTextRun("formatting", New RichTextRunFont("Castellar", 14, System.Drawing.Color.FromArgb(&H2f, &H24, &H4f)))
' Assign the rich formatted text to the cell B2.
worksheet("B2").SetRichText(richText)
Example II
The code snippet below uses the RichTextString.Characters method to format a portion of the cell’s text.
VB.NET
// Create a RichTextString instance.
RichTextString richText = new RichTextString();
// Specify the cell text.
richText.Text = "Rich text formatting";
// Change font characteristics of the first word.
richText.Characters(0, 4).SetFont(new RichTextRunFont("Calibri", 12, System.Drawing.Color.Red));
// Assign the rich formatted text to the cell B2.
worksheet["B2"].SetRichText(richText);
' Create a RichTextString instance.
Dim richText As New RichTextString()
' Specify the cell text.
richText.Text = "Rich text formatting"
' Change font characteristics of the first word.
richText.Characters(0, 4).SetFont(New RichTextRunFont("Calibri", 12, System.Drawing.Color.Red))
' Assign the rich formatted text to the cell B2.
worksheet("B2").SetRichText(richText)
Format Characters within an Existing Text
The following example shows how to apply rich formatting to a cell that already contains a text value.
VB.NET
// Set a cell value.
worksheet["B2"].Value = "Rich text formatting";
// Obtain the RichTextString object containing the cell text.
RichTextString richText = worksheet["B2"].GetRichText();
// Format the first word as bold.
richText.Characters(0, 4).Font.Bold = true;
// Assign the rich formatted text to the cell B2.
worksheet["B2"].SetRichText(richText);
' Set a cell value.
worksheet("B2").Value = "Rich text formatting"
' Obtain the RichTextString object containing the cell text.
Dim richText As RichTextString = worksheet("B2").GetRichText()
' Format the first word as bold.
richText.Characters(0, 4).Font.Bold = True
' Assign the rich formatted text to the cell B2.
worksheet("B2").SetRichText(richText)
Obtain the Entire Rich Text String and Its Length
|