How do I apply partial styling to contents of a cell? For example, if a cell contains the following text "Hello World". I want to make the word "Hello" bold while leaving the word "World" unchanged. I've applied styling to the entire cell just haven't been able to apply it to certain portions of a cell.
Asked
Active
Viewed 4,907 times
9
-
In VBA you would look to the `Characters` and format each character individually. – Ron Rosenfeld Oct 26 '16 at 20:35
2 Answers
12
You should try to use ExcelRichText class. For example:
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();
}

PiKos
- 1,344
- 1
- 16
- 15
2
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();
}

Halim Bezek
- 331
- 3
- 5