How to insert rich text into a cell

How can i add a RTF string inside a cell?

My RTF string looks like this (just an example):

{\rtf1\deff0 \stshfdbch1\stshfloch0\stshfhich0\stshfbi1{\fonttbl{\f0 Calibri;}{\f1 Times New Roman;}{\f2 Cambria Math;}}{\colortbl ;\red0\green0\blue255 ;\red255\green0\blue0 ;}{*\defchp \fcs1\f1\fcs1\f1\dbch\af1\hich\af0\loch\af0\cgrid1\fs22}{*\defpap \widctlpar}{\stylesheet {\ql\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs22 Normal;}{*\cs1\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs22 Default Paragraph Font;}{*\cs2\sbasedon1\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs20 Line Number;}{*\cs3\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs22\ul\cf1 Hyperlink;}{*\ts4\tsrowd\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs22\ql\tscellpaddfl3\tscellpaddl108\tscellpaddfb3\tscellpaddfr3\tscellpaddr108\tscellpaddft3\tsvertalt\cltxlrtb Normal Table;}{*\ts5\tsrowd\sbasedon4\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs22\ql\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trbrdrh\brdrs\brdrw10\trbrdrv\brdrs\brdrw10\tscellpaddfl3\tscellpaddl108\tscellpaddfb3\tscellpaddfr3\tscellpaddr108\tscellpaddft3\tsvertalt\cltxlrtb Table Simple 1;}}{*\listoverridetable}{\info{\creatim\yr2025\mo4\dy7\hr17\min2}{\revtim\yr2025\mo4\dy7\hr17\min2}{\version1}}{\mmathPr\mbrkBin0\mbrkBinSub0\mdefJc0\minterSp0\mintLim1\mintraSp0\mmathFont2\mnaryLim1\mpostSp0\mpreSp0\mrMargin0\msmallFrac0\mwrapIndent0\mwrapRight0}\nouicompat\htmautsp\splytwnine\alntblind\expshrtn\spltpgpar\nogrowautofit\utinl\nobrkwrptbl\ftnlytwnine\notcvasp\dntblnsbdb\newtblstyruls\pgbrdrhead\pgbrdrfoot\formshade\jexpand\sectd\endnhere\colsx720\pard\plain\ql\widctlpar{\fcs1\af1\alang2067\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs16\lang2067\langfe2067\langnp2067\langfenp2067\cf0 Test }{\fcs1\af1\alang2067\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs16\lang2067\langfe2067\langnp2067\langfenp2067\b\cf0 algemene }{\fcs1\af1\alang2067\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs16\lang2067\langfe2067\langnp2067\langfenp2067\cf0\highlight2 info }\fcs1\af1\ltrch\fcs0\hich\af0\dbch\af1\loch\f0\fs16\par{*\themedata 504b03041400020008000000210201159325b5000000270100000b0000005f72656c732f2e72656c738ccf390ec2301404d0ab58bf274e28104271d2d0a4a041b980e57c2fc29b6c0785b3517024ae409b200a9aa9466f34efe7abed1767c91d5336c133 68aa1a087a1126e31583b9c8dd11faaebda2e5c5049fb589992ccefacc4097124f9466a1d1f15c85887e715686e478c955488a462e6e5c21ddd7f581a6b5015b930c1383344c0d90f111f11f3b4869049e83981dfaf263e2ab0164e449616150343aa4ab bc70cf15a66a711608ed5abab9db7d000000ffff0300504b0304140002000800000021027f624aeb780000007b0000001c0000007468656d652f7468656d652f7468656d654d616e616765722e786d6c04c1d10dc2201000d05508ff72e88731a4b41338 c4a5bd020977344095ddfc702457f0bddfe73b2d83b37a516da988d75763b52259cb9624787df6fdf2d0cb3ca1eb91989e2818a8aac1599a43af63ef8703686b24c666ca413238efa532f6664a0db0557c27099ce166ed1d18936805f31f0000ffff0300 504b0304140002000800000021020a5d705eae0000000c010000270000007468656d652f7468656d652f5f72656c732f7468656d654d616e616765722e786d6c2e72656c738ccf3d0ac23018c6f1ab847737691d44a4691797aed20b84f46d12cc17492a f16c0e1ec92b084e161c1c9fe5f7f07f3d9edd509d25374cd904cfa1a50d10f432ccc62b0e6b59764718faee825614137cd62666529df599832e259e18cb52a313998688be3abb84e444c93424c5a29057a190ed9be6c0d2b7015b938c338734ce2d90e9 1ef11f3b2c8b91780e7275e8cb8f0b56343a043289a4b070f8cc96566781b0be639ba6fe0d0000ffff0300504b030414000200080000002102eb6be9c1c4050000331c0000160000007468656d652f7468656d652f7468656d65312e786d6cec594f6fdb 3614ff2a04efade4bf8d832a45ecd8ed96a60d12af438fcf122db1a14881a493f836b4c701038675c32e0376db61d856a0057658877d986c1db60ec8571844c996644b8dd3a4dd80d6066292fafdde7b7ceff191544e7ff9edfa8de390a143221515dcc1 b5ab364684bbc2a3dc77f0448faface11b1bd7615d072424e838645cad838303ada375cb526e404250574544f871c8c64286a0d555217dcb937044b91f32ab6edb6d2b04ca31e2101207df1d8fa94bf05c6c9f919070ade20197c9fd582859c67a07b5f8 474d558f497408ccc147947be268488e35460c94ee31e960db7c30b236ae5b7316d315e41c71603e3362caf00eea8628fdd19c591b343bd7b6320d06c1f432b0dfeff7fab54ca24180eb129e9a930737076bb5ee5c6a0e953497a5f7ec96dd5c20e43434 96089d6eb7dbea14090695349b4b8435bbdddcac17090695345bcb73e86ef67aed22c1a092667b8930b8d669371708061530ca0f96e07164b310cd3163c16e95e2d76cdb5e9be74206b37299b66104705d9577213c107220b83651064d39d2d3888cc125 0eee41389214308a800b451c6cd7ed81ddb0ebe6db34ada6d10feb0472c474cc55cb63b13148b99246dac19b1246388719d2902874871ca13d11022fa37409f7f3947b92720fca903de02a8fec4fa28084b41c1b1099c7ee32e01a7cc28946f133714048 196f8b1c429eb703dc075686eceb80e69177a6c04a4db94944c194fd291b0329f5c5cdc983223490134dcb91729247ee011c96026f41d1bd2fbffbfc9f6f3e417f3ffdf6e5e32f2b08052ffff1e3a7bffffa450552e7912fbe7af2e7b3272fbefeecafef 1f97e2c9489e33393e8ce2f18c72fafc87d3e74fd1e9f327270f9f9d3cfcf9e4d1a393873f9531b783b0a06c4708d615b25ccdb6c9b89c6513ee97c6f2360891076e89835e0061540ade61d3300fde06a9e941395414a314f705a3c05117687906dc95d3 82cddbc06885d5fb940779e807ea4008066857e872fc54ba8585a6b404ee132650df234a95928610b23ce936e8a01c485821756fc24443583ac76100c5d5780f7505ab821696e310021196eba7a342daee50570a25c61adda221309896b23ea27e50ce8a 9f4c6419e71e25fa9c097f9f525aaee63ecde582b550e843ca5f55f5191d49ba50f55b6faeea6f4a5a5e33dfd7fa77b5d66f56a5c4e555f82d98f05d1257baf705fe7d81ff4f0a7c65969fa3ac6795dcca9ff48dc8b0f2d83fa68cedeb2923b795d90394 60d41b50c64cc790e6d78c28e8b1643ad602d09760da480afd31d5c17e00117170cda8f0552adb572812cac136ae146e6ecb94eb64ac35bfe2c2ba02bd23bc64bc51b8fbce05999eaff2aa1ab18855d535ae5d545d2d41aea8afd6aad0d77ab53e2be753 463982f82d46ad5d4fcd542e30e2c5de4f446bcac81e71752a6f16ab4b8f9b0ac02369e06ae533ab3556f5647ca55d5d5fa771517dab442eafb059a5b07519a1b3cf0a9db5bc72192ff6d091833bad7a0b23378c3c07ab782301e67307bb3a9dd0998b7d 71daad8a69dbd5d32e2889a4d25ba08284661ecddf1bf1ccee7aab198bbb80e165556a45e58db5dadb576e2d06908cc7c4d5152359377d26269ac8fdc03b422336917be03938ce501b238f2aede0faac231d1cfbd6f48a8b3e4dfec5f750e9a200160590 96a7786dcee798e04d7b6e84e9e5ecb32a8c7fcdb9342e712ead77792e71fa124e1a9eb9ad414824a0384f1d2ca40e842f210aa83b90826ba34c0a8d1868539f58fc263d36961cceea9535139254373fd07bd44792fa0ed681246457a7333d435a6d560e d3e5914a4a6bcbdc601525bf237248d8305eb9edd8051805f30292fac200170357eca7fe18f983fff38128cd9d739f183255898455d5e5cb7d6e17e85cd48a73eeb669f55a52586fadbedb46a00314ff71b04ba5cbb2e3ef50982d363b6f6a075f49ce1f 285e96496be4e05a3a98a88b65bdcde3551696cc8c37794e5599ffb353d482ffcf50f8fafe4f5b05f717b2eb4cef5bcb8b393e34cd6e42a6b7f07fb9d9c8c6bf000000ffff0300504b030414000200080000002102e9278183f500000010020000130000 005b436f6e74656e745f54797065735d2e786d6cac913d4ec4301046af62b945b1b31408a1385b00253f0517183993c4c21e5bf6240a67a3e0485c016d765921b4200a9a69e69bf73e69de5fdf9aed12bc98311717c9c88daaa540b2b173341839715f5d ca6ddb3cbd242c62099e8a912373bad2bad81103141513d2127c1f73002e2ae64127b0cf30a03eafeb0b6d23311257bc63c8b6b9c11e26cfe27661a4bd36a32f525cef833b979190927716d845d23375df2cd5c1a032fa35534697cad912bc14faa4625d fd68f83c7c983167d7a17884ccf710d048cd2306fc3a37ea77d889bab1ef9dc52eda2920b15a31c7b67f92de01c180f97fd407d8b1815effdb7e000000ffff0300504b010214001400020008000000210201159325b5000000270100000b000000000000 00000000000000000000005f72656c732f2e72656c73504b01021400140002000800000021027f624aeb780000007b0000001c00000000000000000000000000de0000007468656d652f7468656d652f7468656d654d616e616765722e786d6c504b0102 1400140002000800000021020a5d705eae0000000c0100002700000000000000000000000000900100007468656d652f7468656d652f5f72656c732f7468656d654d616e616765722e786d6c2e72656c73504b0102140014000200080000002102eb6be9 c1c4050000331c00001600000000000000000000000000830200007468656d652f7468656d652f7468656d65312e786d6c504b0102140014000200080000002102e9278183f50000001002000013000000000000000000000000007b0800005b436f6e74 656e745f54797065735d2e786d6c504b050600000000050005005d010000a10900000000}{*\colorschememapping 3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d227574662d38223f3e3c613a636c724d617020786d6c6e73 3a613d22687474703a2f2f736368656d61732e6f70656e786d6c666f726d6174732e6f72672f64726177696e676d6c2f323030362f6d61696e22206267313d226c743122207478313d22646b3122206267323d226c743222207478323d22646b32222061 6363656e74313d22616363656e74312220616363656e74323d22616363656e74322220616363656e74333d22616363656e74332220616363656e74343d22616363656e74342220616363656e74353d22616363656e74352220616363656e74363d226163 63656e74362220686c696e6b3d22686c696e6b2220666f6c486c696e6b3d22666f6c486c696e6b22202f3e}}

With Aspose.Words i can insert a RTF string into a document using the code:

public static void WriteRTF(DocumentBuilder builder, string bookmark, string rtf)
{
    if (string.IsNullOrEmpty(rtf)) return;

    builder.MoveToMergeField(bookmark);

    byte[] rtfBytes = Encoding.UTF8.GetBytes(rtf);
    using (MemoryStream rtfStream = new MemoryStream(rtfBytes))
    {
        var rtfDoc = new Document(rtfStream);
        builder.InsertDocument(rtfDoc, ImportFormatMode.KeepSourceFormatting);
    }
}

So how can i do this with Aspose.Cells?

@tom.procureur

To insert rich text into a cell using Aspose.Cells, you can utilize the HtmlString property of the Cell class. This allows you to add HTML formatted text, which can include various styles and formatting options.

Here’s a simple example demonstrating how to insert HTML formatted text into a cell:

using System;
using Aspose.Cells;

namespace Aspose.Cells.Examples
{
    public class AddingHTMLRichTextInCell
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Access the cell where you want to insert rich text
            Cell cell = worksheet.Cells["A1"];
            
            // Set the HTML formatted string
            cell.HtmlString = "<Font Style=\"FONT-WEIGHT: bold; FONT-STYLE: italic; TEXT-DECORATION: underline; FONT-FAMILY: Arial; FONT-SIZE: 11pt; COLOR: #ff0000;\">This is simple HTML formatted text.</Font>";

            // Save the workbook
            workbook.Save("output_out.xlsx");
        }
    }
}

In this example, the HtmlString property is used to insert a string that contains HTML formatting. You can customize the HTML string to include various styles as needed.

If you need to work with RTF (Rich Text Format) specifically, Aspose.Cells does not directly support RTF strings in the same way it does for HTML. However, you can convert RTF to HTML and then use the above method to insert it into a cell.

For more detailed operations on rich text, such as accessing and updating specific portions of rich text within a cell, you can use the GetCharacters() and SetCharacters() methods. These methods allow you to manipulate the font settings of different portions of text within a cell.

For further information, you can refer to the official documentation on accessing and updating rich text portions in cells here and check out the example code here.

Feel free to ask if you have any more questions!

Does Aspose offers a conversion to convert the RTF to HTML? Is there any documentation about which HTML code is supported ?

I’m inserting data into a workboo with the following code:

private Workbook ExportToExcel<T>(string[] headers, List<T> data)
{
    Workbook workBook = new Workbook();
    Worksheet sheet = workBook.Worksheets[0];

    ImportTableOptions imp = new ImportTableOptions();
    imp.InsertRows = true;
    imp.IsFieldNameShown = false;

    sheet.Cells.ImportArray(headers, 0, 0, false);
    sheet.AutoFilter.Range = $"A1:{CellsHelper.CellIndexToName(0, headers.Length - 1)}";

    if (data.Any()) sheet.Cells.ImportCustomObjects(data, 1, 0, imp);

    return workBook;
}

If i change my code so that the List data contains a HTML string, instead of a RTF string.
How could i then for instance change the column ‘L’ that this needs to render the HTML?

@tom.procureur,

You cannot insert RTF to be parsed into a worksheet cell accordingly. Aspose.Cells will take it as normal string. I think you may use Aspose.Words to convert your RTF string to HTML and then insert the HTML string into the cell via Cell.HtmlString attribute. Alternatively, you may use manual approach to specify rich text for the cell using relevant Aspose.Cells APIs, see the document for your reference.

You may add a line to your code segment:

imp.IsHtmlString = true;

Hope, this helps a bit.

I’ve converted the rtf text to html, and set the imp.IsHtmlString = true.

This indeed inserts the html, but the styles are not applied.

For instance my HTML string looks like this:

Test reden terugsturen besluit

Inside the Excel column it shows the text: Test reden terugsturen besluit.
But there isnt any style applied to it

@tom.procureur,

I tested your scenario using sample code to assign the HTML string to the cell via Cell.HtmlString, and it worked successfully.
e.g.,
Sample code:

// Create a new workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

// Access the cell where you want to insert rich text
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
            
// Set the HTML formatted string
cell.HtmlString = "<html><body>   <p style=\"text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt;\"><span style=\"color:#000000;background-color:transparent;font-family:Calibri;font-size:8pt;font-weight:normal;font-style:normal;\">Test </span><span style=\"color:#000000;background-color:#FFFF00;font-family:Calibri;font-size:8pt;font-weight:normal;font-style:normal;\">reden </span><span style=\"color:#FF0000;background-color:transparent;font-family:Calibri;font-size:8pt;font-weight:normal;font-style:normal;\">terugsturen </span><span style=\"color:#000000;background-color:transparent;font-family:Calibri;font-size:8pt;font-weight:bold;font-style:normal;text-decoration: underline;\">besluit</span></p></body></html>";

// Save the workbook
workbook.Save("e:\\test2\\out1.xlsx");

Please find attached the output Excel file for your reference.
out1.zip (6.1 KB)

You can see cell A1 contains rich text formatting. Please note that if you intend to highlight the text string “reden” with a yellow background, this is not possible due to limitations in cell formatting in MS Excel. In Microsoft Excel, it is not feasible to apply a background color to only a portion of the text within a cell. You can verify this limitation directly in Microsoft Excel.