How can I create multistyled cell with EPPlus library for Excel

Anton Norko picture Anton Norko · Apr 2, 2012 · Viewed 13.5k times · Source

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.

Answer

Anton Norko picture Anton Norko · Apr 2, 2012

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();
      }