Home > Software design >  Give different styles in a single excel cell using EPPlus C#
Give different styles in a single excel cell using EPPlus C#

Time:09-28

I cannot find a way to style a single excel cell in different styles. For example I need to make only some part of the string bold and leave the rest unbold in one cell. I can only access Cells not characters in EPPlus.

Excel Image

Usually what I do to style the cell is,

ExcelPackage package = new ExcelPackage();
ExcelWorksheet ws = package.Workbook.Worksheets.Add("SheetName");
ws.Cells[1, 1].Style.Font.Bold = true;

I can't find a way to access characters in a cell. I saw some other excel plugins do the same but Is there any way EPPlus can do this? Any suggestions will be great. Thanks

CodePudding user response:

You have to make the cell a RichText one and add content as RichText:

using (ExcelPackage ep = new ExcelPackage())
{
    var sheet = ep.Workbook.Worksheets.Add("Sheet1");
    
    //Creating a RichText Cell
    var cell = sheet.Cells[1,1];
    cell.IsRichText = true;
    //Create the content
    var part1 = cell.RichText.Add("This is blue bold");
    part1.Bold = true;
    part1.Color = System.Drawing.Color.Blue;
    var part2 = cell.RichText.Add(" And this is red and not bold");
    part2.Bold = false;
    part2.Color = System.Drawing.Color.Red;
    
    ep.SaveAs(new FileInfo(myFile));
}
  • Related