使用 EPplus 对 excel 单元格中的文本应用部分样式

Apply partial styling to text in excel cell using EPpplus

如何将部分样式应用于单元格的内容?例如,如果单元格包含以下文本 "Hello World"。我想将 "Hello" 加粗,同时保持 "World" 不变。我已将样式应用于整个单元格,但无法将其应用于单元格的某些部分。

您应该尝试使用 ExcelRichText class。例如:

var newFile = new FileInfo("example.xlsx");
using (var package = new ExcelPackage(newFile))
{
    var worksheet = package.Workbook.Worksheets.Add("Example");

    var boldRichText = worksheet.Cells[1, 1].RichText.Add("Hello");
    boldRichText.Bold = true;

    var normalRichText = worksheet.Cells[1, 1].RichText.Add(" World");
    normalRichText.Bold = false;

    package.Save();
}
var newFile = new FileInfo("example.xlsx");
using (var package = new ExcelPackage(newFile))
{
var worksheet = package.Workbook.Worksheets.Add("Example");

worksheet.Cells[1, 1].RichText.Add("Hello").Bold = true;
worksheet.Cells[1, 1].RichText.Add(" World").Bold = false;

package.Save();
}