I've created an extension method for this which helps to reduce the code a tad:
public static class RichtTextExtensions
{
public static ExcelRichText Add(this ExcelRichTextCollection richTextCollection,
string text, bool bold = false, bool italic = false, Color? color = null, float size = 11,
bool underline = false, string fontName = "Segoe UI Light")
{
var richText = richTextCollection.Add(text);
richText.Color = color ?? Color.Black;
richText.Bold = bold;
richText.Italic = italic;
richText.Size = size;
richText.FontName = fontName;
richText.UnderLine = underline;
return richText;
}
}
And to use it:
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
using (ExcelRange cellRange = worksheet.Cells[1,1])
{
cellRange.RichText.Add("This is ", size: 18, underline:true);
cellRange.RichText.Add("a simple ", bold: true, size: 18, underline: true);
cellRange.RichText.Add("test ", size: 18, underline: true);
cellRange.RichText.Add("of the extension method", bold: true, size: 18, underline: true);
}
Not sure why EPPlus doesn't already have something like this, or perhaps they do and I missed it.