Search code examples
epplus

How can I add formatting within a cell that only applies to part of the cell value using EPPlus?


I know how to format cell values in EPPlus but only if the format applies to the whole value. How can I add formatting that only applies to part of the cell value?

for example...

enter image description here


Solution

  • Use the RichText collection to build it:

    using (var pck = new ExcelPackage())
    {
        var wb = pck.Workbook;
        var ws = wb.Worksheets.First();
    
        var cell = ws.Cells["B2"];
        cell.RichText.Add("This is some ");
        cell.RichText.Add("formatting");
        cell.RichText.Add(" withing a value");
    
        cell.RichText[1].Color = Color.Red;
        cell.RichText[1].Size = 14;
    
        pck.Save();
    }