如何使用EPPlus库创建Excel表格中带有多种样式的单元格

22

我使用EPPlus来生成Excel文件。

我的意思是,我需要将HTML文本(加粗、斜体、字体颜色、名称、大小参数)转换为Excel单元格。我想它需要创建多样式的单元格,例如:

单元格文本为“hello!”
我想要的样式是:

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

或者(更复杂的情况)

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

我了解MS OpenXML库(它可以让我完成所需的工作)。这个库非常好,但实现起来有点复杂。


解决了!我可以使用这个:http://pastebin.com/wJfcgyhV - Anton Norko
1
请将以下与编程有关的内容从英语翻译为中文。仅返回翻译后的文本:您可以将其编写为答案并接受它以标记问题已解决。 - Aprillion
3个回答

29

问题已解决! 我可以使用这个:

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

3
我创建了一个扩展方法来帮助减少代码量:
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没有类似此功能,或者他们可能已经有了,而我错过了。


3

出于某些原因,安东的回答对我的情况不起作用。我必须使用:

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.0.0.2,请检查您的库的版本。 - Anton Norko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接