How can I create multistyled cell with EPPlus library for Excel
Asked Answered
J

3

22

I use EPPlus for Excel file generation.

I mean I need to convert HTML text (bold, italic, font color, name, size parameters) to Excel Cell. I suppose it needs to create the multi-styled cell, like:

cell text is "hello!"
the style I want is:

he - bold  
ll - italic  
o! - red colored font  

or (more complex)

hello! - bold  
ll - italic (also bold)  
o! - red colored (also bold)  

I know about MS OpenXML library (it allows me to do what I need). This is good but bit more complex library for implementation.

Juieta answered 2/4, 2012 at 8:40 Comment(2)
Solved!!! I can use that: pastebin.com/wJfcgyhVJuieta
you can write that as an answer and accept it to mark the question as solved..Dillion
J
29

Solved! I can use that:

FileInfo fi = new FileInfo(@"c:\Book1.xlsx");

      using (ExcelPackage package = new ExcelPackage(fi))
      {
        // add a new worksheet to the empty workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets["Inv"];
        //Add the headers

        worksheet.Cells[2, 1].IsRichText = true;
        ExcelRichText ert = worksheet.Cells[2, 1].RichText.Add("bugaga");
        ert.Bold = true;
        ert.Color = System.Drawing.Color.Red;
        ert.Italic = true;

        ert = worksheet.Cells[2, 1].RichText.Add("alohaaaaa");
        ert.Bold = true;
        ert.Color = System.Drawing.Color.Purple;
        ert.Italic = true;

        ert = worksheet.Cells[2, 1].RichText.Add("mm");
        ert.Color = System.Drawing.Color.Peru;
        ert.Italic = false;
        ert.Bold = false;


        package.Save();
      }
Juieta answered 2/4, 2012 at 20:46 Comment(0)
G
3

For some reason Anton's answer didnt work for me. I had to use:

FileInfo fi = new FileInfo(@"c:\Book1.xlsx");

using (ExcelPackage package = new ExcelPackage(fi))
{
    // add a new worksheet to the empty workbook
    ExcelWorksheet worksheet = package.Workbook.Worksheets["Inv"];

    //add multi-coloured text to a cell
    worksheet.Cells[2, 1].IsRichText = true;
    ExcelRichTextCollection rtfCollection = worksheet.Cells[2, 1].RichText;
    ExcelRichText ert = rtfCollection.Add("bugaga");
    ert.Bold = true;
    ert.Color = System.Drawing.Color.Red;
    ert.Italic = true;

    ert = rtfCollection.Add("alohaaaaa");
    ert.Bold = true;
    ert.Color = System.Drawing.Color.Purple;
    ert.Italic = true;

    ert = rtfCollection.Add("mm");
    ert.Color = System.Drawing.Color.Peru;
    ert.Italic = false;
    ert.Bold = false;

    package.Save();
}
Gemagemara answered 13/4, 2012 at 8:16 Comment(1)
I have used the latest version 3.0.0.2 at this moment. Please check your lib's version.Juieta
B
3

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.

Bille answered 2/11, 2017 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.