我为此创建了一个扩展方法,有助于减少一点代码:
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;
}
}
并使用它: 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);
}
不知道为什么 EPPlus 还没有这样的东西,或者他们有,我错过了。