【问题标题】:How can I create multistyled cell with EPPlus library for Excel如何使用 EPPlus 库为 Excel 创建多样式单元格
【发布时间】:2012-04-02 08:40:36
【问题描述】:

我使用EPPlus 生成 Excel 文件。

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

单元格文本是“你好!”
我想要的风格是:

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

或者(更复杂的)

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

我了解 MS OpenXML 库(它允许我做我需要的事情)。这是一个很好但实现起来有点复杂的库。

【问题讨论】:

  • 已解决!!!我可以使用它:pastebin.com/wJfcgyhV
  • 您可以将其写为答案并接受它以将问题标记为已解决..

标签: c# excel openxml epplus


【解决方案1】:

解决了! 我可以使用它:

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】:

    由于某种原因,安东的回答对我不起作用。我不得不使用:

    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。请检查您的 lib 版本。
    【解决方案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 还没有这样的东西,或者也许他们有,但我错过了。

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 2016-03-08
      相关资源
      最近更新 更多