【问题标题】:Give different styles in a single excel cell using EPPlus C#使用 EPPlus C# 在单个 excel 单元格中提供不同的样式
【发布时间】:2021-09-28 19:10:46
【问题描述】:

我找不到以不同样式设置单个 Excel 单元格的方法。例如,我只需要将字符串的一部分加粗,其余部分不加粗在一个单元格中。我只能访问单元格而不是 EPPlus 中的字符。

Excel Image

通常我对单元格的样式是,

ExcelPackage package = new ExcelPackage();
ExcelWorksheet ws = package.Workbook.Worksheets.Add("SheetName");
ws.Cells[1, 1].Style.Font.Bold = true;

我找不到访问单元格中字符的方法。我看到其他一些 excel 插件也这样做,但是 EPPlus 有什么办法可以做到这一点吗?任何建议都会很棒。谢谢

【问题讨论】:

    标签: c# .net excel epplus


    【解决方案1】:

    您必须将单元格设为 RichText 并将内容添加为 RichText:

    using (ExcelPackage ep = new ExcelPackage())
    {
        var sheet = ep.Workbook.Worksheets.Add("Sheet1");
        
        //Creating a RichText Cell
        var cell = sheet.Cells[1,1];
        cell.IsRichText = true;
        //Create the content
        var part1 = cell.RichText.Add("This is blue bold");
        part1.Bold = true;
        part1.Color = System.Drawing.Color.Blue;
        var part2 = cell.RichText.Add(" And this is red and not bold");
        part2.Bold = false;
        part2.Color = System.Drawing.Color.Red;
        
        ep.SaveAs(new FileInfo(myFile));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      相关资源
      最近更新 更多