How can I create multistyled cell with EPPlus library for Excel

眉间皱痕 提交于 2019-12-30 03:44:10

问题


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.


回答1:


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



回答2:


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



回答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.



来源:https://stackoverflow.com/questions/9973240/how-can-i-create-multistyled-cell-with-epplus-library-for-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!